pub struct Extensions { /* private fields */ }Expand description
A type-erased container for app-specific state.
Extensions lets applications attach arbitrary typed values to
AppState without modifying the framework. Each type can appear at most
once — the type itself is the key.
§Examples
use atrg_core::Extensions;
struct S3Client { bucket: String }
struct SmtpConfig { host: String }
let mut ext = Extensions::new();
ext.insert(S3Client { bucket: "my-blobs".into() });
ext.insert(SmtpConfig { host: "smtp.example.com".into() });
assert_eq!(ext.get::<S3Client>().expect("registered").bucket, "my-blobs");
assert_eq!(ext.get::<SmtpConfig>().expect("registered").host, "smtp.example.com");
assert!(ext.get::<u64>().is_none());Implementations§
Source§impl Extensions
impl Extensions
Sourcepub fn insert<T: Send + Sync + 'static>(&mut self, value: T) -> Option<T>
pub fn insert<T: Send + Sync + 'static>(&mut self, value: T) -> Option<T>
Insert a value into the map. If a value of this type already exists, it is replaced and the old value is returned.
Sourcepub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>
Retrieve a reference to a value by type. Returns None if the type
has not been inserted.
Trait Implementations§
Source§impl Debug for Extensions
impl Debug for Extensions
Source§impl Default for Extensions
impl Default for Extensions
Source§fn default() -> Extensions
fn default() -> Extensions
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for Extensions
impl !RefUnwindSafe for Extensions
impl Send for Extensions
impl Sync for Extensions
impl Unpin for Extensions
impl UnsafeUnpin for Extensions
impl !UnwindSafe for Extensions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more