embassy-sync

Crates

git

Versions

default

Flavors

Skip to main content

Signal

Struct Signal 

Source
pub struct Signal<M, T>
where M: RawMutex,
{ /* private fields */ }
Expand description

Single-slot signaling primitive for a single consumer.

This is similar to a Channel with a buffer size of 1, except “sending” to it (calling Signal::signal) when full will overwrite the previous value instead of waiting for the receiver to pop the previous value.

It is useful for sending data between tasks when the receiver only cares about the latest data, and therefore it’s fine to “lose” messages. This is often the case for “state” updates.

For more advanced use cases, you might want to use Channel instead. For multiple consumers, use Watch instead.

Signals are generally declared as statics and then borrowed as required.

use embassy_sync::signal::Signal;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;

enum SomeCommand {
  On,
  Off,
}

static SOME_SIGNAL: Signal<CriticalSectionRawMutex, SomeCommand> = Signal::new();

Implementations§

Source§

impl<M, T> Signal<M, T>
where M: RawMutex,

Source

pub const fn new() -> Self

Create a new Signal.

Source§

impl<M, T> Signal<M, T>
where M: RawMutex,

Source

pub fn signal(&self, val: T)

Mark this Signal as signaled.

Source

pub fn reset(&self)

Remove the queued value in this Signal, if any.

Source

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

Poll for state changes of this Signal.

Source

pub fn wait(&self) -> impl Future<Output = T> + '_

Future that completes when this Signal has been signaled, taking the value out of the signal.

The returned Future is cancel-safe. No value will be lost even if it isn’t polled to completion.

Source

pub fn try_take(&self) -> Option<T>

non-blocking method to try and take the signal value.

Source

pub fn signaled(&self) -> bool

non-blocking method to check whether this signal has been signaled. This does not clear the signal.

Trait Implementations§

Source§

impl<M, T> Default for Signal<M, T>
where M: RawMutex,

Source§

fn default() -> Self

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

impl<M, T> Sink<T> for Signal<M, T>
where M: RawMutex,

Source§

type Error = !

The type of value produced by the sink when an error occurs.
Source§

fn poll_ready( self: Pin<&mut Self>, _cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>

Attempts to prepare the Sink to receive a value. Read more
Source§

fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error>

Begin the process of sending a value to the sink. Each call to this function must be preceded by a successful call to poll_ready which returned Poll::Ready(Ok(())). Read more
Source§

fn poll_flush( self: Pin<&mut Self>, _cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>

Flush any remaining output from this sink. Read more
Source§

fn poll_close( self: Pin<&mut Self>, _cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>

Flush any remaining output and close this sink, if necessary. Read more
Source§

impl<M, T> Sink<T> for &Signal<M, T>
where M: RawMutex,

Source§

type Error = !

The type of value produced by the sink when an error occurs.
Source§

fn poll_ready( self: Pin<&mut Self>, _cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>

Attempts to prepare the Sink to receive a value. Read more
Source§

fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error>

Begin the process of sending a value to the sink. Each call to this function must be preceded by a successful call to poll_ready which returned Poll::Ready(Ok(())). Read more
Source§

fn poll_flush( self: Pin<&mut Self>, _cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>

Flush any remaining output from this sink. Read more
Source§

fn poll_close( self: Pin<&mut Self>, _cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>

Flush any remaining output and close this sink, if necessary. Read more
Source§

impl<M, T> Stream for Signal<M, T>
where M: RawMutex,

Source§

type Item = T

Values yielded by the stream.
Source§

fn poll_next( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<Self::Item>>

Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more
Source§

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream. Read more
Source§

impl<M, T> Stream for &Signal<M, T>
where M: RawMutex,

Source§

type Item = T

Values yielded by the stream.
Source§

fn poll_next( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<Self::Item>>

Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more
Source§

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream. Read more

Auto Trait Implementations§

§

impl<M, T> !Freeze for Signal<M, T>

§

impl<M, T> !RefUnwindSafe for Signal<M, T>

§

impl<M, T> Send for Signal<M, T>
where Mutex<M, Cell<State<T>>>: Send,

§

impl<M, T> Sync for Signal<M, T>
where Mutex<M, Cell<State<T>>>: Sync,

§

impl<M, T> Unpin for Signal<M, T>
where Mutex<M, Cell<State<T>>>: Unpin,

§

impl<M, T> UnsafeUnpin for Signal<M, T>
where Mutex<M, Cell<State<T>>>: UnsafeUnpin,

§

impl<M, T> UnwindSafe for Signal<M, T>
where Mutex<M, Cell<State<T>>>: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

Source§

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.
Source§

impl<S, T, E> TryStream for S
where S: Stream<Item = Result<T, E>> + ?Sized,

Source§

type Ok = T

The type of successful values yielded by this future
Source§

type Error = E

The type of failures yielded by this future
Source§

fn try_poll_next( self: Pin<&mut S>, cx: &mut Context<'_>, ) -> Poll<Option<Result<<S as TryStream>::Ok, <S as TryStream>::Error>>>

Poll this TryStream as if it were a Stream. Read more