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

source

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.

source

pub fn inner(&self) -> &HashMap<String, Box<dyn Any + Send + Sync>>

Get reference to depot inner map.

source

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.

source

pub fn capacity(&self) -> usize

Returns the number of elements the depot can hold without reallocating.

source

pub fn inject<V: Any + Send + Sync>(&mut self, value: V) -> &mut Self

Inject a value into the depot.

source

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.

source

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.

source

pub fn insert<K, V>(&mut self, key: K, value: V) -> &mut Self
where K: Into<String>, V: Any + Send + Sync,

Inserts a key-value pair into the depot.

source

pub fn contains_key(&self, key: &str) -> bool

Check is there a value stored in depot with this key.

source

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.

source

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.

source

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.

source

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.

source

pub fn delete(&mut self, key: &str) -> bool

Delete the key from depot, if the key is not present, return false.

source

pub fn scrape<T: Any + Send + Sync>( &mut self ) -> Result<T, Option<Box<dyn Any + Send + Sync>>>

Remove value from depot and returning the value if the type was previously in the depot.

Trait Implementations§

source§

impl Debug for Depot

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Depot

source§

fn default() -> Depot

Returns the “default value” for a type. Read more

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<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

source§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self, E>

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more