#[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_voidRust 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
impl Closure
Sourcepub fn fn_mut<Arg, Return, Function>(f: Function) -> Self
pub fn fn_mut<Arg, Return, Function>(f: Function) -> Self
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
});Sourcepub fn fn_not_mut<Arg, Return, Function>(f: Function) -> Self
pub fn fn_not_mut<Arg, Return, Function>(f: Function) -> Self
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
});Sourcepub fn fn_once<Arg, Return, Function>(f: Function) -> Self
pub fn fn_once<Arg, Return, Function>(f: Function) -> Self
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);
});Sourcepub fn new_noop() -> Self
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.
Sourcepub fn rebind_closure_ref<C: ClosureMarkerTrait>(&self) -> &C
pub fn rebind_closure_ref<C: ClosureMarkerTrait>(&self) -> &C
Similar to the rebind_closure macro, except this operates on immutable references instead.
Sourcepub fn rebind_closure_mut<C: ClosureMarkerTrait>(&mut self) -> &mut C
pub fn rebind_closure_mut<C: ClosureMarkerTrait>(&mut self) -> &mut C
Similar to the rebind_closure macro, except this operates on mutable references instead.