Struct Signal
pub struct Signal<T>(/* private fields */)
where
T: 'static;
Expand description
A reactive value that can be read and written to.
This is the writable version of ReadSignal
.
See create_signal
for more information.
Implementations§
§impl<T> Signal<T>
impl<T> Signal<T>
pub fn set_silent(self, new: T)
pub fn set_silent(self, new: T)
Silently set a new value for the signal. This will not trigger any updates in dependent signals. As such, this is generally not recommended as it can easily lead to state inconsistencies.
§Example
let state = create_signal(0);
let doubled = create_memo(move || state.get() * 2);
assert_eq!(doubled.get(), 0);
state.set_silent(1);
assert_eq!(doubled.get(), 0); // We now have inconsistent state!
pub fn set(self, new: T)
pub fn set(self, new: T)
Set a new value for the signal and automatically update any dependents.
§Example
let state = create_signal(0);
let doubled = create_memo(move || state.get() * 2);
assert_eq!(doubled.get(), 0);
state.set(1);
assert_eq!(doubled.get(), 2);
pub fn replace_silent(self, new: T) -> T
pub fn replace_silent(self, new: T) -> T
Silently set a new value for the signal and return the previous value.
This is the silent version of Signal::replace
.
pub fn replace(self, new: T) -> T
pub fn replace(self, new: T) -> T
Set a new value for the signal and return the previous value.
§Example
let state = create_signal(123);
let prev = state.replace(456);
assert_eq!(state.get(), 456);
assert_eq!(prev, 123);
pub fn take_silent(self) -> Twhere
T: Default,
pub fn take_silent(self) -> Twhere
T: Default,
Silently gets the value of the signal and sets the new value to the default value.
This is the silent version of Signal::take
.
pub fn take(self) -> Twhere
T: Default,
pub fn take(self) -> Twhere
T: Default,
Gets the value of the signal and sets the new value to the default value.
§Example
let state = create_signal(Some(123));
let prev = state.take();
assert_eq!(state.get(), None);
assert_eq!(prev, Some(123));
pub fn update_silent<U>(self, f: impl FnOnce(&mut T) -> U) -> U
pub fn update_silent<U>(self, f: impl FnOnce(&mut T) -> U) -> U
Update the value of the signal silently. This will not trigger any updates in dependent signals. As such, this is generally not recommended as it can easily lead to state inconsistencies.
This is the silent version of Signal::update
.
pub fn update<U>(self, f: impl FnOnce(&mut T) -> U) -> U
pub fn update<U>(self, f: impl FnOnce(&mut T) -> U) -> U
Update the value of the signal and automatically update any dependents.
Using this has the advantage of not needing to clone the value when updating it, especially
with types that do not implement Copy
where cloning can be expensive, or for types that
do not implement Clone
at all.
§Example
let state = create_signal("Hello".to_string());
state.update(|val| val.push_str(" Sycamore!"));
assert_eq!(state.get_clone(), "Hello Sycamore!");
pub fn set_fn_silent(self, f: impl FnOnce(&T) -> T)
pub fn set_fn_silent(self, f: impl FnOnce(&T) -> T)
Use a function to produce a new value and sets the value silently.
This is the silent version of Signal::set_fn
.
pub fn set_fn(self, f: impl FnOnce(&T) -> T)
pub fn set_fn(self, f: impl FnOnce(&T) -> T)
Use a function to produce a new value and sets the value.
§Example
let state = create_signal(123);
state.set_fn(|val| *val + 1);
assert_eq!(state.get(), 124);
pub fn split(self) -> (ReadSignal<T>, impl Fn(T))
pub fn split(self) -> (ReadSignal<T>, impl Fn(T))
Split the signal into a reader/writter pair.
§Example
let (read_signal, mut write_signal) = create_signal(0).split();
assert_eq!(read_signal.get(), 0);
write_signal(1);
assert_eq!(read_signal.get(), 1);
Trait Implementations§
§impl<T, Rhs> AddAssign<Rhs> for Signal<T>where
T: AddAssign<Rhs>,
impl<T, Rhs> AddAssign<Rhs> for Signal<T>where
T: AddAssign<Rhs>,
§fn add_assign(&mut self, rhs: Rhs)
fn add_assign(&mut self, rhs: Rhs)
+=
operation. Read more§impl<'de, T> Deserialize<'de> for Signal<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Signal<T>where
T: Deserialize<'de>,
§fn deserialize<D>(
deserializer: D,
) -> Result<Signal<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Signal<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
§impl<T, Rhs> DivAssign<Rhs> for Signal<T>where
T: DivAssign<Rhs>,
impl<T, Rhs> DivAssign<Rhs> for Signal<T>where
T: DivAssign<Rhs>,
§fn div_assign(&mut self, rhs: Rhs)
fn div_assign(&mut self, rhs: Rhs)
/=
operation. Read more§impl<T, Rhs> MulAssign<Rhs> for Signal<T>where
T: MulAssign<Rhs>,
impl<T, Rhs> MulAssign<Rhs> for Signal<T>where
T: MulAssign<Rhs>,
§fn mul_assign(&mut self, rhs: Rhs)
fn mul_assign(&mut self, rhs: Rhs)
*=
operation. Read more§impl<T> Ord for Signal<T>where
T: Ord,
impl<T> Ord for Signal<T>where
T: Ord,
§impl<T> PartialOrd for Signal<T>where
T: PartialOrd,
impl<T> PartialOrd for Signal<T>where
T: PartialOrd,
§impl<T, Rhs> RemAssign<Rhs> for Signal<T>where
T: RemAssign<Rhs>,
impl<T, Rhs> RemAssign<Rhs> for Signal<T>where
T: RemAssign<Rhs>,
§fn rem_assign(&mut self, rhs: Rhs)
fn rem_assign(&mut self, rhs: Rhs)
%=
operation. Read more§impl<T> Serialize for Signal<T>where
T: Serialize,
impl<T> Serialize for Signal<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, Rhs> SubAssign<Rhs> for Signal<T>where
T: SubAssign<Rhs>,
impl<T, Rhs> SubAssign<Rhs> for Signal<T>where
T: SubAssign<Rhs>,
§fn sub_assign(&mut self, rhs: Rhs)
fn sub_assign(&mut self, rhs: Rhs)
-=
operation. Read moreimpl<T> Copy for Signal<T>
impl<T> Eq for Signal<T>where
T: Eq,
Auto Trait Implementations§
impl<T> Freeze for Signal<T>
impl<T> !RefUnwindSafe for Signal<T>
impl<T> !Send for Signal<T>
impl<T> !Sync for Signal<T>
impl<T> Unpin for Signal<T>where
T: Unpin,
impl<T> !UnwindSafe for Signal<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
)