Skip to main content

Computed

Struct Computed 

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

A lazily-evaluated, memoized value derived from one or more Observable dependencies.

Cloning a Computed creates a new handle to the same inner state.

§Invariants

  1. dirty is true after any dependency changes and before get().
  2. version increments by 1 on each recomputation.
  3. The compute function is called only when dirty is true and get() is called.

Implementations§

Source§

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

Source

pub fn from_observable<S: Clone + PartialEq + 'static>( source: &Observable<S>, map: impl Fn(&S) -> T + 'static, ) -> Self

Create a computed value derived from a single observable.

The map function receives a reference to the source value and returns the derived value.

Source

pub fn from2<S1, S2>( s1: &Observable<S1>, s2: &Observable<S2>, map: impl Fn(&S1, &S2) -> T + 'static, ) -> Self
where S1: Clone + PartialEq + 'static, S2: Clone + PartialEq + 'static,

Create a computed value derived from two observables.

Source

pub fn from3<S1, S2, S3>( s1: &Observable<S1>, s2: &Observable<S2>, s3: &Observable<S3>, map: impl Fn(&S1, &S2, &S3) -> T + 'static, ) -> Self
where S1: Clone + PartialEq + 'static, S2: Clone + PartialEq + 'static, S3: Clone + PartialEq + 'static,

Create a computed value derived from three observables.

Source

pub fn from_fn( compute: impl Fn() -> T + 'static, subscriptions: Vec<Subscription>, ) -> Self

Create a computed value from a standalone compute function and pre-built subscriptions.

This is the low-level constructor for advanced use cases where the caller manages dependency subscriptions manually.

Source

pub fn get(&self) -> T

Get the current value, recomputing if any dependency has changed.

Returns a clone of the cached value. If the value is dirty, the compute function is called first and the result is cached.

Source

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

Access the current value by reference without cloning.

Forces recomputation if dirty. The closure receives an immutable reference to the cached value.

§Panics

Panics if the closure attempts to call get() on the same Computed (re-entrant borrow).

Source

pub fn is_dirty(&self) -> bool

Whether the cached value is stale.

Source

pub fn invalidate(&self)

Force invalidation of the cached value. The next get() will recompute.

Source

pub fn version(&self) -> u64

Current version number. Increments by 1 on each recomputation.

Trait Implementations§

Source§

impl<T> Clone for Computed<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Computed<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Computed<T>

§

impl<T> !RefUnwindSafe for Computed<T>

§

impl<T> !Send for Computed<T>

§

impl<T> !Sync for Computed<T>

§

impl<T> Unpin for Computed<T>

§

impl<T> UnsafeUnpin for Computed<T>

§

impl<T> !UnwindSafe for Computed<T>

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 = 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.
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