Struct CallbackOnce

Source
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:

§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§

Source§

impl<T> CallbackOnce<T>

Source

pub fn prepare<F>(f: F) -> *mut c_void
where F: FnOnce(T) + 'static,

Prepares closure for later call.

This allocates memory. To prevent memory leaks, call execute() on the returned pointer exactly once.

Source

pub unsafe fn execute(data: *mut c_void, payload: T)

Unwraps c_void pointer and calls closure.

§Safety

The given pointer must have been returned from prepare() and must not have been passed into execute() yet.

The value type T must be the same as in prepare().

Trait Implementations§

Source§

impl<T: Debug + 'static> Debug for CallbackOnce<T>

Source§

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

Formats the value using the given formatter. Read more

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> 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.