pub struct ArcMemo<T, S = SyncStorage>where
S: Storage<T>,{ /* private fields */ }
Expand description
An efficient derived reactive value based on other reactive values.
This is a reference-counted memo, which is Clone
but not Copy
.
For arena-allocated Copy
memos, use Memo
.
Unlike a “derived signal,” a memo comes with two guarantees:
- The memo will only run once per change, no matter how many times you access its value.
- The memo will only notify its dependents if the value of the computation changes.
This makes a memo the perfect tool for expensive computations.
Memos have a certain overhead compared to derived signals. In most cases, you should create a derived signal. But if the derivation calculation is expensive, you should create a memo.
As with an Effect
, the argument to the memo function is the previous value,
i.e., the current value of the memo, which will be None
for the initial calculation.
§Examples
let (value, set_value) = signal(0);
// 🆗 we could create a derived signal with a simple function
let double_value = move || value.get() * 2;
set_value.set(2);
assert_eq!(double_value(), 4);
// but imagine the computation is really expensive
let expensive = move || really_expensive_computation(value.get()); // lazy: doesn't run until called
// 🆗 run #1: calls `really_expensive_computation` the first time
println!("expensive = {}", expensive());
// ❌ run #2: this calls `really_expensive_computation` a second time!
let some_value = expensive();
// instead, we create a memo
// 🆗 run #1: the calculation runs once immediately
let memoized = ArcMemo::new(move |_| really_expensive_computation(value.get()));
// 🆗 reads the current value of the memo
// can be `memoized()` on nightly
println!("memoized = {}", memoized.get());
// ✅ reads the current value **without re-running the calculation**
let some_value = memoized.get();
§Core Trait Implementations
.get()
clones the current value of the memo. If you call it within an effect, it will cause that effect to subscribe to the memo, and to re-run whenever the value of the memo changes..get_untracked()
clones the value of the memo without reactively tracking it.
.read()
returns a guard that allows accessing the value of the memo by reference. If you call it within an effect, it will cause that effect to subscribe to the memo, and to re-run whenever the value of the memo changes..read_untracked()
gives access to the current value of the memo without reactively tracking it.
.with()
allows you to reactively access the memo’s value without cloning by applying a callback function..with_untracked()
allows you to access the memo’s value by applying a callback function without reactively tracking it.
.to_stream()
converts the memo to anasync
stream of values.::from_stream()
converts anasync
stream of values into a memo containing the latest value.
Implementations§
Source§impl<T: 'static> ArcMemo<T, SyncStorage>where
SyncStorage: Storage<T>,
impl<T: 'static> ArcMemo<T, SyncStorage>where
SyncStorage: Storage<T>,
Sourcepub fn new(fun: impl Fn(Option<&T>) -> T + Send + Sync + 'static) -> Selfwhere
T: PartialEq,
pub fn new(fun: impl Fn(Option<&T>) -> T + Send + Sync + 'static) -> Selfwhere
T: PartialEq,
Creates a new memo by passing a function that computes the value.
This is lazy: the function will not be called until the memo’s value is read for the first time.
Sourcepub fn new_with_compare(
fun: impl Fn(Option<&T>) -> T + Send + Sync + 'static,
changed: fn(Option<&T>, Option<&T>) -> bool,
) -> Self
pub fn new_with_compare( fun: impl Fn(Option<&T>) -> T + Send + Sync + 'static, changed: fn(Option<&T>, Option<&T>) -> bool, ) -> Self
Creates a new memo by passing a function that computes the value, and a comparison function
that takes the previous value and the new value and returns true
if the value has
changed.
This is lazy: the function will not be called until the memo’s value is read for the first time.
Sourcepub fn new_owning(
fun: impl Fn(Option<T>) -> (T, bool) + Send + Sync + 'static,
) -> Self
pub fn new_owning( fun: impl Fn(Option<T>) -> (T, bool) + Send + Sync + 'static, ) -> Self
Creates a new memo by passing a function that computes the value.
Unlike ArcMemo::new
, this receives ownership of the previous value. As a result, it
must return both the new value and a bool
that is true
if the value has changed.
This is lazy: the function will not be called until the memo’s value is read for the first time.
Trait Implementations§
Source§impl<T, S> DefinedAt for ArcMemo<T, S>where
S: Storage<T>,
impl<T, S> DefinedAt for ArcMemo<T, S>where
S: Storage<T>,
Source§fn defined_at(&self) -> Option<&'static Location<'static>>
fn defined_at(&self) -> Option<&'static Location<'static>>
None
in
release mode.Source§impl<T> From<ArcMemo<T>> for MaybeSignal<T>
impl<T> From<ArcMemo<T>> for MaybeSignal<T>
Source§impl<T> From<ArcMemo<T>> for Memo<T>
impl<T> From<ArcMemo<T>> for Memo<T>
Source§fn from(value: ArcMemo<T, SyncStorage>) -> Self
fn from(value: ArcMemo<T, SyncStorage>) -> Self
Source§impl<T> From<ArcMemo<T, LocalStorage>> for Signal<T, LocalStorage>
impl<T> From<ArcMemo<T, LocalStorage>> for Signal<T, LocalStorage>
Source§fn from(value: ArcMemo<T, LocalStorage>) -> Self
fn from(value: ArcMemo<T, LocalStorage>) -> Self
Source§impl<T> From<ArcReadSignal<T>> for ArcMemo<T, SyncStorage>
impl<T> From<ArcReadSignal<T>> for ArcMemo<T, SyncStorage>
Source§fn from(value: ArcReadSignal<T>) -> Self
fn from(value: ArcReadSignal<T>) -> Self
Source§impl<T> From<ArcRwSignal<T>> for ArcMemo<T, SyncStorage>
impl<T> From<ArcRwSignal<T>> for ArcMemo<T, SyncStorage>
Source§fn from(value: ArcRwSignal<T>) -> Self
fn from(value: ArcRwSignal<T>) -> Self
Source§impl<T> FromLocal<ArcMemo<T, LocalStorage>> for MaybeSignal<T, LocalStorage>
impl<T> FromLocal<ArcMemo<T, LocalStorage>> for MaybeSignal<T, LocalStorage>
Source§fn from_local(value: ArcMemo<T, LocalStorage>) -> Self
fn from_local(value: ArcMemo<T, LocalStorage>) -> Self
Source§impl<T> FromLocal<ArcMemo<T, LocalStorage>> for Memo<T, LocalStorage>where
T: 'static,
impl<T> FromLocal<ArcMemo<T, LocalStorage>> for Memo<T, LocalStorage>where
T: 'static,
Source§fn from_local(value: ArcMemo<T, LocalStorage>) -> Self
fn from_local(value: ArcMemo<T, LocalStorage>) -> Self
Source§impl<T: 'static, S> IsDisposed for ArcMemo<T, S>where
S: Storage<T>,
impl<T: 'static, S> IsDisposed for ArcMemo<T, S>where
S: Storage<T>,
Source§fn is_disposed(&self) -> bool
fn is_disposed(&self) -> bool
true
, the signal cannot be accessed without a panic.Source§impl<T: 'static, S> ReactiveNode for ArcMemo<T, S>where
S: Storage<T>,
impl<T: 'static, S> ReactiveNode for ArcMemo<T, S>where
S: Storage<T>,
Source§fn mark_dirty(&self)
fn mark_dirty(&self)
Source§fn mark_check(&self)
fn mark_check(&self)
Source§fn mark_subscribers_check(&self)
fn mark_subscribers_check(&self)
Source§fn update_if_necessary(&self) -> bool
fn update_if_necessary(&self) -> bool
Source§impl<T: 'static, S> ReadUntracked for ArcMemo<T, S>where
S: Storage<T>,
impl<T: 'static, S> ReadUntracked for ArcMemo<T, S>where
S: Storage<T>,
Source§type Value = ReadGuard<T, Mapped<Plain<Option<<S as Storage<T>>::Wrapped>>, T>>
type Value = ReadGuard<T, Mapped<Plain<Option<<S as Storage<T>>::Wrapped>>, T>>
Source§fn try_read_untracked(&self) -> Option<Self::Value>
fn try_read_untracked(&self) -> Option<Self::Value>
None
if the signal has already been disposed.Source§fn read_untracked(&self) -> Self::Value
fn read_untracked(&self) -> Self::Value
Source§fn custom_try_read(&self) -> Option<Option<Self::Value>>
fn custom_try_read(&self) -> Option<Option<Self::Value>>
Read::try_read
implementation despite it being auto implemented. Read moreSource§impl<T: 'static, S> Source for ArcMemo<T, S>where
S: Storage<T>,
impl<T: 'static, S> Source for ArcMemo<T, S>where
S: Storage<T>,
Source§fn add_subscriber(&self, subscriber: AnySubscriber)
fn add_subscriber(&self, subscriber: AnySubscriber)
Source§fn remove_subscriber(&self, subscriber: &AnySubscriber)
fn remove_subscriber(&self, subscriber: &AnySubscriber)
Source§fn clear_subscribers(&self)
fn clear_subscribers(&self)
Source§impl<T: 'static, S> Subscriber for ArcMemo<T, S>where
S: Storage<T>,
impl<T: 'static, S> Subscriber for ArcMemo<T, S>where
S: Storage<T>,
Source§fn add_source(&self, source: AnySource)
fn add_source(&self, source: AnySource)
Source§fn clear_sources(&self, subscriber: &AnySubscriber)
fn clear_sources(&self, subscriber: &AnySubscriber)
Source§impl<T: 'static, S> ToAnySource for ArcMemo<T, S>where
S: Storage<T>,
impl<T: 'static, S> ToAnySource for ArcMemo<T, S>where
S: Storage<T>,
Source§fn to_any_source(&self) -> AnySource
fn to_any_source(&self) -> AnySource
Source§impl<T: 'static, S> ToAnySubscriber for ArcMemo<T, S>where
S: Storage<T>,
impl<T: 'static, S> ToAnySubscriber for ArcMemo<T, S>where
S: Storage<T>,
Source§fn to_any_subscriber(&self) -> AnySubscriber
fn to_any_subscriber(&self) -> AnySubscriber
impl<T, S> Eq for ArcMemo<T, S>where
S: Storage<T>,
Auto Trait Implementations§
impl<T, S> Freeze for ArcMemo<T, S>
impl<T, S = SyncStorage> !RefUnwindSafe for ArcMemo<T, S>
impl<T, S> Send for ArcMemo<T, S>
impl<T, S> Sync for ArcMemo<T, S>
impl<T, S> Unpin for ArcMemo<T, S>
impl<T, S = SyncStorage> !UnwindSafe for ArcMemo<T, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<S, T> FromStream<T> for S
impl<S, T> FromStream<T> for S
Source§fn from_stream(stream: impl Stream<Item = T> + Send + 'static) -> S
fn from_stream(stream: impl Stream<Item = T> + Send + 'static) -> S
Source§fn from_stream_unsync(stream: impl Stream<Item = T> + 'static) -> S
fn from_stream_unsync(stream: impl Stream<Item = T> + 'static) -> S
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Read for Twhere
T: Track + ReadUntracked,
impl<T> Read for Twhere
T: Track + ReadUntracked,
Source§impl<T> StorageAccess<T> for T
impl<T> StorageAccess<T> for T
Source§fn as_borrowed(&self) -> &T
fn as_borrowed(&self) -> &T
Source§fn into_taken(self) -> T
fn into_taken(self) -> T
Source§impl<T> With for Twhere
T: Read,
impl<T> With for Twhere
T: Read,
Source§type Value = <<T as Read>::Value as Deref>::Target
type Value = <<T as Read>::Value as Deref>::Target
Source§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
Source§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
Source§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Source§impl<T> WithUntracked for Twhere
T: DefinedAt + ReadUntracked,
impl<T> WithUntracked for Twhere
T: DefinedAt + ReadUntracked,
Source§type Value = <<T as ReadUntracked>::Value as Deref>::Target
type Value = <<T as ReadUntracked>::Value as Deref>::Target
Source§fn try_with_untracked<U>(
&self,
fun: impl FnOnce(&<T as WithUntracked>::Value) -> U,
) -> Option<U>
fn try_with_untracked<U>( &self, fun: impl FnOnce(&<T as WithUntracked>::Value) -> U, ) -> Option<U>
None
if the signal has already been disposed.