Trait capi::FromCOwned [] [src]

pub trait FromCOwned<T> {
    unsafe fn to_owned_from_c(self) -> T;
    unsafe fn as_ref_from_c(&self) -> &T;
    unsafe fn as_ref_from_c_mut(&mut self) -> &mut T;
}

Takes ownership back from C. The pointer must not be used after to_owned_from_c() is called.

Every call to to_c_owned() must have a corresponding one call to to_owned_from_c().

If to_owned_from_c() is not called Rust will leak memory. If to_owned_from_c() is called more than once on the same pointer the program will crash or corrupt data.

use capi::FromCOwned;
struct Foo;

#[no_mangle]
unsafe extern "C" fn destroy_foo(ptr: *const Foo) {
    ptr.to_owned_from_c();
}

Required Methods

unsafe fn to_owned_from_c(self) -> T

The pointer must not be null.

The returned object will be owned by Rust and will be freed if you don't store a reference to it.

unsafe fn as_ref_from_c(&self) -> &T

Lets you "peek" the content pointed to by the pointer without taking ownership back. You're borrowing the object from C.

It's usually better to declare functions as taking Rust references (e.g. extern "C" fn use_foo(obj: &Foo)) instead of using this method.

unsafe fn as_ref_from_c_mut(&mut self) -> &mut T

Implementors