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
impl Truck
sourcepub fn new() -> Truck
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.
sourcepub fn with_capacity(capacity: usize) -> Self
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.
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the truck can hold without reallocating.
sourcepub fn obtain<T: Any>(&self) -> Result<&T, Option<&Box<dyn Any>>>
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.
sourcepub fn obtain_mut<T: Any>(
&mut self
) -> Result<&mut T, Option<&mut Box<dyn Any>>>
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.
sourcepub fn insert<K, V>(&mut self, key: K, value: V) -> &mut Self
pub fn insert<K, V>(&mut self, key: K, value: V) -> &mut Self
Inserts a key-value pair into the truck.
sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Check is there a value stored in truck with this key.
sourcepub fn get<V: Any>(&self, key: &str) -> Result<&V, Option<&Box<dyn Any>>>
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.
sourcepub fn get_mut<V: Any>(
&mut self,
key: &str
) -> Result<&mut V, Option<&mut Box<dyn Any>>>
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.
sourcepub fn remove<V: Any>(&mut self, key: &str) -> Result<V, Option<Box<dyn Any>>>
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.