sycamore::reactive

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>

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)

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

Get a value from the signal without tracking it.

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>

Creates a new memo from this signal and a function. The resulting memo will be created in the current reactive scope.

§Example
let state = create_signal(0);
let doubled = state.map(|val| *val * 2);
assert_eq!(doubled.get(), 0);
state.set(1);
assert_eq!(doubled.get(), 2);

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>

We manually implement Clone + Copy for Signal so that we don’t get extra bounds on T.

§

fn clone(&self) -> ReadSignal<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 ReadSignal<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 ReadSignal<T>
where T: Default,

§

fn default() -> ReadSignal<T>

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

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

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

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

§

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

Formats the value using the given formatter. Read more
§

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

§

type Output = T

The returned type after the call operator is used.
§

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

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

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

§

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

Converts to this type from the input type.
§

impl<T> Hash for ReadSignal<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> Ord for ReadSignal<T>
where T: Ord,

§

fn cmp(&self, other: &ReadSignal<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 ReadSignal<T>
where T: PartialEq,

§

fn eq(&self, other: &ReadSignal<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 ReadSignal<T>
where T: PartialOrd,

§

fn partial_cmp(&self, other: &ReadSignal<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> 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,

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

impl<T> Trackable for ReadSignal<T>

§

fn _track(&self)

Track the data reactively.
§

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