StorageEntry

Struct StorageEntry 

Source
pub struct StorageEntry<S: StorageBacking, T: Serialize + DeserializeOwned + Clone + Send + Sync + 'static> { /* private fields */ }
Expand description

A storage entry that can be used to store data across application reloads. It optionally provides a channel to subscribe to updates to the underlying storage.

Implementations§

Source§

impl<S, T> StorageEntry<S, T>
where S: StorageBacking, T: Serialize + DeserializeOwned + Clone + Send + Sync + 'static, S::Key: Clone,

Source

pub fn new(key: S::Key, data: T) -> Self

Creates a new StorageEntry

Methods from Deref<Target = Signal<T>>§

Source

pub fn point_to(&self, other: Signal<T, S>) -> Result<(), BorrowError>
where T: 'static,

Point to another signal. This will subscribe the other signal to all subscribers of this signal.

Source

pub fn manually_drop(&self)
where T: 'static,

Drop the value out of the signal, invalidating the signal in the process.

Source

pub fn origin_scope(&self) -> ScopeId

Get the scope the signal was created in.

Source

pub fn id(&self) -> GenerationalBoxId

Get the generational id of the signal.

Source

pub fn write_silent(&self) -> WriteLock<'static, T, S>

👎Deprecated: This pattern is no longer recommended. Prefer peek or creating new signals instead.

This pattern is no longer recommended. Prefer peek or creating new signals instead.

This function is the equivalent of the write_silent method on use_ref.

§What you should use instead
§Reading and Writing to data in the same scope

Reading and writing to the same signal in the same scope will cause that scope to rerun forever:

let mut signal = use_signal(|| 0);
// This makes the scope rerun whenever we write to the signal
println!("{}", *signal.read());
// This will rerun the scope because we read the signal earlier in the same scope
*signal.write() += 1;

You may have used the write_silent method to avoid this infinite loop with use_ref like this:

let signal = use_signal(|| 0);
// This makes the scope rerun whenever we write to the signal
println!("{}", *signal.read());
// Write silent will not rerun any subscribers
*signal.write_silent() += 1;

Instead you can use the peek and write methods instead. The peek method will not subscribe to the current scope which will avoid an infinite loop if you are reading and writing to the same signal in the same scope.

let mut signal = use_signal(|| 0);
// Peek will read the value but not subscribe to the current scope
println!("{}", *signal.peek());
// Write will update any subscribers which does not include the current scope
*signal.write() += 1;
§Reading and Writing to different data

This pattern is no longer recommended because it is very easy to allow your state and UI to grow out of sync. write_silent globally opts out of automatic state updates which can be difficult to reason about.

Lets take a look at an example: main.rs:

fn app() -> Element {
    let signal = use_context_provider(|| Signal::new(0));

    // We want to log the value of the signal whenever the app component reruns
    println!("{}", *signal.read());

    rsx! {
        button {
            // If we don't want to rerun the app component when the button is clicked, we can use write_silent
            onclick: move |_| *signal.write_silent() += 1,
            "Increment"
        }
        Child {}
    }
}

child.rs:

fn Child() -> Element {
    let signal: Signal<i32> = use_context();

    // It is difficult to tell that changing the button to use write_silent in the main.rs file will cause UI to be out of sync in a completely different file
    rsx! {
        "{signal}"
    }
}

Instead peek locally opts out of automatic state updates explicitly for a specific read which is easier to reason about.

Here is the same example using peek: main.rs:

fn app() -> Element {
    let mut signal = use_context_provider(|| Signal::new(0));

    // We want to log the value of the signal whenever the app component reruns, but we don't want to rerun the app component when the signal is updated so we use peek instead of read
    println!("{}", *signal.peek());

    rsx! {
        button {
            // We can use write like normal and update the child component automatically
            onclick: move |_| *signal.write() += 1,
            "Increment"
        }
        Child {}
    }
}

child.rs:

fn Child() -> Element {
    let signal: Signal<i32> = use_context();

    rsx! {
        "{signal}"
    }
}

Trait Implementations§

Source§

impl<S: Clone + StorageBacking, T: Clone + Serialize + DeserializeOwned + Clone + Send + Sync + 'static> Clone for StorageEntry<S, T>
where S::Key: Clone,

Source§

fn clone(&self) -> StorageEntry<S, T>

Returns a duplicate 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<S: StorageBacking, T: Debug + Serialize + DeserializeOwned + Clone + Send + Sync> Debug for StorageEntry<S, T>

Source§

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

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

impl<S: StorageBacking, T: Serialize + DeserializeOwned + Clone + Send + Sync> Deref for StorageEntry<S, T>

Source§

type Target = Signal<T>

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<S: StorageBacking, T: Serialize + DeserializeOwned + Clone + Send + Sync> DerefMut for StorageEntry<S, T>

Source§

fn deref_mut(&mut self) -> &mut Signal<T>

Mutably dereferences the value.
Source§

impl<S: StorageBacking, T: Display + Serialize + DeserializeOwned + Clone + Send + Sync> Display for StorageEntry<S, T>

Source§

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

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

impl<S, T> StorageEntryTrait<S, T> for StorageEntry<S, T>

Source§

fn save(&self)

Saves the current state to storage
Source§

fn update(&mut self)

Updates the state from storage
Source§

fn key(&self) -> &S::Key

Gets the key used to store the data in storage
Source§

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

Gets the signal that can be used to read and modify the state
Source§

fn save_to_storage_on_change(&self)

Creates a hook that will save the state to storage when the state changes

Auto Trait Implementations§

§

impl<S, T> Freeze for StorageEntry<S, T>
where <S as StorageBacking>::Key: Freeze,

§

impl<S, T> !RefUnwindSafe for StorageEntry<S, T>

§

impl<S, T> !Send for StorageEntry<S, T>

§

impl<S, T> !Sync for StorageEntry<S, T>

§

impl<S, T> Unpin for StorageEntry<S, T>
where <S as StorageBacking>::Key: Unpin, T: Unpin,

§

impl<S, T> !UnwindSafe for StorageEntry<S, 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, dest: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> InitializeFromFunction<T> for T

Source§

fn initialize_from_function(f: fn() -> T) -> T

Create an instance of this type from an initialization function
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<Ret> SpawnIfAsync<(), Ret> for Ret

Source§

fn spawn(self) -> Ret

Spawn the value into the dioxus runtime if it is an async block
Source§

impl<T, O> SuperFrom<T> for O
where O: From<T>,

Source§

fn super_from(input: T) -> O

Convert from a type to another type.
Source§

impl<T, O, M> SuperInto<O, M> for T
where O: SuperFrom<T, M>,

Source§

fn super_into(self) -> O

Convert from a type to another type.
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§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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