Skip to main content

Storage

Struct Storage 

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

Browser storage interface.

This provides a typed interface to browser storage APIs. In WASM, this uses actual localStorage/sessionStorage. In tests/non-WASM, this uses an in-memory fallback.

Implementations§

Source§

impl Storage

Source

pub fn new(storage_type: StorageType) -> Self

Create a new storage instance.

Source

pub fn local() -> Self

Create localStorage instance.

Source

pub fn session() -> Self

Create sessionStorage instance.

Source

pub const fn storage_type(&self) -> StorageType

Get the storage type.

Source

pub fn get(&self, key: &str) -> Option<String>

Get a value from storage.

Source

pub fn set(&self, key: &str, value: &str) -> Result<(), StorageError>

Set a value in storage.

Source

pub fn remove(&self, key: &str) -> Result<(), StorageError>

Remove a value from storage.

Source

pub fn clear(&self) -> Result<(), StorageError>

Clear all values in storage.

Source

pub fn len(&self) -> usize

Get the number of items in storage.

Source

pub fn is_empty(&self) -> bool

Check if storage is empty.

Source

pub fn key(&self, index: usize) -> Option<String>

Get a key at the given index.

Source

pub fn keys(&self) -> Vec<String>

Get all keys in storage.

Source

pub fn get_json<T: DeserializeOwned>( &self, key: &str, ) -> Result<Option<T>, StorageError>

Get a value and deserialize it as JSON.

Source

pub fn set_json<T: Serialize>( &self, key: &str, value: &T, ) -> Result<(), StorageError>

Serialize a value as JSON and store it.

Trait Implementations§

Source§

impl Debug for Storage

Source§

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

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

impl Default for Storage

Source§

fn default() -> Self

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

Auto Trait Implementations§

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