[][src]Struct c_closures::Closure

#[repr(C)]pub struct Closure {
    pub function: Option<unsafe extern "C" fn(data: *mut c_void, arg: *mut c_void) -> *mut c_void>,
    pub data: *mut c_void,
    pub delete_data: Option<unsafe extern "C" fn(data: *mut c_void)>,
    pub delete_ret: Option<unsafe extern "C" fn(ret: *mut c_void)>,
}

A general purpose closure type defined in C code which can be created in Rust.

Fields

function: Option<unsafe extern "C" fn(data: *mut c_void, arg: *mut c_void) -> *mut c_void>

Directions to call the contained closure

data: *mut c_void

Rust user data for this closure.

delete_data: Option<unsafe extern "C" fn(data: *mut c_void)>

The data pointer may require personalized delete instructions, we can access those here.

delete_ret: Option<unsafe extern "C" fn(ret: *mut c_void)>

The value returned by this function may require personalized delete instructions, we can access those here.

Implementations

impl Closure[src]

pub fn fn_mut<Arg, Return, Function>(f: Function) -> Self where
    Arg: FromClosureArgPointer,
    Function: FnMut(Arg) -> Return + Send + 'static, 
[src]

Transform an FnMut Rust closure into a structure you can pass into a C/C++ library.

This structure currently assumes it will never be called in multiple threads simultaneously. If that guarantee cannot be upheld, then you should instead use fn_not_mut.

let mut y = 5;
let _f = Closure::fn_mut(move |x: &i32| {
    y *= 2;
    *x * 2
});

pub fn fn_not_mut<Arg, Return, Function>(f: Function) -> Self where
    Arg: FromClosureArgPointer,
    Function: Fn(Arg) -> Return + Send + 'static, 
[src]

Transform an Fn Rust closure into a structure you can pass into a C/C++ library.

This structure is safe to use in multiple threads simultaneously. If your usage is single threaded, consider fn_mut instead as it permits more robust closures.

let y = 5;
let _f = Closure::fn_not_mut(move |x: &i32| {
    *x * y
});

pub fn fn_once<Arg, Return, Function>(f: Function) -> Self where
    Arg: FromClosureArgPointer,
    Function: FnOnce(Arg) -> Return + Send + 'static, 
[src]

Transform an FnOnce Rust closure into a structure you can pass into a C/C++ library.

This structure assumes it will only ever be called once. If you attempt to call it more than once the return value will be zeroed memory. If the return type does not consider zeroed memory to be a valid representation, then usage of the return type in this instance may result in undefined behavior.

let values = vec![String::from("1"), String::from("2"), String::from("3")];
let _f = Closure::fn_once(move |_: ()| {
    for item in &values {
        println!("Item: {}", item);
    }
    // Probably not how this would actually be used, just to demonstrate that we can.
    std::mem::drop(values);
});

pub fn new_noop() -> Self[src]

Constructs a new instance of this class that when called does nothing. It provides all possible signatures simultaneously, excluding those with a return value, because the Closure machinery will do nothing with it.

pub fn rebind_closure_ref<C: ClosureMarkerTrait>(&self) -> &C[src]

Similar to the rebind_closure macro, except this operates on immutable references instead.

pub fn rebind_closure_mut<C: ClosureMarkerTrait>(&mut self) -> &mut C[src]

Similar to the rebind_closure macro, except this operates on mutable references instead.

Trait Implementations

impl ClosureMarkerTrait for Closure[src]

impl Debug for Closure[src]

impl Drop for Closure[src]

Auto Trait Implementations

impl RefUnwindSafe for Closure

impl !Send for Closure

impl !Sync for Closure

impl Unpin for Closure

impl UnwindSafe for Closure

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.