pub trait LocalStorage {
// Required methods
fn get<T>(&self) -> Option<&T>
where T: 'static;
fn get_mut<T>(&mut self) -> Option<&mut T>
where T: 'static;
fn insert<T>(&mut self, val: T) -> Option<T>
where T: Merge + Clone + Send + 'static;
fn remove<T>(&mut self) -> Option<T>
where T: 'static;
}Expand description
Provides type-based local storage for arbitrary values.
LocalStorage is a type-based per branch local storage,
which is not be shared with any other branch.
It allows storing and retrieving values by their type.
Each type T has at most one instance stored at a time.
This trait is designed for use in systems that need to manage per-node state which should be shared between nodes in the same branch, but not between branches.
§Examples
use node_flow::context::storage::LocalStorage;
use std::collections::HashMap;
use std::any::{TypeId, Any};
struct ExampleStorage(HashMap<TypeId, Box<dyn Any>>);
impl LocalStorage for ExampleStorage {
fn get<T>(&self) -> Option<&T>
where
T: 'static,
{
self.0.get(&TypeId::of::<T>())?
.downcast_ref::<T>()
}
fn get_mut<T>(&mut self) -> Option<&mut T>
where
T: 'static,
{
self.0.get_mut(&TypeId::of::<T>())?
.downcast_mut::<T>()
}
fn insert<T>(&mut self, val: T) -> Option<T>
where
T: Clone + Send + 'static,
{
self.0
.insert(TypeId::of::<T>(), Box::new(val))
.and_then(|boxed| boxed.downcast::<T>().ok().map(|b| *b))
}
fn remove<T>(&mut self) -> Option<T>
where
T: 'static,
{
self.0
.remove(&TypeId::of::<T>())
.and_then(|boxed| boxed.downcast::<T>().ok().map(|b| *b))
}
}Required Methods§
Sourcefn get<T>(&self) -> Option<&T>where
T: 'static,
fn get<T>(&self) -> Option<&T>where
T: 'static,
Gets reference of a value with type T from storage if it is present.
§Examples
#[derive(Debug, PartialEq, Eq, Clone)]
struct ExampleValue(u8);
impl Merge for ExampleValue // ...
let mut storage = ExampleStorage::new();
storage.insert(ExampleValue(5u8));
let result: Option<&ExampleValue> = storage.get();
assert_eq!(result, Some(&ExampleValue(5u8)));
let result: Option<&u16> = storage.get();
assert_eq!(result, None);Sourcefn get_mut<T>(&mut self) -> Option<&mut T>where
T: 'static,
fn get_mut<T>(&mut self) -> Option<&mut T>where
T: 'static,
Gets mutable reference of a value with type T from storage if it is present.
§Examples
#[derive(Debug, PartialEq, Eq, Clone)]
struct ExampleValue(u8);
impl Merge for ExampleValue // ...
let mut storage = ExampleStorage::new();
storage.insert(ExampleValue(5u8));
if let Some(val) = storage.get_mut::<ExampleValue>() {
val.0 = 15u8;
}
let result: Option<&ExampleValue> = storage.get();
assert_eq!(result, Some(&ExampleValue(15u8)));Sourcefn insert<T>(&mut self, val: T) -> Option<T>
fn insert<T>(&mut self, val: T) -> Option<T>
Inserts value with type T to storage and returns the value that was there previously if it was there.
§Examples
#[derive(Debug, PartialEq, Eq, Clone)]
struct ExampleValue(u8);
impl Merge for ExampleValue // ...
let mut storage = ExampleStorage::new();
let result = storage.insert(ExampleValue(5u8));
assert_eq!(result, None);
storage.insert(ExampleValue(15u8));
let result = storage.insert(ExampleValue(25u8));
assert_eq!(result, Some(ExampleValue(15u8)));
let result: Option<&ExampleValue> = storage.get();
assert_eq!(result, Some(&ExampleValue(25u8)));Sourcefn remove<T>(&mut self) -> Option<T>where
T: 'static,
fn remove<T>(&mut self) -> Option<T>where
T: 'static,
Removes and returns value with type T from storage if it is present.
§Examples
#[derive(Debug, PartialEq, Eq, Clone)]
struct ExampleValue(u8);
impl Merge for ExampleValue // ...
let mut storage = ExampleStorage::new();
let result = storage.insert(ExampleValue(5u8));
assert_eq!(result, None);
let result = storage.remove();
assert_eq!(result, Some(ExampleValue(5u8)));
let result = storage.remove::<ExampleValue>();
assert_eq!(result, None);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl LocalStorage for LocalStorageImpl
local_storage_impl only.