Struct embassy_sync::channel::Channel
source · [−]Expand description
A bounded channel for communicating between asynchronous tasks with backpressure.
The channel will buffer up to the provided number of messages. Once the
buffer is full, attempts to send
new messages will wait until a message is
received from the channel.
All data sent will become available in the same order as it was sent.
Implementations
sourceimpl<M, T, const N: usize> Channel<M, T, N>where
M: RawMutex,
impl<M, T, const N: usize> Channel<M, T, N>where
M: RawMutex,
sourcepub const fn new() -> Self
pub const fn new() -> Self
Establish a new bounded channel. For example, to create one with a NoopMutex:
use embassy_sync::channel::Channel;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
// Declare a bounded channel of 3 u32s.
let mut channel = Channel::<NoopRawMutex, u32, 3>::new();
sourcepub fn send(&self, message: T) -> SendFuture<'_, M, T, N>ⓘNotable traits for SendFuture<'ch, M, T, N>impl<'ch, M, T, const N: usize> Future for SendFuture<'ch, M, T, N>where
M: RawMutex, type Output = ();
pub fn send(&self, message: T) -> SendFuture<'_, M, T, N>ⓘNotable traits for SendFuture<'ch, M, T, N>impl<'ch, M, T, const N: usize> Future for SendFuture<'ch, M, T, N>where
M: RawMutex, type Output = ();
M: RawMutex, type Output = ();
Send a value, waiting until there is capacity.
Sending completes when the value has been pushed to the channel’s queue. This doesn’t mean the value has been received yet.
sourcepub fn try_send(&self, message: T) -> Result<(), TrySendError<T>>
pub fn try_send(&self, message: T) -> Result<(), TrySendError<T>>
Attempt to immediately send a message.
This method differs from send
by returning immediately if the channel’s
buffer is full, instead of waiting.
Errors
If the channel capacity has been reached, i.e., the channel has n
buffered values where n
is the argument passed to Channel
, then an
error is returned.
sourcepub fn recv(&self) -> RecvFuture<'_, M, T, N>ⓘNotable traits for RecvFuture<'ch, M, T, N>impl<'ch, M, T, const N: usize> Future for RecvFuture<'ch, M, T, N>where
M: RawMutex, type Output = T;
pub fn recv(&self) -> RecvFuture<'_, M, T, N>ⓘNotable traits for RecvFuture<'ch, M, T, N>impl<'ch, M, T, const N: usize> Future for RecvFuture<'ch, M, T, N>where
M: RawMutex, type Output = T;
M: RawMutex, type Output = T;
Receive the next value.
If there are no messages in the channel’s buffer, this method will wait until a message is sent.
sourcepub fn try_recv(&self) -> Result<T, TryRecvError>
pub fn try_recv(&self) -> Result<T, TryRecvError>
Attempt to immediately receive a message.
This method will either receive a message from the channel immediately or return an error if the channel is empty.