Watch

Struct Watch 

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

The Watch is a single-slot signaling primitive that allows multiple (N) receivers to get changes to the value. Unlike a Signal, Watch supports multiple receivers, and unlike a PubSubChannel, Watch immediately overwrites the previous value when a new one is sent, without waiting for all receivers to read the previous value.

This makes Watch particularly useful when a single task updates a value or “state”, and multiple other tasks need to be notified about changes to this value asynchronously. Receivers may “lose” stale values, as they are always provided with the latest value.


use embedded_sync::watch::Watch;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;

static WATCH: Watch<CriticalSectionRawMutex, u8> = Watch::new();

// Obtain receivers and sender
let mut rcv0 = WATCH.receiver();
let mut rcv1 = WATCH.receiver();
let mut snd = WATCH.sender();

snd.send(10);

// Receive the new value (async or try)
assert_eq!(rcv0.try_changed(), Some(10));
assert_eq!(rcv1.try_changed(), Some(10));

// No update
assert_eq!(rcv0.try_changed(), None);
assert_eq!(rcv1.try_changed(), None);

snd.send(20);

// Using `get` marks the value as seen
assert_eq!(rcv1.try_get(), Some(20));
assert_eq!(rcv1.try_changed(), None);

snd.send(20);

assert_eq!(rcv1.try_get(), Some(20));
assert_eq!(rcv1.try_get(), Some(20));

Implementations§

Source§

impl<M: RawMutex, T: Clone> Watch<M, T>

Source

pub const fn new() -> Self

Create a new Watch channel for N receivers.

Source

pub const fn new_with(data: T) -> Self

Create a new Watch channel with default data.

Source

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

Create a new Sender for the Watch.

Source

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

Try to create a new Receiver for the Watch. If the maximum number of receivers has been reached, None is returned.

Source

pub fn get_msg_id(&self) -> u8

Returns the message ID of the latest message sent to the Watch.

This counter is monotonic, and is incremented every time a new message is sent.

Source

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

Tries to get the value of the Watch.

Trait Implementations§

Source§

impl<M: Debug + RawMutex, T: Debug + Clone> Debug for Watch<M, T>

Source§

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

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

impl<M: RawMutex, T: Clone> WatchBehavior<T> for Watch<M, T>

Source§

fn try_get(&self, id: Option<&mut u8>) -> Option<T>

Tries to get the value of the Watch, marking it as seen, if an id is given.
Source§

fn contains_value(&self) -> bool

Checks if the Watch is been initialized with a value.

Auto Trait Implementations§

§

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

§

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

§

impl<M, T> Send for Watch<M, T>
where M: Send, T: Send,

§

impl<M, T> Sync for Watch<M, T>
where M: Sync, T: Send,

§

impl<M, T> Unpin for Watch<M, T>
where M: Unpin, T: Unpin,

§

impl<M, T> UnwindSafe for Watch<M, T>
where M: UnwindSafe, 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 = 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 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.