LocalsClone

Struct LocalsClone 

Source
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

Source

pub fn new() -> LocalsClone

Source

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"]);
Source

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");
Source

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());
}
Source

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)
}
Source

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);
}
Source

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.

Source

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.

Source

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));
Source

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

Source§

fn clone(&self) -> LocalsClone

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LocalsClone

Source§

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

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

impl Default for LocalsClone

Source§

fn default() -> LocalsClone

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

impl Display for LocalsClone

Source§

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

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ParamValue for T
where T: Any + Clone + Send + Sync + 'static,

Source§

fn clone_box(&self) -> Box<dyn ParamValue>

Clones this value and returns it as a boxed ParamValue. Read more
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Returns a reference to the underlying value as Any. Read more
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Returns a mutable reference to the underlying value as Any. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V