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:
CallbackStream::prepare()to wrap the stream sender and get thec_voidpointerCallbackStream::notify()to use thec_voidpointer and send messages to the streamCallbackStream::delete()to unwrap thec_voidpointer and close the underlying stream
§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§
Trait Implementations§
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> 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