pub struct Depot { /* private fields */ }Expand description
Store temporary data for the current request.
A Depot is created when the server processes a request from a client, and dropped
when all processing for the request is finished.
§Example
We set the current_user value in function set_user, and then use this value in the following
middlewares and handlers.
use salvo_core::prelude::*;
#[handler]
async fn set_user(depot: &mut Depot) {
depot.insert("user", "client");
}
#[handler]
async fn hello(depot: &mut Depot) -> String {
format!(
"Hello {}",
depot.get::<&str>("user").copied().unwrap_or_default()
)
}
#[tokio::main]
async fn main() {
let router = Router::new().hoop(set_user).goal(hello);
let acceptor = TcpListener::new("0.0.0.0:8698").bind().await;
Server::new(acceptor).serve(router).await;
}Implementations§
Source§impl Depot
impl Depot
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty Depot.
The depot is initially created with a capacity of 0, so it will not allocate until it is first inserted into.
Sourcepub fn inner(&self) -> &HashMap<String, Box<dyn Any + Send + Sync>>
pub fn inner(&self) -> &HashMap<String, Box<dyn Any + Send + Sync>>
Get reference to the depot’s inner map of string-keyed values.
Note: this exposes only values inserted with an explicit string key; values stored by
type (via Depot::insert_typed) are kept in separate storage and are not included.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty Depot with the specified capacity for string-keyed values.
The depot will be able to hold at least capacity string-keyed elements without reallocating. If capacity is 0, the depot will not allocate.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of string-keyed elements the depot can hold without reallocating.
Sourcepub fn insert_typed<V: Any + Send + Sync>(&mut self, value: V) -> &mut Self
pub fn insert_typed<V: Any + Send + Sync>(&mut self, value: V) -> &mut Self
Store a value in the depot, keyed by its type.
Sourcepub fn inject<V: Any + Send + Sync>(&mut self, value: V) -> &mut Self
👎Deprecated since 0.94.0: use Depot::insert_typed instead
pub fn inject<V: Any + Send + Sync>(&mut self, value: V) -> &mut Self
use Depot::insert_typed instead
Deprecated alias for Depot::insert_typed.
Sourcepub fn get_typed<T: Any + Send + Sync>(
&self,
) -> Result<&T, Option<&Box<dyn Any + Send + Sync>>>
pub fn get_typed<T: Any + Send + Sync>( &self, ) -> Result<&T, Option<&Box<dyn Any + Send + Sync>>>
Get a reference to the value of the given type, previously stored by type.
Returns Err(None) if the value is not present in the depot.
Returns Err(Some(Box<dyn Any + Send + Sync>)) if the value is present but downcasting
failed.
Sourcepub fn obtain<T: Any + Send + Sync>(
&self,
) -> Result<&T, Option<&Box<dyn Any + Send + Sync>>>
👎Deprecated since 0.94.0: use Depot::get_typed instead
pub fn obtain<T: Any + Send + Sync>( &self, ) -> Result<&T, Option<&Box<dyn Any + Send + Sync>>>
use Depot::get_typed instead
Deprecated alias for Depot::get_typed.
Sourcepub fn get_typed_mut<T: Any + Send + Sync>(
&mut self,
) -> Result<&mut T, Option<&mut Box<dyn Any + Send + Sync>>>
pub fn get_typed_mut<T: Any + Send + Sync>( &mut self, ) -> Result<&mut T, Option<&mut Box<dyn Any + Send + Sync>>>
Get a mutable reference to the value of the given type, previously stored by type.
Returns Err(None) if value is not present in depot.
Returns Err(Some(Box<dyn Any + Send + Sync>)) if value is present in depot but downcasting
failed.
Sourcepub fn obtain_mut<T: Any + Send + Sync>(
&mut self,
) -> Result<&mut T, Option<&mut Box<dyn Any + Send + Sync>>>
👎Deprecated since 0.94.0: use Depot::get_typed_mut instead
pub fn obtain_mut<T: Any + Send + Sync>( &mut self, ) -> Result<&mut T, Option<&mut Box<dyn Any + Send + Sync>>>
use Depot::get_typed_mut instead
Deprecated alias for Depot::get_typed_mut.
Sourcepub fn insert<K, V>(&mut self, key: K, value: V) -> &mut Self
pub fn insert<K, V>(&mut self, key: K, value: V) -> &mut Self
Inserts a key-value pair into the depot.
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Check whether a value is stored in the depot under the given key.
Sourcepub fn contains_typed<T: Any + Send + Sync>(&self) -> bool
pub fn contains_typed<T: Any + Send + Sync>(&self) -> bool
Check whether a value of the given type has been stored in the depot.
Note: Only checks values inserted via Depot::insert_typed.
Sourcepub fn contains<T: Any + Send + Sync>(&self) -> bool
👎Deprecated since 0.94.0: use Depot::contains_typed instead
pub fn contains<T: Any + Send + Sync>(&self) -> bool
use Depot::contains_typed instead
Deprecated alias for Depot::contains_typed.
Sourcepub fn get<V: Any + Send + Sync>(
&self,
key: &str,
) -> Result<&V, Option<&Box<dyn Any + Send + Sync>>>
pub fn get<V: Any + Send + Sync>( &self, key: &str, ) -> Result<&V, Option<&Box<dyn Any + Send + Sync>>>
Immutably borrows value from depot.
Returns Err(None) if value is not present in depot.
Returns Err(Some(Box<dyn Any + Send + Sync>)) if value is present in depot but downcasting
failed.
Sourcepub fn get_mut<V: Any + Send + Sync>(
&mut self,
key: &str,
) -> Result<&mut V, Option<&mut Box<dyn Any + Send + Sync>>>
pub fn get_mut<V: Any + Send + Sync>( &mut self, key: &str, ) -> Result<&mut V, Option<&mut Box<dyn Any + Send + Sync>>>
Mutably borrows value from depot.
Returns Err(None) if value is not present in depot.
Returns Err(Some(Box<dyn Any + Send + Sync>)) if value is present in depot but downcasting
failed.
Sourcepub fn remove(&mut self, key: &str) -> Option<Box<dyn Any + Send + Sync>>
pub fn remove(&mut self, key: &str) -> Option<Box<dyn Any + Send + Sync>>
Remove the value at the given key from the depot and return it, if present.
The value is returned in its type-erased box; downcast it with
Box::downcast if you need the concrete type back. Returns None if the key was not
present.
Sourcepub fn delete(&mut self, key: &str) -> bool
👎Deprecated since 0.94.0: use Depot::remove and check the returned Option
pub fn delete(&mut self, key: &str) -> bool
use Depot::remove and check the returned Option
Deprecated: use Depot::remove and check the Option (e.g. remove(key).is_some()).
Sourcepub fn remove_typed<T: Any + Send + Sync>(
&mut self,
) -> Result<T, Option<Box<dyn Any + Send + Sync>>>
pub fn remove_typed<T: Any + Send + Sync>( &mut self, ) -> Result<T, Option<Box<dyn Any + Send + Sync>>>
Remove the value of the given type from the depot and return it, if present.
Returns Err(None) if value is not present in depot.
Returns Err(Some(Box<dyn Any + Send + Sync>)) if value is present in depot but downcasting
failed.