pub struct CallbackOnce<T: 'static>(/* private fields */);Expand description
Type-erased one-shot callback.
Use this to wrap an FnOnce closure into a data structure that may be passed via a c_void
pointer as user data to an external library. Later, when this extern callback is run with that
data, we may unwrap it and can thus call our initial closure.
The implementation uses associated methods:
CallbackOnce::prepare()to wrap the closure and get thec_voidpointerCallbackOnce::execute()to unwrap thec_voidpointer and call the closure
§Examples
use open62541::CallbackOnce;
use std::{cell::Cell, rc::Rc};
let cell = Rc::new(Cell::new(0));
// Turn `tx` into type-erased void pointer for FFI.
let raw_data: *mut c_void = CallbackOnce::<u32>::prepare({
let cell = Rc::clone(&cell);
move |value| {
cell.set(value);
}
});
// Use type-erased pointer to call closure.
unsafe { CallbackOnce::<u32>::execute(raw_data, 123); }
// Value has been received.
assert_eq!(cell.get(), 123);Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for CallbackOnce<T>
impl<T> RefUnwindSafe for CallbackOnce<T>where
T: RefUnwindSafe,
impl<T> Send for CallbackOnce<T>where
T: Send,
impl<T> Sync for CallbackOnce<T>where
T: Sync,
impl<T> Unpin for CallbackOnce<T>where
T: Unpin,
impl<T> UnwindSafe for CallbackOnce<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more