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: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) -> 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 + Send + Sync>>>
pub fn obtain<T>(&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 downcasting failed.
Sourcepub fn obtain_mut<T>(
&mut self,
) -> Result<&mut T, Option<&mut Box<dyn Any + Send + Sync>>>
pub fn obtain_mut<T>( &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 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 + Send + Sync>>>
pub fn get<V>( &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>(
&mut self,
key: &str,
) -> Result<&mut V, Option<&mut Box<dyn Any + Send + Sync>>>
pub fn get_mut<V>( &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<V>(
&mut self,
key: &str,
) -> Result<V, Option<Box<dyn Any + Send + Sync>>>
pub fn remove<V>( &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.
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 CsrfDepotExt for Depot
impl CsrfDepotExt for Depot
Source§fn csrf_token(&self) -> Option<&str>
fn csrf_token(&self) -> Option<&str>
Source§impl FlashDepotExt for Depot
impl FlashDepotExt for Depot
Source§fn incoming_flash(&mut self) -> Option<&Flash>
fn incoming_flash(&mut self) -> Option<&Flash>
Source§fn outgoing_flash(&self) -> &Flash
fn outgoing_flash(&self) -> &Flash
Source§fn outgoing_flash_mut(&mut self) -> &mut Flash
fn outgoing_flash_mut(&mut self) -> &mut Flash
Source§impl JwtAuthDepotExt for Depot
impl JwtAuthDepotExt for Depot
Source§fn jwt_auth_token(&self) -> Option<&str>
fn jwt_auth_token(&self) -> Option<&str>
Source§fn jwt_auth_data<C>(&self) -> Option<&TokenData<C>>
fn jwt_auth_data<C>(&self) -> Option<&TokenData<C>>
Source§fn jwt_auth_state(&self) -> JwtAuthState
fn jwt_auth_state(&self) -> JwtAuthState
Source§fn jwt_auth_error(&self) -> Option<&Error>
fn jwt_auth_error(&self) -> Option<&Error>
Source§impl RequestIdDepotExt for Depot
impl RequestIdDepotExt for Depot
Source§fn csrf_token(&self) -> Option<&str>
fn csrf_token(&self) -> Option<&str>
Source§impl SessionDepotExt for Depot
impl SessionDepotExt for Depot
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);