Struct reactive_graph::signal::RwSignal
source · pub struct RwSignal<T, S = SyncStorage> { /* private fields */ }
Expand description
An arena-allocated signal that can be read from or written to.
A signal is a piece of data that may change over time, and notifies other code when it has changed. This is the atomic unit of reactivity, which begins all other processes of reactive updates.
This is an arena-allocated signal, which is Copy
and is disposed when its reactive
Owner
cleans up. For a reference-counted signal that lives
as long as a reference to it is alive, see ArcRwSignal
.
§Core Trait Implementations
§Reading the Value
.get()
clones the current value of the signal. If you call it within an effect, it will cause that effect to subscribe to the signal, and to re-run whenever the value of the signal changes..get_untracked()
clones the value of the signal without reactively tracking it.
.read()
returns a guard that allows accessing the value of the signal by reference. If you call it within an effect, it will cause that effect to subscribe to the signal, and to re-run whenever the value of the signal changes..read_untracked()
gives access to the current value of the signal without reactively tracking it.
.with()
allows you to reactively access the signal’s value without cloning by applying a callback function..with_untracked()
allows you to access the signal’s value by applying a callback function without reactively tracking it.
.to_stream()
converts the signal to anasync
stream of values.
§Updating the Value
.set()
sets the signal to a new value..update()
updates the value of the signal by applying a closure that takes a mutable reference..write()
returns a guard through which the signal can be mutated, and which notifies subscribers when it is dropped.
Each of these has a related
_untracked()
method, which updates the signal without notifying subscribers. Untracked updates are not desirable in most cases, as they cause “tearing” between the signal’s value and its observed value. If you want a non-reactive container, usedStoredValue
instead.
§Examples
let count = ArcRwSignal::new(0);
// ✅ calling the getter clones and returns the value
// this can be `count()` on nightly
assert_eq!(count.get(), 0);
// ✅ calling the setter sets the value
// this can be `set_count(1)` on nightly
count.set(1);
assert_eq!(count.get(), 1);
// ❌ you could call the getter within the setter
// set_count.set(count.get() + 1);
// ✅ however it's more efficient to use .update() and mutate the value in place
count.update(|count: &mut i32| *count += 1);
assert_eq!(count.get(), 2);
// ✅ you can create "derived signals" with a Fn() -> T interface
let double_count = {
// clone before moving into the closure because we use it below
let count = count.clone();
move || count.get() * 2
};
count.set(0);
assert_eq!(double_count(), 0);
count.set(1);
assert_eq!(double_count(), 2);
Implementations§
source§impl<T, S> RwSignal<T, S>where
T: 'static,
S: Storage<ArcRwSignal<T>>,
impl<T, S> RwSignal<T, S>where
T: 'static,
S: Storage<ArcRwSignal<T>>,
sourcepub fn new_with_storage(value: T) -> Self
pub fn new_with_storage(value: T) -> Self
Creates a new signal with the given arena storage method.
source§impl<T> RwSignal<T, LocalStorage>where
T: 'static,
impl<T> RwSignal<T, LocalStorage>where
T: 'static,
sourcepub fn new_local(value: T) -> Self
pub fn new_local(value: T) -> Self
Creates a new signal, taking the initial value as its argument. Unlike RwSignal::new
,
this pins the value to the current thread. Accessing it from any other thread will panic.
source§impl<T, S> RwSignal<T, S>
impl<T, S> RwSignal<T, S>
sourcepub fn read_only(&self) -> ReadSignal<T, S>
pub fn read_only(&self) -> ReadSignal<T, S>
Returns a read-only handle to the signal.
source§impl<T, S> RwSignal<T, S>
impl<T, S> RwSignal<T, S>
sourcepub fn write_only(&self) -> WriteSignal<T, S>
pub fn write_only(&self) -> WriteSignal<T, S>
Returns a write-only handle to the signal.
source§impl<T, S> RwSignal<T, S>where
T: 'static,
S: Storage<ArcRwSignal<T>> + Storage<ArcWriteSignal<T>> + Storage<ArcReadSignal<T>>,
impl<T, S> RwSignal<T, S>where
T: 'static,
S: Storage<ArcRwSignal<T>> + Storage<ArcWriteSignal<T>> + Storage<ArcReadSignal<T>>,
sourcepub fn split(&self) -> (ReadSignal<T, S>, WriteSignal<T, S>)
pub fn split(&self) -> (ReadSignal<T, S>, WriteSignal<T, S>)
Splits the signal into its readable and writable halves.
sourcepub fn unite(read: ReadSignal<T, S>, write: WriteSignal<T, S>) -> Option<Self>
pub fn unite(read: ReadSignal<T, S>, write: WriteSignal<T, S>) -> Option<Self>
Reunites the two halves of a signal. Returns None
if the two signals
provided were not created from the same signal.
Trait Implementations§
source§impl<T, S> DefinedAt for RwSignal<T, S>
impl<T, S> DefinedAt for RwSignal<T, S>
source§fn defined_at(&self) -> Option<&'static Location<'static>>
fn defined_at(&self) -> Option<&'static Location<'static>>
None
in
release mode.source§impl<'de, T, S> Deserialize<'de> for RwSignal<T, S>
impl<'de, T, S> Deserialize<'de> for RwSignal<T, S>
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl<'a, T> From<&'a ArcRwSignal<T>> for RwSignal<T>
impl<'a, T> From<&'a ArcRwSignal<T>> for RwSignal<T>
source§fn from(value: &'a ArcRwSignal<T>) -> Self
fn from(value: &'a ArcRwSignal<T>) -> Self
source§impl<T> From<ArcRwSignal<T>> for RwSignal<T>
impl<T> From<ArcRwSignal<T>> for RwSignal<T>
source§fn from(value: ArcRwSignal<T>) -> Self
fn from(value: ArcRwSignal<T>) -> Self
source§impl<T> From<RwSignal<Option<T>, LocalStorage>> for MaybeProp<T, LocalStorage>
impl<T> From<RwSignal<Option<T>, LocalStorage>> for MaybeProp<T, LocalStorage>
source§impl<T> From<RwSignal<T>> for MaybeSignal<T>
impl<T> From<RwSignal<T>> for MaybeSignal<T>
source§impl<T> From<RwSignal<T, LocalStorage>> for MaybeProp<T, LocalStorage>
impl<T> From<RwSignal<T, LocalStorage>> for MaybeProp<T, LocalStorage>
source§fn from(value: RwSignal<T, LocalStorage>) -> Self
fn from(value: RwSignal<T, LocalStorage>) -> Self
source§impl<T> From<RwSignal<T, LocalStorage>> for MaybeSignal<T, LocalStorage>
impl<T> From<RwSignal<T, LocalStorage>> for MaybeSignal<T, LocalStorage>
source§fn from(value: RwSignal<T, LocalStorage>) -> Self
fn from(value: RwSignal<T, LocalStorage>) -> Self
source§impl<T> From<RwSignal<T, LocalStorage>> for Signal<T, LocalStorage>where
T: 'static,
impl<T> From<RwSignal<T, LocalStorage>> for Signal<T, LocalStorage>where
T: 'static,
source§fn from(value: RwSignal<T, LocalStorage>) -> Self
fn from(value: RwSignal<T, LocalStorage>) -> Self
source§impl<T, S> From<RwSignal<T, S>> for ArcRwSignal<T>where
T: 'static,
S: Storage<ArcRwSignal<T>>,
impl<T, S> From<RwSignal<T, S>> for ArcRwSignal<T>where
T: 'static,
S: Storage<ArcRwSignal<T>>,
source§impl<T, S> From<RwSignal<T, S>> for SignalSetter<T, S>
impl<T, S> From<RwSignal<T, S>> for SignalSetter<T, S>
source§impl<T> FromLocal<ArcRwSignal<T>> for RwSignal<T, LocalStorage>where
T: 'static,
impl<T> FromLocal<ArcRwSignal<T>> for RwSignal<T, LocalStorage>where
T: 'static,
source§fn from_local(value: ArcRwSignal<T>) -> Self
fn from_local(value: ArcRwSignal<T>) -> Self
source§impl<T: 'static, S> IsDisposed for RwSignal<T, S>
impl<T: 'static, S> IsDisposed for RwSignal<T, S>
source§fn is_disposed(&self) -> bool
fn is_disposed(&self) -> bool
true
, the signal cannot be accessed without a panic.source§impl<T, S> PartialEq for RwSignal<T, S>
impl<T, S> PartialEq for RwSignal<T, S>
source§impl<T, S> ReadUntracked for RwSignal<T, S>where
T: 'static,
S: Storage<ArcRwSignal<T>>,
impl<T, S> ReadUntracked for RwSignal<T, S>where
T: 'static,
S: Storage<ArcRwSignal<T>>,
§type Value = ReadGuard<T, Plain<T>>
type Value = ReadGuard<T, Plain<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§impl<T, S> Writeable for RwSignal<T, S>where
T: 'static,
S: Storage<ArcRwSignal<T>>,
impl<T, S> Writeable for RwSignal<T, S>where
T: 'static,
S: Storage<ArcRwSignal<T>>,
source§fn try_write(&self) -> Option<impl UntrackableGuard<Target = Self::Value>>
fn try_write(&self) -> Option<impl UntrackableGuard<Target = Self::Value>>
None
if the signal has already been disposed.source§fn try_write_untracked(&self) -> Option<UntrackedWriteGuard<Self::Value>>
fn try_write_untracked(&self) -> Option<UntrackedWriteGuard<Self::Value>>
None
if the signal has already been disposed.impl<T, S> Copy for RwSignal<T, S>
impl<T, S> Eq for RwSignal<T, S>
Auto Trait Implementations§
impl<T, S> Freeze for RwSignal<T, S>
impl<T, S> RefUnwindSafe for RwSignal<T, S>where
S: RefUnwindSafe,
impl<T, S> Send for RwSignal<T, S>where
S: Send,
impl<T, S> Sync for RwSignal<T, S>where
S: Sync,
impl<T, S> Unpin for RwSignal<T, S>where
S: Unpin,
impl<T, S> UnwindSafe for RwSignal<T, S>where
S: UnwindSafe,
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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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<F, T, S> IntoSignalSetter<T, S> for F
impl<F, T, S> IntoSignalSetter<T, S> for F
source§fn into_signal_setter(self) -> SignalSetter<T, S>
fn into_signal_setter(self) -> SignalSetter<T, S>
self
, returning SignalSetter<T>
.source§impl<T> ReactiveNode for Twhere
T: AsSubscriberSet + DefinedAt,
impl<T> ReactiveNode for Twhere
T: AsSubscriberSet + DefinedAt,
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> Read for Twhere
T: Track + ReadUntracked,
impl<T> Read for Twhere
T: Track + ReadUntracked,
source§impl<T> Set for Twhere
T: Update + IsDisposed,
impl<T> Set for Twhere
T: Update + IsDisposed,
source§impl<T> Source for Twhere
T: AsSubscriberSet + DefinedAt,
impl<T> Source for Twhere
T: AsSubscriberSet + DefinedAt,
source§fn clear_subscribers(&self)
fn clear_subscribers(&self)
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§impl<T> Update for Twhere
T: Writeable,
impl<T> Update for Twhere
T: Writeable,
source§fn try_maybe_update<U>(
&self,
fun: impl FnOnce(&mut <T as Update>::Value) -> (bool, U),
) -> Option<U>
fn try_maybe_update<U>( &self, fun: impl FnOnce(&mut <T as Update>::Value) -> (bool, U), ) -> Option<U>
(true, _)
, and returns the value returned by the update function,
or None
if the signal has already been disposed.source§fn update(&self, fun: impl FnOnce(&mut Self::Value))
fn update(&self, fun: impl FnOnce(&mut Self::Value))
source§impl<T> UpdateUntracked for Twhere
T: Writeable,
impl<T> UpdateUntracked for Twhere
T: Writeable,
source§fn try_update_untracked<U>(
&self,
fun: impl FnOnce(&mut <T as UpdateUntracked>::Value) -> U,
) -> Option<U>
fn try_update_untracked<U>( &self, fun: impl FnOnce(&mut <T as UpdateUntracked>::Value) -> U, ) -> Option<U>
None
if the signal has already been disposed.
Does not notify subscribers that the signal has changed.source§impl<T> With for Twhere
T: WithUntracked + Track,
impl<T> With for Twhere
T: WithUntracked + Track,
§type Value = <T as WithUntracked>::Value
type Value = <T as WithUntracked>::Value
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,
§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.