Struct CallbackStream

Source
pub struct CallbackStream<T: 'static>(/* private fields */);
Expand description

Type-erased stream sender.

Use this to wrap a Sender 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 send messages into the stream or close the stream.

The implementation uses associated methods:

§Examples

use futures::executor::block_on;
use open62541::CallbackStream;
use tokio::sync::mpsc;

let (tx, mut rx) = mpsc::channel::<u32>(10);

// Turn `tx` into type-erased void pointer for FFI.
let raw_data: *mut c_void = CallbackStream::<u32>::prepare(tx);

// Use type-erased pointer to send messages.
unsafe { CallbackStream::<u32>::notify(raw_data, 1); }
unsafe { CallbackStream::<u32>::notify(raw_data, 2); }
unsafe { CallbackStream::<u32>::notify(raw_data, 3); }

// Clean up resources held by pointer.
unsafe { CallbackStream::<u32>::delete(raw_data); }

// Values have been received.
assert_eq!(block_on(rx.recv()), Some(1));
assert_eq!(block_on(rx.recv()), Some(2));
assert_eq!(block_on(rx.recv()), Some(3));
assert_eq!(block_on(rx.recv()), None);

Implementations§

Source§

impl<T> CallbackStream<T>

Source

pub fn prepare(tx: Sender<T>) -> *mut c_void

Prepares sender for later use.

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

Source

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

Uses c_void pointer and sends message.

§Safety

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

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

Source

pub unsafe fn delete(data: *mut c_void)

Unwraps c_void pointer and closes channel.

§Safety

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

Trait Implementations§

Source§

impl<T: Debug + 'static> Debug for CallbackStream<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 CallbackStream<T>

§

impl<T> RefUnwindSafe for CallbackStream<T>
where T: RefUnwindSafe,

§

impl<T> Send for CallbackStream<T>
where T: Send,

§

impl<T> Sync for CallbackStream<T>
where T: Sync,

§

impl<T> Unpin for CallbackStream<T>
where T: Unpin,

§

impl<T> UnwindSafe for CallbackStream<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.