Struct embassy_sync::priority_channel::PriorityChannel

source ·
pub struct PriorityChannel<M, T, K, const N: usize>
where T: Ord, K: Kind, M: RawMutex,
{ /* 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 priority within the channel. For example, in a Max PriorityChannel containing u32’s, data sent in the following order [1, 2, 3] will be received as [3, 2, 1].

Implementations§

source§

impl<M, T, K, const N: usize> PriorityChannel<M, T, K, N>
where T: Ord, K: Kind, M: RawMutex,

source

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

pub fn poll_receive(&self, cx: &mut Context<'_>) -> Poll<T>

Poll the channel for the next message

source

pub fn poll_ready_to_receive(&self, cx: &mut Context<'_>) -> Poll<()>

Allows a poll_fn to poll until the channel is ready to receive

source

pub fn poll_ready_to_send(&self, cx: &mut Context<'_>) -> Poll<()>

Allows a poll_fn to poll until the channel is ready to send

source

pub fn sender(&self) -> Sender<'_, M, T, K, N>

Get a sender for this channel.

source

pub fn receiver(&self) -> Receiver<'_, M, T, K, N>

Get a receiver for this channel.

source

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.

source

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.

source

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.

source

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.

source

pub const fn capacity(&self) -> usize

Returns the maximum number of elements the channel can hold.

source

pub fn free_capacity(&self) -> usize

Returns the free capacity of the channel.

This is equivalent to capacity() - len()

source

pub fn clear(&self)

Clears all elements in the channel.

source

pub fn len(&self) -> usize

Returns the number of elements currently in the channel.

source

pub fn is_empty(&self) -> bool

Returns whether the channel is empty.

source

pub fn is_full(&self) -> bool

Returns whether the channel is full.

Auto Trait Implementations§

§

impl<M, T, K, const N: usize> !Freeze for PriorityChannel<M, T, K, N>

§

impl<M, T, K, const N: usize> !RefUnwindSafe for PriorityChannel<M, T, K, N>

§

impl<M, T, K, const N: usize> Send for PriorityChannel<M, T, K, N>
where M: Send, K: Send, T: Send,

§

impl<M, T, K, const N: usize> Sync for PriorityChannel<M, T, K, N>
where M: Sync, K: Send, T: Send,

§

impl<M, T, K, const N: usize> Unpin for PriorityChannel<M, T, K, N>
where M: Unpin, K: Unpin, T: Unpin,

§

impl<M, T, K, const N: usize> UnwindSafe for PriorityChannel<M, T, K, N>
where M: UnwindSafe, K: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.