Struct salvo_core::Depot
source · pub struct Depot { /* private fields */ }
Expand description
Depot if for store temp data of current request. Each handler can read or write data to it.
Example
use salvo_core::prelude::*;
#[handler]
async fn set_user(req: &mut Request, depot: &mut Depot, res: &mut Response, ctrl: &mut FlowCtrl) {
depot.insert("user", "client");
ctrl.call_next(req, depot, res).await;
}
#[handler]
async fn hello_world(depot: &mut Depot) -> String {
format!("Hello {}", depot.get::<&str>("user").map(|s|*s).unwrap_or_default())
}
#[tokio::main]
async fn main() {
let router = Router::new().hoop(set_user).handle(hello_world);
Server::new(TcpListener::bind("127.0.0.1:7878")).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) -> Option<&T>
pub fn obtain<T: Any + Send + Sync>(&self) -> Option<&T>
Obtain a reference to a value previous inject to the depot.
sourcepub fn insert<K, V>(&mut self, key: K, value: V) -> &mut Selfwhere
K: Into<String>,
V: Any + Send + Sync,
pub fn insert<K, V>(&mut self, key: K, value: V) -> &mut Selfwhere
K: Into<String>,
V: Any + Send + Sync,
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 get<V: Any + Send + Sync>(&self, key: &str) -> Option<&V>
pub fn get<V: Any + Send + Sync>(&self, key: &str) -> Option<&V>
Immutably borrows value from depot, returing none if value is not present in depot.
sourcepub fn get_mut<V: Any + Send + Sync>(&mut self, key: &str) -> Option<&mut V>
pub fn get_mut<V: Any + Send + Sync>(&mut self, key: &str) -> Option<&mut V>
Mutably borrows value from depot, returing none if value is not present in depot.