Skip to content

Instantly share code, notes, and snippets.

@ChunMinChang
ChunMinChang / Makefile
Last active March 19, 2020 21:58
LRU Table sample
# Build C++ client example
# Process with GNU make
all: test
check: all
./test
HEADER := lru_table.h
CXXFLAGS = -g -Wall -std=c++17 -DLRU_TABLE_DEBUG=1
@ChunMinChang
ChunMinChang / player.cpp
Last active November 12, 2019 23:10
An example of mapping strings and functions
#include <assert.h>
#include <iostream>
#include <string>
#include <unordered_map>
class Player {
public:
static void DoAction(std::string action) {
std::string message;
@ChunMinChang
ChunMinChang / mixer.rs
Created October 23, 2019 14:36
A mixer prototype
#[derive(Clone, Debug, PartialEq)]
enum Channel {
FrontLeft = 0,
FrontRight = 1,
FrontCenter = 2,
LowFrequency = 3,
BackLeft = 4,
BackRight = 5,
FrontLeftOfCenter = 6,
FrontRightOfCenter = 7,
@ChunMinChang
ChunMinChang / finally.h
Created February 7, 2019 23:13
Finally: A function call that runs at the end of a scope to tear things down.
#ifndef FINALLY_H
#define FINALLY_H
#include <utility>
template<class Lambda>
class Finally {
public:
Finally(Lambda &&func): func_(std::forward<Lambda>(func)) {};
~Finally() { func_(); };
@ChunMinChang
ChunMinChang / sample.rs
Last active January 16, 2019 06:43
Compute X * Y without arithmetical operators
fn main() {
println!("123 * 345 = {}", multiply(123, 345));
}
fn multiply(mut a: u32, mut b: u32) -> u32 {
let mut sum = 0;
while a != 0 {
if a & 1 == 1 {
sum = add(sum, b);
}
@ChunMinChang
ChunMinChang / Makefile
Last active December 31, 2018 19:52
A mistake when using a Rust vector as a buffer to get the data by a C API
all:
# Build a static library
gcc -c -o query.o query.c
ar rcs libquery.a query.o
# Build and run a C sample
gcc sample.c libquery.a -o sample-c
./sample-c
# Build and run a C++ sample
g++ sample.cpp libquery.a -o sample-cpp
./sample-cpp
@ChunMinChang
ChunMinChang / lifetime.md
Last active April 21, 2020 09:07
Note for Rust lifetime

Lifetimes for The Rust References

Terms

  • x outlives y
    • When y is alive, x is alive. But x and y can be destroyed at the same time. That is, x must live at least as long as y.
  • x strictly outlives y
    • When y is alive, x is alive, and x must be alive when y is destroyed. That is, x lives longer than y.
  • If starttime(z) and endtime(z) are the timestamps when z is created and destroyed, then
    • x outlives y: (starttime(x) <= starttime(y)) ∧ (endtime(x) >= endtime(y)).
  • x strictly outlives y: (starttime(x) <= starttime(y)) ∧ (endtime(x) > endtime(y)).
@ChunMinChang
ChunMinChang / makefile
Last active October 29, 2018 01:08
Rust wrappers for AudioObjectAddPropertyListener on OSX
all:
rustc property_listener_native_apis.rs
rustc property_listener_generic_type.rs
rustc property_listener_within_context.rs
clean:
rm property_listener_native_apis
rm property_listener_generic_type
rm property_listener_within_context
@ChunMinChang
ChunMinChang / dispatch_api.rs
Last active February 20, 2020 21:00
Rust wrappers for OSX dispatch_async
mod sys;
use std::ffi::CString;
use std::os::raw::c_void;
use std::{thread, time::Duration};
// A macro to print the function name
macro_rules! function {
() => {{
fn f() {}
@ChunMinChang
ChunMinChang / Makefile
Last active February 13, 2019 19:17
Pass arrays from Rust to C
all:
# Build a static library from the Rust file
rustc --crate-type=staticlib ext.rs
# Compile the C file with the static library
# gcc -o sample-c sample.c libext.a
gcc -o sample sample.c -L. -lext
./sample
clean:
rm libext.a
rm sample