Skip to main content

Signal

Struct Signal 

Source
pub struct Signal<T> { /* private fields */ }
Expand description

Mutable source node in the reactive graph.

Reading a signal from inside an observer (an effect, thunk, or memo computation) records a dependency; writing to it marks those observers stale — thunks and memos recompute on their next read, and effects are queued for the next microtask flush. Clones share the same underlying node, so a signal can be captured by any number of closures. The node lives as long as any clone does.

Writes through set are suppressed when the new value equals the old one, which is what allows convergent feedback (e.g. an effect clamping a value it reads) to settle. replace and update always mark dependents stale.

Implementations§

Source§

impl<T: 'static> Signal<T>

Source

pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R

Runs f with a shared reference to the current value, recording a dependency for the currently running observer.

§Panics

A shared borrow is held while f runs: writing this same signal (set, replace, or update) from inside f panics with a borrow error.

Source

pub fn with_peek<R>(&self, f: impl FnOnce(&T) -> R) -> R

Runs f with a shared reference to the current value without recording a dependency.

§Panics

A shared borrow is held while f runs, exactly as in with: writing this same signal (set, replace, or update) from inside f panics with a borrow error.

Source

pub fn reactor(&self) -> Reactor

Returns the reactor this signal’s node belongs to.

Source

pub fn id(&self) -> NodeId

Returns this signal’s node id, for use with the graph queries on Reactor — observer_count, dependencies_of, node_origin and friends. Ids are unique within one reactor, so aggregate on (Reactor::id, id).

Source

pub fn replace(&self, value: T) -> T

Replaces the current value and marks dependents stale, even when the new value equals the old one (compare set).

§Panics

The value is swapped through a mutable borrow, so writing this same signal from inside a closure that already holds one — with, with_peek or update on this signal — panics with a borrow error. The panic is std’s bare RefCell already borrowed, but #[track_caller] attributes it to this call site, so the offending write is named by line in every build.

Source

pub fn update<R>(&self, f: impl FnOnce(&mut T) -> R) -> R

Mutates the current value in place and marks dependents stale, regardless of whether f actually changed anything (compare set).

§Panics

f runs while the value is mutably borrowed: any read or write of the same signal from inside f (get, with, peek, set, replace, or a nested update) panics with a borrow error.

Source§

impl<T: Clone + 'static> Signal<T>

Source

pub fn get(&self) -> T

Clones and returns the current value.

Source

pub fn peek(&self) -> T

Clones and returns the current value without recording a dependency.

Source§

impl<T: PartialEq + 'static> Signal<T>

Source

pub fn set(&self, value: T) -> Option<T>

Sets the signal to value, suppressing unchanged writes.

Returns the previous value if the signal changed, or None when the new value was equal to the old one.

The equality check runs untracked, so a PartialEq implementation that reads reactive state records no dependencies for the currently running observer.

§Panics

A write borrows the value twice over: the equality check takes a shared borrow, and a write that survives it takes a mutable one. So writing this same signal from inside a closure that already holds a borrow — with, with_peek or update on this signal — panics with a borrow error, as does a PartialEq implementation that writes the very signal it was asked to compare. The panic is std’s bare RefCell already borrowed, but #[track_caller] attributes it to this call site, so the offending write is named by line in every build.

Trait Implementations§

Source§

impl<T> Clone for Signal<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Signal<T>

Source§

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

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

impl<T: 'static> Observable for Signal<T>

Source§

type Item = T

The type of value this observable holds.
Source§

fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R

Runs f with a shared reference to the current value, recording a dependency for the currently running observer.
Source§

fn with_peek<R>(&self, f: impl FnOnce(&T) -> R) -> R

Runs f with a shared reference to the current value without recording a dependency.
Source§

fn reactor(&self) -> Option<Reactor>

Returns the reactor this observable’s node belongs to, when it has one. Read more
Source§

fn get(&self) -> Self::Item
where Self::Item: Clone,

Clones and returns the current value, recording a dependency.
Source§

fn peek(&self) -> Self::Item
where Self::Item: Clone,

Clones and returns the current value without recording a dependency.
Source§

fn map<U, F>(&self, f: F) -> Memo<U>
where Self: Clone + Sized + 'static, U: PartialEq + 'static, F: Fn(&Self::Item) -> U + 'static,

Derives a memo from this observable, cloning the receiver’s handle internally. Read more
Source§

fn into_dyn(self) -> DynObservable<Self::Item>
where Self: Sized + 'static,

Erases this observable’s concrete type behind a cheaply-cloneable handle.
Source§

impl<T: PartialEq + 'static> WritableObservable for Signal<T>

Source§

fn set(&self, value: T)

Assigns a new value.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Signal<T>

§

impl<T> !Send for Signal<T>

§

impl<T> !Sync for Signal<T>

§

impl<T> !UnwindSafe for Signal<T>

§

impl<T> Freeze for Signal<T>
where Rc<SignalInner<T>>: Freeze,

§

impl<T> Unpin for Signal<T>
where Rc<SignalInner<T>>: Unpin,

§

impl<T> UnsafeUnpin for Signal<T>
where Rc<SignalInner<T>>: UnsafeUnpin,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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