Struct glory_core::Truck

source ·
pub struct Truck { /* private fields */ }
Expand description

Truck is for store temp data of current request. Each handler can read or write data to it.

Example

use salvo_core::prelude::*;

#[handler]
async fn set_user(truck: &mut Truck) {
    truck.insert("user", "client");
    ctrl.call_next(req, truck, res).await;
}
#[handler]
async fn hello(truck: &mut Truck) -> String {
    format!("Hello {}", truck.get::<&str>("user").map(|s|*s).unwrap_or_default())
}
#[tokio::main]
async fn main() {
    let router = Router::new().hoop(set_user).handle(hello);
    let acceptor = TcpListener::new("127.0.0.1:5800").bind().await;
    Server::new(acceptor).serve(router).await;
}

Implementations§

source§

impl Truck

source

pub fn new() -> Truck

Creates an empty Truck.

The truck 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>>

Get reference to truck inner map.

source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty Truck with the specified capacity.

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

source

pub fn capacity(&self) -> usize

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

source

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

Inject a value into the truck.

source

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

Obtain a reference to a value previous inject to the truck.

Returns Err(None) if value is not present in truck. Returns Err(Some(Box<dyn Any>)) if value is present in truck but downcast failed.

source

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

Obtain a mutable reference to a value previous inject to the truck.

Returns Err(None) if value is not present in truck. Returns Err(Some(Box<dyn Any>)) if value is present in truck but downcast failed.

source

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

Inserts a key-value pair into the truck.

source

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

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

source

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

Check is there a value stored in truck.

source

pub fn get<V: Any>(&self, key: &str) -> Result<&V, Option<&Box<dyn Any>>>

Immutably borrows value from truck.

Returns Err(None) if value is not present in truck. Returns Err(Some(Box<dyn Any>)) if value is present in truck but downcast failed.

source

pub fn get_mut<V: Any>( &mut self, key: &str ) -> Result<&mut V, Option<&mut Box<dyn Any>>>

Mutably borrows value from truck.

Returns Err(None) if value is not present in truck. Returns Err(Some(Box<dyn Any>)) if value is present in truck but downcast failed.

source

pub fn remove<V: Any>(&mut self, key: &str) -> Result<V, Option<Box<dyn Any>>>

Remove value from truck and returning the value at the key if the key was previously in the truck.

source

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

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

source

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

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

Trait Implementations§

source§

impl Debug for Truck

source§

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

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

impl Default for Truck

source§

fn default() -> Truck

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

Auto Trait Implementations§

§

impl !RefUnwindSafe for Truck

§

impl !Send for Truck

§

impl !Sync for Truck

§

impl Unpin for Truck

§

impl !UnwindSafe for Truck

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