Skip to main content

Depot

Struct Depot 

Source
pub struct Depot { /* private fields */ }
Expand description

Store temporary data for the current request.

A Depot is created when the server processes a request from a client, and dropped when all processing for the request is finished.

§Example

We set the 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

Source

pub fn new() -> Self

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 the depot’s inner map of string-keyed values.

Note: this exposes only values inserted with an explicit string key; values stored by type (via Depot::insert_typed) are kept in separate storage and are not included.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty Depot with the specified capacity for string-keyed values.

The depot will be able to hold at least capacity string-keyed elements without reallocating. If capacity is 0, the depot will not allocate.

Source

pub fn capacity(&self) -> usize

Returns the number of string-keyed elements the depot can hold without reallocating.

Source

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

Store a value in the depot, keyed by its type.

Source

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

👎Deprecated since 0.94.0:

use Depot::insert_typed instead

Deprecated alias for Depot::insert_typed.

Source

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

Get a reference to the value of the given type, previously stored by type.

Returns Err(None) if the value is not present in the depot. Returns Err(Some(Box<dyn Any + Send + Sync>)) if the value is present but downcasting failed.

Source

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

👎Deprecated since 0.94.0:

use Depot::get_typed instead

Deprecated alias for Depot::get_typed.

Source

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

Get a mutable reference to the value of the given type, previously stored by type.

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.

Source

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

👎Deprecated since 0.94.0:

use Depot::get_typed_mut instead

Deprecated alias for Depot::get_typed_mut.

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 whether a value is stored in the depot under the given key.

Source

pub fn contains_typed<T: Any + Send + Sync>(&self) -> bool

Check whether a value of the given type has been stored in the depot.

Note: Only checks values inserted via Depot::insert_typed.

Source

pub fn contains<T: Any + Send + Sync>(&self) -> bool

👎Deprecated since 0.94.0:

use Depot::contains_typed instead

Deprecated alias for Depot::contains_typed.

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 downcasting 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 downcasting failed.

Source

pub fn remove(&mut self, key: &str) -> Option<Box<dyn Any + Send + Sync>>

Remove the value at the given key from the depot and return it, if present.

The value is returned in its type-erased box; downcast it with Box::downcast if you need the concrete type back. Returns None if the key was not present.

Source

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

👎Deprecated since 0.94.0:

use Depot::remove and check the returned Option

Deprecated: use Depot::remove and check the Option (e.g. remove(key).is_some()).

Source

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

Remove the value of the given type from the depot and return it, if present.

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.

Source

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

👎Deprecated since 0.94.0:

use Depot::remove_typed instead

Deprecated alias for Depot::remove_typed.

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 !RefUnwindSafe for Depot

§

impl !UnwindSafe for Depot

§

impl Freeze for Depot

§

impl Send for Depot

§

impl Sync for Depot

§

impl Unpin for Depot

§

impl UnsafeUnpin 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<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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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>,

Source§

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