#c #ffi #api

yanked capi

Helper methods for building a C API in Rust: passing ownership of Rust objects to C and back. NB: This will be obsolete once Box::from_raw/into_raw are stable

Uses old Rust 2015

0.0.3 Sep 12, 2015
0.0.2 Feb 7, 2015
0.0.1 Feb 7, 2015

#65 in #c

BSD-2-Clause

6KB
81 lines

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.

See crates.io.


lib.rs:

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_c_owned() and 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 to_owned_from_c().

Objects owned by C can be borrowed back to the Rust land by using &T as 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.

mut and const versions of methods are only for convenience and are interchangeable.

No runtime deps