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>

source

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));
source

pub fn send(&self, data: T)

Sends some data through a channel

Example
use floresta_common::spsc;
let channel = spsc::Channel::new();

channel.send(1);
assert_eq!(channel.recv().next(), Some(1));
source

pub fn recv(&self) -> RecvIter<T>

Reads from a channel

This method returns an iterator over all alements inside a Channel

Trait Implementations§

source§

impl<T: Debug> Debug for Channel<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default> Default for Channel<T>

source§

fn default() -> Channel<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Channel<T>

§

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

§

impl<T> Sync for Channel<T>where T: Send,

§

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

§

impl<T> UnwindSafe for Channel<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.