pub struct Depot { /* private fields */ }Expand description
Store temp data for current request.
A Depot created when server process a request from client. It will dropped when all process
for this request done.
§Example
We set 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() -> 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 + Sync + Send>>
pub fn inner(&self) -> &HashMap<String, Box<dyn Any + Sync + Send>>
Get reference to depot inner map.
Sourcepub fn with_capacity(capacity: usize) -> Depot
pub fn with_capacity(capacity: usize) -> Depot
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 obtain<T>(&self) -> Result<&T, Option<&Box<dyn Any + Sync + Send>>>
pub fn obtain<T>(&self) -> Result<&T, Option<&Box<dyn Any + Sync + Send>>>
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 downcasting failed.
Sourcepub fn obtain_mut<T>(
&mut self,
) -> Result<&mut T, Option<&mut Box<dyn Any + Sync + Send>>>
pub fn obtain_mut<T>( &mut self, ) -> Result<&mut T, Option<&mut Box<dyn Any + Sync + Send>>>
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 downcasting failed.
Sourcepub fn insert<K, V>(&mut self, key: K, value: V) -> &mut Depot
pub fn insert<K, V>(&mut self, key: K, value: V) -> &mut Depot
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>(&self) -> bool
pub fn contains<T>(&self) -> bool
Check is there a value is injected to the depot.
Note: This is only check injected value.
Sourcepub fn get<V>(
&self,
key: &str,
) -> Result<&V, Option<&Box<dyn Any + Sync + Send>>>
pub fn get<V>( &self, key: &str, ) -> Result<&V, Option<&Box<dyn Any + Sync + Send>>>
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>(
&mut self,
key: &str,
) -> Result<&mut V, Option<&mut Box<dyn Any + Sync + Send>>>
pub fn get_mut<V>( &mut self, key: &str, ) -> Result<&mut V, Option<&mut Box<dyn Any + Sync + Send>>>
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<V>(
&mut self,
key: &str,
) -> Result<V, Option<Box<dyn Any + Sync + Send>>>
pub fn remove<V>( &mut self, key: &str, ) -> Result<V, Option<Box<dyn Any + Sync + Send>>>
Remove value from depot and returning the value at the key if the key was previously in the depot.
Trait Implementations§
Source§impl BasicAuthDepotExt for Depot
impl BasicAuthDepotExt for Depot
Source§fn basic_auth_username(&self) -> Option<&str>
fn basic_auth_username(&self) -> Option<&str>
Source§impl RequestIdDepotExt for Depot
impl RequestIdDepotExt for Depot
Source§fn csrf_token(&self) -> Option<&str>
fn csrf_token(&self) -> Option<&str>
Auto Trait Implementations§
impl Freeze for Depot
impl !RefUnwindSafe for Depot
impl Send for Depot
impl Sync for Depot
impl Unpin for Depot
impl !UnwindSafe for Depot
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request