Struct ductile::ChannelSender
source · pub struct ChannelSender<T> { /* private fields */ }
Expand description
The channel part that sends data. It is generic over the type of messages sent and abstracts the underlying type of connection. The connection type can be local in memory, or remote using TCP sockets or local with Unix sockets.
This sender is Clone since the channel is multiple producers, single consumer, just like
std::sync::mpsc
.
Implementations§
source§impl<T> ChannelSender<T>where
T: 'static + Send + Sync + Serialize,
impl<T> ChannelSender<T>where
T: 'static + Send + Sync + Serialize,
sourcepub fn send(&self, data: T) -> Result<()>
pub fn send(&self, data: T) -> Result<()>
Serialize and send a message in the channel. The message is not actually serialized for
local channels, but the type must be Send + Sync
.
For remote channel the message is serialized, if you want to send raw data (i.e. [u8]
)
that not need to be serialized consider using send_raw
since its performance is way
better.
This method is guaranteed to fail if the receiver is dropped only for local channels. Using remote channels drops this requirement.
let (sender, receiver) = new_local_channel();
sender.send(42u8).unwrap();
drop(receiver);
assert!(sender.send(69u8).is_err());
sourcepub fn send_raw(&self, data: &[u8]) -> Result<()>
pub fn send_raw(&self, data: &[u8]) -> Result<()>
Send some raw data in the channel without serializing it. This is possible only for raw
unstructured data ([u8]
), but this methods is much faster than send
since it avoids
serializing the message.
This method is guaranteed to fail if the receiver is dropped only for local channels. Using remote channels drops this requirement.
let (sender, receiver) = new_local_channel::<()>();
sender.send_raw(&vec![1, 2, 3, 4]).unwrap();
drop(receiver);
assert!(sender.send_raw(&vec![1, 2]).is_err());
sourcepub fn change_type<T2>(self) -> ChannelSender<T2>
pub fn change_type<T2>(self) -> ChannelSender<T2>
Given this is a remote channel, change the type of the message. Will panic if this is a local channel.
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 sender: ChannelSender<i32> = sender.change_type();
let sender: ChannelSender<String> = sender.change_type();
Trait Implementations§
source§impl<T: Clone> Clone for ChannelSender<T>
impl<T: Clone> Clone for ChannelSender<T>
source§fn clone(&self) -> ChannelSender<T>
fn clone(&self) -> ChannelSender<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more