Skip to content

Instantly share code, notes, and snippets.

@ChunMinChang
ChunMinChang / eq_sample.rs
Created October 3, 2018 20:39
A counterexample to use the memory allocated in external library
// A module containing all the types and APIs in the external library.
mod sys {
use std::cmp;
use std::ffi::CString;
use std::os::raw::{c_char, c_void};
pub type XString = *mut c_void;
extern "C" {
fn calloc(items: usize, size: usize) -> *mut c_void;
@ChunMinChang
ChunMinChang / Makefile
Created October 2, 2018 16:40
C, C++, Rust Examples to call C-compatible Query APIs
all:
# Sample in C(static library):
# gcc -c string_ref.c -o string_ref.o
# ar rcs libstringref.a string_ref.o
# gcc -c device.c -o device.o
# ar rcs libdevice.a device.o
# gcc sample.c -L. -ldevice -lstringref -o sample-c
# ./sample-c
# Sample in C:
@ChunMinChang
ChunMinChang / Makefile
Last active October 18, 2021 07:02
opaque or transparent interface of the external library
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-c sample.c -L. -lext
./sample-c
# g++ -o sample-cpp sample.cpp libext.a
g++ -o sample-cpp sample.cpp -L. -lext
./sample-cpp
@ChunMinChang
ChunMinChang / README.md
Last active September 13, 2018 03:08
Don't use the pointers of the instances allocated in functions stack

Don't misuse the pointers of the instances allocated in functions stack

struct Foo { ... }

fn create_foo() -> Foo {
    let foo = Foo { ... };
    foo
}
@ChunMinChang
ChunMinChang / README.md
Last active September 13, 2018 03:08
A counterexample to register the callback functions to the external libraries

A counterexample to register the callback functions to the external libraries

The key point for this gist repo is to indicate:

Don't misuse the pointers of the instances allocated in functions stack since they will be gone after finishing the function calling.

The sample code here is to demonstrate the problem I have when I am developing a [Rust audio library][draft]. To play sounds, it needs to register callbacks to the underlying OS through audio APIs and then the callbacks will be fired periodically. The callback usually will come with at least 2 parameters:

  1. a buffer that will be filled with audio data
  2. a pointer indicating what variable calls the callback.

To create Rust APIs that can play audio, I create a struct for audio stream named Stream and store all the stream related settings like sampling rate or channels in that struct. There is a struct method named new that will create a Stream instance that will be returnd. Before the instance is returned, I use the pointer of

@ChunMinChang
ChunMinChang / audio_device_utils.rs
Last active September 4, 2018 19:56
Access data from CoreAudio APIs with a single-element tuple structs wrapping native CoreAudio types
#[path = ""]
mod utils {
#[path = "audio_object.rs"]
mod audio_object;
use self::audio_object::{get_property_data, sys::*};
use std::fmt; // For fmt::{Display, Formatter, Formatter}
#[derive(PartialEq)]
pub enum Scope {
@ChunMinChang
ChunMinChang / Makefile
Last active October 1, 2018 20:51
Wrap native type by tuple struct
all:
# Sample in C:
# gcc -shared -fPIC device.c -o libdevice.so
# gcc sample.c libdevice.so -o sample-c
# ./sample-c
# Sample in C++:
g++ -std=c++11 -shared -fPIC device.cpp -o libdevice.so
g++ -std=c++11 sample.cpp libdevice.so -o sample-cpp
./sample-cpp
@ChunMinChang
ChunMinChang / size.cpp
Last active August 28, 2018 00:15
Size of struct/class
#include <cassert>
typedef int NewType;
struct A {
NewType a;
};
struct B {
NewType b;
@ChunMinChang
ChunMinChang / Makefile
Last active September 28, 2018 23:08
Using tuple struct to wrap native C types
all:
gcc -shared -fPIC ext.c -o libext.so
rustc problem.rs -L.
LD_LIBRARY_PATH=. ./problem
rustc solution.rs -L.
LD_LIBRARY_PATH=. ./solution
clean:
rm problem
rm solution
@ChunMinChang
ChunMinChang / error_passing_with_converted_error_type.rs
Created August 23, 2018 17:51
Passing error from inner modules to outer modules
// Converting the error types of inner modules to the error types of outer modules.
mod server {
use std::fmt; // For fmt::Debug trait.
pub enum Error {
File(file::Error),
Network(network::Error),
Others,
}