Struct leptos::StoredValue

source ·
pub struct StoredValue<T>
where T: 'static,
{ /* private fields */ }
Expand description

A non-reactive wrapper for any value, which can be created with store_value.

If you want a reactive wrapper, use create_signal.

This allows you to create a stable reference for any value by storing it within the reactive system. Like the signal types (e.g., ReadSignal and RwSignal), it is Copy and 'static. Unlike the signal types, it is not reactive; accessing it does not cause effects to subscribe, and updating it does not notify anything else.

Implementations§

source§

impl<T> StoredValue<T>

source

pub fn get_value(&self) -> T
where T: Clone,

Returns a clone of the current stored value.

§Panics

Panics if you try to access a value owned by a reactive node that has been disposed.

§Examples

#[derive(Clone)]
pub struct MyCloneableData {
    pub value: String,
}
let data = store_value(MyCloneableData { value: "a".into() });

// calling .get_value() clones and returns the value
assert_eq!(data.get_value().value, "a");
// can be `data().value` on nightly
// assert_eq!(data().value, "a");
source

pub fn try_get_value(&self) -> Option<T>
where T: Clone,

Same as StoredValue::get_value but will not panic by default.

source

pub fn with_value<U>(&self, f: impl FnOnce(&T) -> U) -> U

Applies a function to the current stored value and returns the result.

§Panics

Panics if you try to access a value owned by a reactive node that has been disposed.

§Examples

pub struct MyUncloneableData {
    pub value: String,
}
let data = store_value(MyUncloneableData { value: "a".into() });

// calling .with_value() to extract the value
assert_eq!(data.with_value(|data| data.value.clone()), "a");
source

pub fn try_with_value<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>

Same as StoredValue::with_value but returns [Some(O)] only if the stored value has not yet been disposed. None otherwise.

source

pub fn update_value(&self, f: impl FnOnce(&mut T))

Updates the stored value.

§Examples

pub struct MyUncloneableData {
    pub value: String,
}
let data = store_value(MyUncloneableData { value: "a".into() });
data.update_value(|data| data.value = "b".into());
assert_eq!(data.with_value(|data| data.value.clone()), "b");
use leptos_reactive::*;

pub struct MyUncloneableData {
    pub value: String,
}

let data = store_value(MyUncloneableData { value: "a".into() });
let updated = data.try_update_value(|data| {
    data.value = "b".into();
    data.value.clone()
});

assert_eq!(data.with_value(|data| data.value.clone()), "b");
assert_eq!(updated, Some(String::from("b")));
§Panics

Panics if there is no current reactive runtime, or if the stored value has been disposed.

source

pub fn try_update_value<O>(self, f: impl FnOnce(&mut T) -> O) -> Option<O>

Same as Self::update_value, but returns [Some(O)] if the stored value has not yet been disposed, None otherwise.

source

pub fn dispose(self)

Disposes of the stored value

source

pub fn set_value(&self, value: T)

Sets the stored value.

§Examples

pub struct MyUncloneableData {
    pub value: String,
}
let data = store_value(MyUncloneableData { value: "a".into() });
data.set_value(MyUncloneableData { value: "b".into() });
assert_eq!(data.with_value(|data| data.value.clone()), "b");
source

pub fn try_set_value(&self, value: T) -> Option<T>

Same as Self::set_value, but returns None if the stored value has not yet been disposed, [Some(T)] otherwise.

source§

impl<T> StoredValue<T>

source

pub fn new(value: T) -> StoredValue<T>

Creates a non-reactive wrapper for any value by storing it within the reactive system.

Like the signal types (e.g., ReadSignal and RwSignal), it is Copy and 'static. Unlike the signal types, it is not reactive; accessing it does not cause effects to subscribe, and updating it does not notify anything else.

// this structure is neither `Copy` nor `Clone`
pub struct MyUncloneableData {
  pub value: String
}

// ❌ this won't compile, as it can't be cloned or copied into the closures
let data = MyUncloneableData { value: "a".into() };
let callback_a = move || data.value == "a";
let callback_b = move || data.value == "b";
// this structure is neither `Copy` nor `Clone`
pub struct MyUncloneableData {
    pub value: String,
}

// ✅ you can move the `StoredValue` and access it with .with_value()
let data = StoredValue::new(MyUncloneableData { value: "a".into() });
let callback_a = move || data.with_value(|data| data.value == "a");
let callback_b = move || data.with_value(|data| data.value == "b");
§Panics

Panics if there is no current reactive runtime.

Trait Implementations§

source§

impl<T> Clone for StoredValue<T>

source§

fn clone(&self) -> StoredValue<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
source§

impl<T> Debug for StoredValue<T>

source§

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

Formats the value using the given formatter. Read more
source§

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

source§

fn default() -> StoredValue<T>

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

impl<T> Hash for StoredValue<T>

source§

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
source§

impl<T> PartialEq for StoredValue<T>

source§

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

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

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

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

impl<T> Copy for StoredValue<T>

source§

impl<T> Eq for StoredValue<T>

Auto Trait Implementations§

§

impl<T> Freeze for StoredValue<T>

§

impl<T> RefUnwindSafe for StoredValue<T>
where T: RefUnwindSafe,

§

impl<T> Send for StoredValue<T>
where T: Send,

§

impl<T> Sync for StoredValue<T>
where T: Sync,

§

impl<T> Unpin for StoredValue<T>
where T: Unpin,

§

impl<T> UnwindSafe for StoredValue<T>
where T: UnwindSafe,

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<El> ElementDescriptorBounds for El
where El: Debug,