Storage

Struct Storage 

Source
pub struct Storage { /* private fields */ }
Expand description

An storage implementation for singleton design pattern, where we only have one value associated with each types.

Implementations§

Source§

impl Storage

Source

pub fn with<T: 'static + Default, U, F: FnOnce(&T) -> U>( &mut self, callback: F, ) -> U

Pass an immutable reference to the stored data of the type T to the closure, if there is no data associated with the type, store the Default and then perform the operation.

Source

pub fn maybe_with<T: 'static, U, F: FnOnce(&T) -> U>( &mut self, callback: F, ) -> Option<U>

Pass an immutable reference to the stored data of the type T to the closure, if there is no data associated with the type, just return None.

Source

pub fn with_mut<T: 'static + Default, U, F: FnOnce(&mut T) -> U>( &mut self, callback: F, ) -> U

Like Self::with but passes a mutable reference.

Source

pub fn maybe_with_mut<T: 'static, U, F: FnOnce(&mut T) -> U>( &mut self, callback: F, ) -> Option<U>

Like Self::maybe_with but passes a mutable reference.

Source

pub fn take<T: 'static>(&mut self) -> Option<T>

Remove the data associated with the type T, and returns it if any.

Source

pub fn swap<T: 'static>(&mut self, value: T) -> Option<T>

Store the given value for type T, returns the previously stored value if any.

Trait Implementations§

Source§

impl Default for Storage

Source§

fn default() -> Storage

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

Auto Trait Implementations§

§

impl Freeze for Storage

§

impl !RefUnwindSafe for Storage

§

impl !Send for Storage

§

impl !Sync for Storage

§

impl Unpin for Storage

§

impl !UnwindSafe for Storage

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, 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> Same for T

Source§

type Output = T

Should always be Self
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.