Struct ReadSignal
pub struct ReadSignal<T>where
T: 'static,{ /* private fields */ }
Expand description
A read-only reactive value.
Unlike the difference between Rust’s shared and mutable-references (&T
and &mut
), the
underlying data is not immutable. The data can be updated with the corresponding Signal
(which has mutable access) and will show up in the ReadSignal
as well.
A ReadSignal
can be simply obtained by dereferencing a Signal
. In fact, every Signal
is a ReadSignal
with additional write abilities!
§Example
let signal: Signal<i32> = create_signal(123);
let read_signal: ReadSignal<i32> = *signal;
assert_eq!(read_signal.get(), 123);
signal.set(456);
assert_eq!(read_signal.get(), 456);
// read_signal.set(789); // <-- This is not allowed!
See create_signal
for more information.
Implementations§
§impl<T> ReadSignal<T>
impl<T> ReadSignal<T>
pub fn is_alive(self) -> bool
pub fn is_alive(self) -> bool
Returns true
if the signal is still alive, i.e. has not yet been disposed.
pub fn dispose(self)
pub fn dispose(self)
Disposes the signal, i.e. frees up the memory held on by this signal. Accessing a signal after it has been disposed immediately causes a panic.
pub fn get_untracked(self) -> Twhere
T: Copy,
pub fn get_untracked(self) -> Twhere
T: Copy,
Get the value of the signal without tracking it. The type must implement Copy
. If this
is not the case, use ReadSignal::get_clone_untracked
or ReadSignal::with_untracked
instead.
§Example
let state = create_signal(0);
// Note that we have used `get_untracked` here so the signal is not actually being tracked
// by the memo.
let doubled = create_memo(move || state.get_untracked() * 2);
state.set(1);
assert_eq!(doubled.get(), 0);
pub fn get_clone_untracked(self) -> Twhere
T: Clone,
pub fn get_clone_untracked(self) -> Twhere
T: Clone,
Get the value of the signal without tracking it. The type is Clone
-ed automatically.
This is the cloned equivalent of ReadSignal::get_untracked
.
pub fn get(self) -> Twhere
T: Copy,
pub fn get(self) -> Twhere
T: Copy,
Get the value of the signal. The type must implement Copy
. If this is not the case, use
ReadSignal::get_clone_untracked
or ReadSignal::with_untracked
instead.
When called inside a reactive scope, the signal will be automatically tracked.
§Example
let state = create_signal(0);
assert_eq!(state.get(), 0);
state.set(1);
assert_eq!(state.get(), 1);
// The signal is automatically tracked in the line below.
let doubled = create_memo(move || state.get());
pub fn get_clone(self) -> Twhere
T: Clone,
pub fn get_clone(self) -> Twhere
T: Clone,
Get the value of the signal. The type is Clone
-ed automatically.
When called inside a reactive scope, the signal will be automatically tracked.
If the value implements Copy
, you should use ReadSignal::get
instead.
§Example
let greeting = create_signal("Hello".to_string());
assert_eq!(greeting.get_clone(), "Hello".to_string());
// The signal is automatically tracked in the line below.
let hello_world = create_memo(move || format!("{} World!", greeting.get_clone()));
assert_eq!(hello_world.get_clone(), "Hello World!");
greeting.set("Goodbye".to_string());
assert_eq!(greeting.get_clone(), "Goodbye".to_string());
assert_eq!(hello_world.get_clone(), "Goodbye World!");
pub fn with_untracked<U>(self, f: impl FnOnce(&T) -> U) -> U
pub fn with_untracked<U>(self, f: impl FnOnce(&T) -> U) -> U
Get a value from the signal without tracking it.
pub fn with<U>(self, f: impl FnOnce(&T) -> U) -> U
pub fn with<U>(self, f: impl FnOnce(&T) -> U) -> U
Get a value from the signal.
When called inside a reactive scope, the signal will be automatically tracked.
pub fn map<U>(self, f: impl FnMut(&T) -> U + 'static) -> ReadSignal<U>
pub fn map<U>(self, f: impl FnMut(&T) -> U + 'static) -> ReadSignal<U>
pub fn track(self)
pub fn track(self)
Track the signal in the current reactive scope. This is done automatically when calling
ReadSignal::get
and other similar methods.
§Example
let state = create_signal(0);
create_effect(move || {
state.track(); // Track the signal without getting its value.
println!("Yipee!");
});
state.set(1); // Prints "Yipee!"
Trait Implementations§
§impl<T> Clone for ReadSignal<T>
impl<T> Clone for ReadSignal<T>
We manually implement Clone
+ Copy
for Signal
so that we don’t get extra bounds on T
.
§fn clone(&self) -> ReadSignal<T>
fn clone(&self) -> ReadSignal<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more§impl<T> Debug for ReadSignal<T>where
T: Debug,
impl<T> Debug for ReadSignal<T>where
T: Debug,
§impl<T> Default for ReadSignal<T>where
T: Default,
impl<T> Default for ReadSignal<T>where
T: Default,
§fn default() -> ReadSignal<T>
fn default() -> ReadSignal<T>
§impl<'de, T> Deserialize<'de> for ReadSignal<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for ReadSignal<T>where
T: Deserialize<'de>,
§fn deserialize<D>(
deserializer: D,
) -> Result<ReadSignal<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<ReadSignal<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
§impl<T> Display for ReadSignal<T>where
T: Display,
impl<T> Display for ReadSignal<T>where
T: Display,
§impl<T> FnOnce() for ReadSignal<T>where
T: Copy,
impl<T> FnOnce() for ReadSignal<T>where
T: Copy,
§impl<T, U> From<ReadSignal<U>> for MaybeDyn<T>
impl<T, U> From<ReadSignal<U>> for MaybeDyn<T>
§fn from(val: ReadSignal<U>) -> MaybeDyn<T>
fn from(val: ReadSignal<U>) -> MaybeDyn<T>
§impl<T> Hash for ReadSignal<T>where
T: Hash,
impl<T> Hash for ReadSignal<T>where
T: Hash,
§impl<T> Ord for ReadSignal<T>where
T: Ord,
impl<T> Ord for ReadSignal<T>where
T: Ord,
§impl<T> PartialEq for ReadSignal<T>where
T: PartialEq,
impl<T> PartialEq for ReadSignal<T>where
T: PartialEq,
§impl<T> PartialOrd for ReadSignal<T>where
T: PartialOrd,
impl<T> PartialOrd for ReadSignal<T>where
T: PartialOrd,
§impl<T> Serialize for ReadSignal<T>where
T: Serialize,
impl<T> Serialize for ReadSignal<T>where
T: Serialize,
§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
§impl<T> Trackable for ReadSignal<T>
impl<T> Trackable for ReadSignal<T>
impl<T> Copy for ReadSignal<T>
impl<T> Eq for ReadSignal<T>where
T: Eq,
Auto Trait Implementations§
impl<T> Freeze for ReadSignal<T>
impl<T> !RefUnwindSafe for ReadSignal<T>
impl<T> !Send for ReadSignal<T>
impl<T> !Sync for ReadSignal<T>
impl<T> Unpin for ReadSignal<T>where
T: Unpin,
impl<T> !UnwindSafe for ReadSignal<T>
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)