pub struct StateBox<T: Serial, S: HasStateApi> { /* private fields */ }
Expand description

A pointer type for data in the state.

The actual data is lazily loaded and thereafter cached in memory.

Due to its laziness, a StateBox can be used to defer loading of data in your state. This is useful when part of your state isn’t used in every receive method.

The type parameter T is the type stored in the box. The type parameter S is the state.

Implementations§

source§

impl<T, S> StateBox<T, S>where T: Serial + DeserialWithState<S>, S: HasStateApi,

source

pub fn get(&self) -> &T

Get a reference to the value.

source

pub fn get_mut(&mut self) -> &mut T

Get a mutable reference to the value. If the value is modified in-memory then it will be stored when the box is dropped.

source

pub fn replace(&mut self, new_val: T) -> T

Replace the value with the provided one. The current value is returned. Note that if the type T contains references to state, e.g., is a StateBox, then it must be deleted to avoid space leaks.

source

pub fn update<F, A>(&mut self, f: F) -> Awhere F: FnOnce(&mut T) -> A,

Update the existing value with the given function. The supplied function may return some data, which is then returned by update.

Trait Implementations§

source§

impl<T: Debug + Serial, S: Debug + HasStateApi> Debug for StateBox<T, S>

source§

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

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

impl<T, S> Deletable for StateBox<T, S>where T: Serial + DeserialWithState<S> + Deletable, S: HasStateApi,

source§

fn delete(self)

Delete all items that this type owns in the state.
source§

impl<S: HasStateApi, T: Serial + DeserialWithState<S>> Deref for StateBox<T, S>

§

type Target = T

The resulting type after dereferencing.
source§

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

Dereferences the value.
source§

impl<S: HasStateApi, T: Serial + DeserialWithState<S>> DerefMut for StateBox<T, S>

source§

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

Mutably dereferences the value.
source§

impl<T, S> DeserialWithState<S> for StateBox<T, S>where S: HasStateApi, T: Serial + DeserialWithState<S>,

source§

fn deserial_with_state<R: Read>(state: &S, source: &mut R) -> ParseResult<Self>

Attempt to read a structure from a given source and state, failing if an error occurs during deserialization or reading.
source§

impl<T: Serial, S: HasStateApi> Drop for StateBox<T, S>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T: Serial, S: HasStateApi> Serial for StateBox<T, S>

source§

fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err>

Attempt to write the structure into the provided writer, failing if only part of the structure could be written. Read more

Auto Trait Implementations§

§

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

§

impl<T, S> Send for StateBox<T, S>where S: Send, T: Send, <S as HasStateApi>::EntryType: Send,

§

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

§

impl<T, S> Unpin for StateBox<T, S>where S: Unpin, T: Unpin, <S as HasStateApi>::EntryType: Unpin,

§

impl<T, S> UnwindSafe for StateBox<T, S>where S: UnwindSafe, T: UnwindSafe, <S as HasStateApi>::EntryType: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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, U> TryFrom<U> for Twhere 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 Twhere 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.