Struct floresta_common::spsc::Channel
source · pub struct Channel<T> { /* private fields */ }
Expand description
A (Send + Sync) single producer, single consumer channel to notify modules about things. The api is super minimalistic to reduce external dependecies, including from the std-lib
One notable difference from the standard mspc channel is that this channel’s ends are’t two different types, while this is possible, there’s no reason to do that. Specially considering that to get a good compile-time asurance that both ends will not be shared, the channel must not be Send, but this is one of the main requirements to use this channel in async code. Moreover, if two worker threads are meant to be identical threads balancing their work, it might be beneficial to use this same channel as a de-facto single producer, multiple consumer channel for work distribution.
Example
use floresta_common::spsc;
let channel = spsc::Channel::new();
// Send something
channel.send(1);
// Read the same thing back
assert_eq!(channel.recv().next(), Some(1));
Implementations§
source§impl<T> Channel<T>
impl<T> Channel<T>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new channel
Example
use floresta_common::spsc;
let channel = spsc::Channel::new();
channel.send(1);
assert_eq!(channel.recv().next(), Some(1));