Setting the include path with bindgen

With the API you can use Builder::clang_arg with arbitrary arguments: let b = bindgen::builder().header(“foo.h”).clang_arg(“-I/path”); From the command line you can do the same by appending arguments after –, like: bindgen foo.h — -I/path

How to expose a Rust `Vec` to FFI?

If you just want some C function to mutably borrow the Vec, you can do it like this: extern “C” { fn some_c_function(ptr: *mut i32, len: ffi::size_t); } fn safe_wrapper(a: &mut [i32]) { unsafe { some_c_function(a.as_mut_ptr(), a.len() as ffi::size_t); } } Of course, the C function shouldn’t store this pointer somewhere else because that would … Read more

Working with c_void in an FFI

You can’t cast a struct to c_void, but you can cast a reference to the struct to *mut c_void and back using some pointer casts: fn my_callback(con: *tsm_screen, …, data: *mut c_void) { // unsafe is needed because we dereference a raw pointer here let data: &mut State = unsafe { &mut *(data as *mut … Read more

Mixing Haskell and C++

The benefit of Haskell is the powerful abstractions it allows you to use. You’re not thinking in terms of ones and zeros and addresses and registers but computations and type properties and continuations. The benefit of C++ is how tightly you can optimize it when necessary. You aren’t thinking about high-minded monads, arrows, partial application, … Read more