Skip to main content

Reactive

Struct Reactive 

Source
pub struct Reactive<T> { /* private fields */ }
Available on crate feature std only.
Expand description

A ReactiveRepr value with lock-free reads and a race-free typed change signal.

Reactive::get is a plain atomic load. Reactive::watch hands out a Changed whose Changed::changed awaits the next change and returns the new value. tokio’s watch handles the wakeup race, versioning, and multiple independent subscribers.

The value is held in the atomic (for lock-free reads) and carried on the watch (so changed() can return it without a separate read). set stores the atomic and then sends, which is a no-op when nobody is subscribed, so an idle value costs nothing beyond the atomic store.

Implementations§

Source§

impl<T: ReactiveRepr> Reactive<T>

Source

pub fn new(value: T) -> Self

Create a new Reactive holding value.

Source

pub fn get(&self) -> T

Read the current value (lock-free).

Source

pub fn set(&self, value: T)

Store value and wake watchers.

Source

pub fn watch(&self) -> Changed<T>

Subscribe to changes. Hold the returned Changed and loop Changed::changed to observe every change, its watch cursor is persistent, so no edge is missed (unlike re-subscribing per await).

Trait Implementations§

Source§

impl<T: Debug> Debug for Reactive<T>

Source§

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

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

impl<T: ReactiveRepr + Default> Default for Reactive<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> !Freeze for Reactive<T>

§

impl<T> RefUnwindSafe for Reactive<T>
where PhantomData<fn() -> T>: RefUnwindSafe,

§

impl<T> Send for Reactive<T>
where PhantomData<fn() -> T>: Send,

§

impl<T> Sync for Reactive<T>
where PhantomData<fn() -> T>: Sync,

§

impl<T> Unpin for Reactive<T>
where PhantomData<fn() -> T>: Unpin,

§

impl<T> UnsafeUnpin for Reactive<T>
where PhantomData<fn() -> T>: UnsafeUnpin,

§

impl<T> UnwindSafe for Reactive<T>
where PhantomData<fn() -> 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, !>

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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more