Struct ductile::ChannelReceiver
source · pub struct ChannelReceiver<T> { /* private fields */ }
Expand description
The channel part that receives data. It is generic over the type of messages sent in the channel. The type of the messages must match between sender and receiver.
This type is not Clone since the channel is multiple producers, single consumer, just like
std::sync::mpsc
.
Implementations§
source§impl<T> ChannelReceiver<T>where
T: 'static + DeserializeOwned,
impl<T> ChannelReceiver<T>where
T: 'static + DeserializeOwned,
sourcepub fn recv(&self) -> Result<T>
pub fn recv(&self) -> Result<T>
Receive a message from the channel. This method will block until the other end sends a message.
If the other end used send_raw
this method panics since the channel corrupts.
let (sender, receiver) = new_local_channel();
sender.send(42);
let num: i32 = receiver.recv().unwrap();
assert_eq!(num, 42);
sourcepub fn recv_raw(&self) -> Result<Vec<u8>>
pub fn recv_raw(&self) -> Result<Vec<u8>>
Receive some raw data from the channel. This method will block until the other end sends some data.
If the other end used send
this method panics since the channel corrupts.
let (sender, receiver) = new_local_channel::<()>();
sender.send_raw(&vec![1, 2, 3]);
let data: Vec<u8> = receiver.recv_raw().unwrap();
assert_eq!(data, vec![1, 2, 3]);
sourcepub fn change_type<T2>(self) -> ChannelReceiver<T2>
pub fn change_type<T2>(self) -> ChannelReceiver<T2>
Given this is a remote channel, change the type of the message. Will panic if this is a
ChannelReceiver::Local
.
This function is useful for implementing a protocol where the message types change during the execution, for example because initially there is an handshake message, followed by the actual protocol messages.
let receiver: ChannelReceiver<i32> = receiver.change_type();
let receiver: ChannelReceiver<String> = receiver.change_type();