UsePersistent

Struct UsePersistent 

Source
pub struct UsePersistent<T: Serialize + DeserializeOwned + Default + 'static> { /* private fields */ }
Expand description

Storage that persists across application reloads

Implementations§

Source§

impl<T: Serialize + DeserializeOwned + Default + 'static> UsePersistent<T>

Source

pub fn read(&self) -> StorageRef<'_, T>

Returns a reference to the value

Source

pub fn write(&self) -> StorageRefMut<'_, T>

Returns a mutable reference to the value

Source

pub fn set(&self, value: T)

Sets the value

Source

pub fn modify<F: FnOnce(&mut T)>(&self, f: F)

Modifies the value

Source§

impl<T: Serialize + DeserializeOwned + Default + Clone + 'static> UsePersistent<T>

Source

pub fn get(&self) -> T

Returns a clone of the value

Methods from Deref<Target = UseRef<StorageEntry<ClientStorage, T>>>§

Source

pub fn read(&self) -> Ref<'_, T>

Read the value in the RefCell into a Ref. If this method is called while other values are still being read or write, then your app will crash.

Be very careful when working with this method. If you can, consider using the with and with_mut methods instead, choosing to render Elements during the read calls.

Source

pub fn write(&self) -> RefMut<'_, T>

Mutably unlock the value in the RefCell. This will mark the component as “dirty”

Uses to write should be as short as possible.

Be very careful when working with this method. If you can, consider using the with and with_mut methods instead, choosing to render Elements during the read and write calls.

Source

pub fn set(&self, new: T)

Set the curernt value to new_value. This will mark the component as “dirty”

This change will propagate immediately, so any other contexts that are using this RefCell will also be affected. If called during an async context, the component will not be re-rendered until the next .await call.

Source

pub fn write_silent(&self) -> RefMut<'_, T>

Mutably unlock the value in the RefCell. This will not mark the component as dirty. This is useful if you want to do some work without causing the component to re-render.

Uses to write should be as short as possible.

Be very careful when working with this method. If you can, consider using the with and with_mut methods instead, choosing to render Elements

Source

pub fn with<O>(&self, immutable_callback: impl FnOnce(&T) -> O) -> O

Take a reference to the inner value termporarily and produce a new value

Note: You can always “reborrow” the value through the RefCell. This method just does it for you automatically.

let val = use_ref(|| HashMap::<u32, String>::new());


// use reborrowing
let inner = &*val.read();

// or, be safer and use `with`
val.with(|i| println!("{:?}", i));
Source

pub fn with_mut<O>(&self, mutable_callback: impl FnOnce(&mut T) -> O) -> O

Take a reference to the inner value termporarily and produce a new value, modifying the original in place.

Note: You can always “reborrow” the value through the RefCell. This method just does it for you automatically.

let val = use_ref(|| HashMap::<u32, String>::new());


// use reborrowing
let inner = &mut *val.write();

// or, be safer and use `with`
val.with_mut(|i| i.insert(1, "hi"));
Source

pub fn needs_update(&self)

Call the inner callback to mark the originator component as dirty.

This will cause the component to be re-rendered after the current scope has ended or the current async task has been yielded through await.

Trait Implementations§

Source§

impl<T: Serialize + DeserializeOwned + Default + 'static> Deref for UsePersistent<T>

Source§

type Target = UseRef<StorageEntry<ClientStorage, T>>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: Serialize + DeserializeOwned + Default + 'static> DerefMut for UsePersistent<T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<T> Freeze for UsePersistent<T>

§

impl<T> !RefUnwindSafe for UsePersistent<T>

§

impl<T> !Send for UsePersistent<T>

§

impl<T> !Sync for UsePersistent<T>

§

impl<T> Unpin for UsePersistent<T>

§

impl<T> !UnwindSafe for UsePersistent<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> 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<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> To for T
where T: ?Sized,

Source§

fn to<T>(self) -> T
where Self: Into<T>,

Converts to T by calling Into<T>::into.
Source§

fn try_to<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Tries to convert to T by calling TryInto<T>::try_into.
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> 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