Depot

Struct Depot 

Source
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

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 + Sync + Send>>

Get reference to depot inner map.

Source

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.

Source

pub fn capacity(&self) -> usize

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

Source

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

Inject a value into the depot.

Source

pub fn obtain<T>(&self) -> Result<&T, Option<&Box<dyn Any + Sync + Send>>>
where T: 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.

Source

pub fn obtain_mut<T>( &mut self, ) -> Result<&mut T, Option<&mut Box<dyn Any + Sync + Send>>>
where T: 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.

Source

pub fn insert<K, V>(&mut self, key: K, value: V) -> &mut Depot
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>(&self) -> bool
where T: Any + Send + Sync,

Check is there a value is injected to the depot.

Note: This is only check injected value.

Source

pub fn get<V>( &self, key: &str, ) -> Result<&V, Option<&Box<dyn Any + Sync + Send>>>
where V: 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>( &mut self, key: &str, ) -> Result<&mut V, Option<&mut Box<dyn Any + Sync + Send>>>
where V: 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<V>( &mut self, key: &str, ) -> Result<V, Option<Box<dyn Any + Sync + Send>>>
where V: 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>(&mut self) -> Result<T, Option<Box<dyn Any + Sync + Send>>>
where T: Any + Send + Sync,

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

Trait Implementations§

Source§

impl BasicAuthDepotExt for Depot

Source§

fn basic_auth_username(&self) -> Option<&str>

Returns the authenticated username if authentication was successful.
Source§

impl Debug for Depot

Source§

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

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
Source§

impl RequestIdDepotExt for Depot

Source§

fn csrf_token(&self) -> Option<&str>

Get request id reference from 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<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> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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 = 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>,

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