pub struct LocalsClone { /* private fields */ }Expand description
String-based extension storage, typically used by application code.
Stores multiple values of the same type under different string keys.
Internally uses a HashMap<String, Box<dyn Any + Send + Sync>>.
Implementations§
Source§impl LocalsClone
impl LocalsClone
pub fn new() -> LocalsClone
Sourcepub fn set<T>(&mut self, key: impl Into<String>, value: T)where
T: ParamValue,
pub fn set<T>(&mut self, key: impl Into<String>, value: T)where
T: ParamValue,
Stores a value in the string-based locals storage with the given key. Any previous value with the same key will be replaced.
§Examples
use akari::extensions::LocalsClone;
let mut req = LocalsClone::default();
// Store various data with descriptive keys
req.set("user_id", 123);
req.set("is_premium", true);
req.set("cart_items", vec!["item1", "item2"]);Sourcepub fn get<T>(&self, key: &str) -> Option<&T>where
T: ParamValue,
pub fn get<T>(&self, key: &str) -> Option<&T>where
T: ParamValue,
Retrieves a reference to a value from the string-based locals storage by key.
Returns None if no value with this key exists or if the type doesn’t match.
§Examples
use akari::extensions::LocalsClone;
let mut req = LocalsClone::default();
// Store various data with descriptive keys
req.set("user_id", 123);
req.set("is_premium", true);
req.set("cart_items", vec!["item1", "item2"]);
// In a request handler
if let Some(is_premium) = req.get::<bool>("is_premium") {
if *is_premium {
// Show premium content
}
}
// With different types
let user_id = req.get::<i32>("user_id");
let items = req.get::<Vec<String>>("cart_items");Sourcepub fn get_mut<T>(&mut self, key: &str) -> Option<&mut T>where
T: ParamValue,
pub fn get_mut<T>(&mut self, key: &str) -> Option<&mut T>where
T: ParamValue,
Retrieves a mutable reference to a value from the string-based locals storage by key.
Returns None if no value with this key exists or if the type doesn’t match.
§Examples
use akari::extensions::LocalsClone;
let mut req = LocalsClone::default();
// Modify a list of items
req.set("cart_items", vec!["item1", "item2"]);
if let Some(items) = req.get_mut::<Vec<String>>("cart_items") {
items.push("new_item".to_string());
}Sourcepub fn take<T>(&mut self, key: &str) -> Option<T>where
T: ParamValue + Clone,
pub fn take<T>(&mut self, key: &str) -> Option<T>where
T: ParamValue + Clone,
Removes a value from the string-based locals storage and returns it.
Returns None if no value with this key exists or if the type doesn’t match.
§Examples
use akari::extensions::LocalsClone;
let mut req = LocalsClone::default();
// Set the token
req.set("session_token", "some_token".to_string());
// Take ownership of a value
if let Some(token) = req.take::<String>("session_token") {
// Use and consume the token
drop(token)
}Sourcepub fn keys(&self) -> Vec<&str>
pub fn keys(&self) -> Vec<&str>
Returns all keys currently stored in the locals map.
§Examples
use akari::extensions::LocalsClone;
let mut req = LocalsClone::default();
// Store various data with descriptive keys
req.set("user_id", 123);
req.set("is_premium", true);
req.set("cart_items", vec!["item1", "item2"]);
// Inspect what data is attached to the request
for key in req.keys() {
println!("Request has data with key: {}", key);
}Sourcepub fn export_param<T>(&mut self, params: &ParamsClone, key: impl Into<String>)where
T: ParamValue + Clone,
pub fn export_param<T>(&mut self, params: &ParamsClone, key: impl Into<String>)where
T: ParamValue + Clone,
Exports a param value to the locals storage with the given key. The value must implement Clone. Does nothing if the param doesn’t exist.
Sourcepub fn import_param<T>(&mut self, params: &mut ParamsClone, key: &str)where
T: ParamValue + Clone,
pub fn import_param<T>(&mut self, params: &mut ParamsClone, key: &str)where
T: ParamValue + Clone,
Imports a local value into the params storage. The value must implement Clone. Does nothing if the local doesn’t exist.
Sourcepub fn combine(&mut self, other: &LocalsClone)
pub fn combine(&mut self, other: &LocalsClone)
Combines entries from other into self.
For each key not already present in self, moves the boxed value from other.
§Examples
use akari::extensions::LocalsClone;
let mut a = LocalsClone::default();
a.set("x", 1);
let mut b = LocalsClone::default();
b.set("y", 2);
a.combine(&b);
assert_eq!(a.get::<i32>("x"), Some(&1));
assert_eq!(a.get::<i32>("y"), Some(&2));Sourcepub fn merge(&mut self, other: &LocalsClone)
pub fn merge(&mut self, other: &LocalsClone)
Merges entries from other into self.
For each key not already present in self, replaces the value with the one from other.
§Examples
use akari::extensions::LocalsClone;
let mut a = LocalsClone::default();
a.set("x", 1);
let mut b = LocalsClone::default();
b.set("y", 2);
a.merge(&b);
assert_eq!(a.get::<i32>("x"), Some(&1));
assert_eq!(a.get::<i32>("y"), Some(&2));Trait Implementations§
Source§impl Clone for LocalsClone
impl Clone for LocalsClone
Source§fn clone(&self) -> LocalsClone
fn clone(&self) -> LocalsClone
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for LocalsClone
impl Debug for LocalsClone
Source§impl Default for LocalsClone
impl Default for LocalsClone
Source§fn default() -> LocalsClone
fn default() -> LocalsClone
Auto Trait Implementations§
impl Freeze for LocalsClone
impl !RefUnwindSafe for LocalsClone
impl Send for LocalsClone
impl Sync for LocalsClone
impl Unpin for LocalsClone
impl !UnwindSafe for LocalsClone
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ParamValue for T
impl<T> ParamValue for T
Source§fn clone_box(&self) -> Box<dyn ParamValue>
fn clone_box(&self) -> Box<dyn ParamValue>
ParamValue. Read moreSource§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Any. Read moreSource§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Any. Read more