Struct salvo_core::Depot
source · pub struct Depot { /* private fields */ }
Expand description
Depot
is for store temp data of current request.
A depot instance is created when server get a request from client. The depot will dropped when all process
for this request done. For example, we can set current_user
in set_user
, and then use this value
in the following middlewares and handlers.
§Example
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:5800").bind().await;
Server::new(acceptor).serve(router).await;
}
Implementations§
source§impl Depot
impl Depot
sourcepub fn new() -> Depot
pub fn new() -> Depot
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 depot inner map.
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty Depot
with the specified capacity.
The depot will be able to hold at least capacity 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 elements the depot can hold without reallocating.
sourcepub fn inject<V: Any + Send + Sync>(&mut self, value: V) -> &mut Self
pub fn inject<V: Any + Send + Sync>(&mut self, value: V) -> &mut Self
Inject a value into the depot.
sourcepub fn obtain<T: Any + Send + Sync>(
&self
) -> Result<&T, Option<&Box<dyn Any + Send + Sync>>>
pub fn obtain<T: Any + Send + Sync>( &self ) -> Result<&T, Option<&Box<dyn Any + Send + Sync>>>
Obtain a reference to a value previous inject to the 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 downcast failed.
sourcepub fn obtain_mut<T: Any + Send + Sync>(
&mut self
) -> Result<&mut T, Option<&mut Box<dyn Any + Send + Sync>>>
pub fn obtain_mut<T: Any + Send + Sync>( &mut self ) -> Result<&mut T, Option<&mut Box<dyn Any + Send + Sync>>>
Obtain a mutable reference to a value previous inject to the 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 downcast failed.
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 is there a value stored in depot with this key.
sourcepub fn contains<T: Any + Send + Sync>(&self) -> bool
pub fn contains<T: Any + Send + Sync>(&self) -> bool
Check is there a value is injected to the depot.
Note: This is only check injected value.
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 downcast 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 downcast failed.
sourcepub fn remove<V: Any + Send + Sync>(
&mut self,
key: &str
) -> Result<V, Option<Box<dyn Any + Send + Sync>>>
pub fn remove<V: Any + Send + Sync>( &mut self, key: &str ) -> Result<V, Option<Box<dyn Any + Send + Sync>>>
Remove value from depot and returning the value at the key if the key was previously in the depot.