sycamore::reactive

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>

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)

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

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

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) -> T
where 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) -> T
where 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

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

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)

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)

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))

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>,

§

fn add_assign(&mut self, rhs: Rhs)

Performs the += operation. Read more
§

impl<T> Clone for Signal<T>

§

fn clone(&self) -> Signal<T>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
§

impl<T> Debug for Signal<T>
where T: Debug,

§

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

Formats the value using the given formatter. Read more
§

impl<T> Default for Signal<T>
where T: Default,

§

fn default() -> Signal<T>

Returns the “default value” for a type. Read more
§

impl<T> Deref for Signal<T>

§

type Target = ReadSignal<T>

The resulting type after dereferencing.
§

fn deref(&self) -> &<Signal<T> as Deref>::Target

Dereferences the value.
§

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>,

Deserialize this value from the given Serde deserializer. Read more
§

impl<T> Display for Signal<T>
where T: Display,

§

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

Formats the value using the given formatter. Read more
§

impl<T, Rhs> DivAssign<Rhs> for Signal<T>
where T: DivAssign<Rhs>,

§

fn div_assign(&mut self, rhs: Rhs)

Performs the /= operation. Read more
§

impl<T> FnOnce() for Signal<T>
where T: Copy,

§

type Output = T

The returned type after the call operator is used.
§

extern "rust-call" fn call_once( self, _args: (), ) -> <Signal<T> as FnOnce()>::Output

🔬This is a nightly-only experimental API. (fn_traits)
Performs the call operation.
§

impl<T> FnOnce(T) for Signal<T>
where T: Copy,

§

type Output = T

The returned type after the call operator is used.
§

extern "rust-call" fn call_once( self, _: (T,), ) -> <Signal<T> as FnOnce(T)>::Output

🔬This is a nightly-only experimental API. (fn_traits)
Performs the call operation.
§

impl<T, U> From<Signal<U>> for MaybeDyn<T>
where T: Into<MaybeDyn<T>>, U: Into<MaybeDyn<T>> + Clone,

§

fn from(val: Signal<U>) -> MaybeDyn<T>

Converts to this type from the input type.
§

impl<T> Hash for Signal<T>
where T: Hash,

§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

impl<T, Rhs> MulAssign<Rhs> for Signal<T>
where T: MulAssign<Rhs>,

§

fn mul_assign(&mut self, rhs: Rhs)

Performs the *= operation. Read more
§

impl<T> Ord for Signal<T>
where T: Ord,

§

fn cmp(&self, other: &Signal<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
§

impl<T> PartialEq for Signal<T>
where T: PartialEq,

§

fn eq(&self, other: &Signal<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl<T> PartialOrd for Signal<T>
where T: PartialOrd,

§

fn partial_cmp(&self, other: &Signal<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
§

impl<T, Rhs> RemAssign<Rhs> for Signal<T>
where T: RemAssign<Rhs>,

§

fn rem_assign(&mut self, rhs: Rhs)

Performs the %= operation. Read more
§

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,

Serialize this value into the given Serde serializer. Read more
§

impl<T, Rhs> SubAssign<Rhs> for Signal<T>
where T: SubAssign<Rhs>,

§

fn sub_assign(&mut self, rhs: Rhs)

Performs the -= operation. Read more
§

impl<T> Trackable for Signal<T>

§

fn _track(&self)

Track the data reactively.
§

impl<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> 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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<F, V> Component<(), V, ()> for F
where F: FnOnce() -> V,

§

fn create(self, _props: ()) -> V

Instantiate the component with the given props and reactive scope.
§

impl<F, T, V> Component<T, V, ((),)> for F
where T: Props, F: FnOnce(T) -> V,

§

fn create(self, props: T) -> V

Instantiate the component with the given props and reactive scope.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,