Uses old Rust 2015
#65 in #c
C API helper functinos for Rust
Helper functions for letting C programs create and free objects in Rust libraries.
NB: This will be obsolete once Box::from_raw/into_raw are stable.
These are helper methods for building C APIs that have "alloc" and "free" methods to handle memory allocation of Rust objects.
The pair of methods
to_owned_from_c() allows passing ownership of a Rust object to C and back.
You can create a Rust object and pass it to C with
to_c_owned(), and you don't need to keep the reference to the object anywhere in the Rust part of the program.
Any object given to C via
to_c_owned() has to be claimed back via
to_owned_from_c(). You need to create a C function for deallocation of objects that then calls
Objects owned by C can be borrowed back to the Rust land by using
extern "C"'s function type. If, for some reason, you declare functions to take a pointer you can get a reference via
as_ref_from_c(), but this should usually be unneccessary.
const versions of methods are only for convenience and are interchangeable.