Struct embassy_sync::priority_channel::PriorityChannel
source · pub struct PriorityChannel<M, T, K, const N: usize>{ /* private fields */ }
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.
Sent data may be reordered based on their priorty within the channel.
For example, in a Max
PriorityChannel
containing u32
’s, data sent in the following order [1, 2, 3]
will be recieved as [3, 2, 1]
.
Implementations§
source§impl<M, T, K, const N: usize> PriorityChannel<M, T, K, N>
impl<M, T, K, const N: usize> PriorityChannel<M, T, K, N>
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::priority_channel::{PriorityChannel, Max};
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
// Declare a bounded channel of 3 u32s.
let mut channel = PriorityChannel::<NoopRawMutex, u32, Max, 3>::new();
sourcepub fn poll_receive(&self, cx: &mut Context<'_>) -> Poll<T>
pub fn poll_receive(&self, cx: &mut Context<'_>) -> Poll<T>
Poll the channel for the next message
sourcepub fn poll_ready_to_receive(&self, cx: &mut Context<'_>) -> Poll<()>
pub fn poll_ready_to_receive(&self, cx: &mut Context<'_>) -> Poll<()>
Allows a poll_fn to poll until the channel is ready to receive
sourcepub fn poll_ready_to_send(&self, cx: &mut Context<'_>) -> Poll<()>
pub fn poll_ready_to_send(&self, cx: &mut Context<'_>) -> Poll<()>
Allows a poll_fn to poll until the channel is ready to send
sourcepub fn send(&self, message: T) -> SendFuture<'_, M, T, K, N> ⓘ
pub fn send(&self, message: T) -> SendFuture<'_, M, T, K, N> ⓘ
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 PriorityChannel
, then an
error is returned.
sourcepub fn receive(&self) -> ReceiveFuture<'_, M, T, K, N> ⓘ
pub fn receive(&self) -> ReceiveFuture<'_, M, T, K, N> ⓘ
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_receive(&self) -> Result<T, TryReceiveError>
pub fn try_receive(&self) -> Result<T, TryReceiveError>
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.