Crate capi [−] [src]
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.
Traits
FromCOwned |
Takes ownership back from C. The pointer must not be used after |
ToCOwned |
Takes a Box and returns a C pointer that can be given to a C program to use for as long as it wants. |