The other way around is relatively well defined and has been the subject of a lot of attention for several decades. Is the Rust-lib-called-from-C even feasible?
The other way around is relatively well defined and has been the subject of a lot of attention for several decades. Is the Rust-lib-called-from-C even feasible?
If the Rust code uses the "StupidAlloc" allocator, it should work as expected. There may be some constructor/destructor code involved with its usual complications.
But it would not replace malloc/free in C code nor will it be compatible with C++ allocators for std containers.
#[global_allocator]
static GLOBAL: StupidAlloc = StupidAlloc;
#[no_mangle]
pub unsafe extern "C" fn stupid_malloc(size: usize) -> *mut u8 {
let layout = Layout::from_size_align(size, 1).unwrap();
GLOBAL.alloc(layout)
}
#[no_mangle]
pub unsafe extern "C" fn stupid_free(ptr: *mut u8) {
let layout = Layout::from_size_align(1, 1).unwrap();
GLOBAL.dealloc(ptr, layout)
}
then build with --crate-type cdylib, giving you a .so you can linkZig nailed it, Rust fucked it up. Time will tell if thats big enough of an issue to warrant some bigger changes to fix it.
See for example this rsvg presentation[1] and gstreamer blog post[2] for examples of the interop
[1] https://viruta.org/docs/fmq-porting-c-to-rust.pdf
[2] https://coaxion.net/blog/2016/05/writing-gstreamer-plugins-a...