pub struct Extensions { /* private fields */ }Expand description
Type-safe container for injecting custom state into handlers.
Extensions allow pre-dispatch hooks to inject state that handlers can retrieve. This enables dependency injection without modifying handler signatures.
§Warning: Clone Behavior
Extensions is not cloned when the container is cloned. Cloning an Extensions instance
results in a new, empty map. This is because the underlying Box<dyn Any> values cannot
be cloned generically.
If you need to share state across threads/clones, use Arc<T> inside the extension.
§Example
use standout_dispatch::{Extensions, CommandContext};
// Define your state types
struct ApiClient { base_url: String }
struct UserScope { user_id: u64 }
// In a pre-dispatch hook, inject state
let mut ctx = CommandContext::default();
ctx.extensions.insert(ApiClient { base_url: "https://api.example.com".into() });
ctx.extensions.insert(UserScope { user_id: 42 });
// In a handler, retrieve state
let api = ctx.extensions.get_required::<ApiClient>()?;
println!("API base: {}", api.base_url);Implementations§
Source§impl Extensions
impl Extensions
Sourcepub fn insert<T: Send + Sync + 'static>(&mut self, val: T) -> Option<T>
pub fn insert<T: Send + Sync + 'static>(&mut self, val: T) -> Option<T>
Inserts a value into the extensions.
If a value of this type already exists, it is replaced and returned.
Sourcepub fn get<T: 'static>(&self) -> Option<&T>
pub fn get<T: 'static>(&self) -> Option<&T>
Gets a reference to a value of the specified type.
Returns None if no value of this type exists.
Sourcepub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>
Gets a mutable reference to a value of the specified type.
Returns None if no value of this type exists.
Sourcepub fn get_required<T: 'static>(&self) -> Result<&T, Error>
pub fn get_required<T: 'static>(&self) -> Result<&T, Error>
Gets a required reference to a value of the specified type.
Returns an error if no value of this type exists.
Sourcepub fn get_mut_required<T: 'static>(&mut self) -> Result<&mut T, Error>
pub fn get_mut_required<T: 'static>(&mut self) -> Result<&mut T, Error>
Gets a required mutable reference to a value of the specified type.
Returns an error if no value of this type exists.
Sourcepub fn remove<T: 'static>(&mut self) -> Option<T>
pub fn remove<T: 'static>(&mut self) -> Option<T>
Removes a value of the specified type, returning it if it existed.