Struct Closure

Source
#[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)>, }
Expand description

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§

Source§

impl Closure

Source

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

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
});
Source

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

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
});
Source

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

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);
});
Source

pub fn new_noop() -> Self

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.

Source

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

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

Source

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

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

Trait Implementations§

Source§

impl Debug for Closure

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Closure

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl ClosureMarkerTrait for Closure

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.