use std::fmt;
#[derive(Clone, Default)]
pub struct Context {
values: http::Extensions,
}
impl Context {
pub fn new() -> Self {
Self::default()
}
pub fn insert<T: Clone + Send + Sync + 'static>(&mut self, value: T) -> Option<T> {
self.values.insert(value)
}
pub fn with<T: Clone + Send + Sync + 'static>(mut self, value: T) -> Self {
self.values.insert(value);
self
}
pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T> {
self.values.get::<T>()
}
pub fn of<B>(request: &http::Request<B>) -> Option<&Self> {
request.extensions().get::<Self>()
}
}
impl fmt::Debug for Context {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.debug_struct("Context").finish_non_exhaustive()
}
}