Skip to main content

Extensions

Struct Extensions 

Source
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

Source

pub fn new() -> Self

Creates a new empty extensions container.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn remove<T: 'static>(&mut self) -> Option<T>

Removes a value of the specified type, returning it if it existed.

Source

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

Returns true if the extensions contain a value of the specified type.

Source

pub fn len(&self) -> usize

Returns the number of extensions stored.

Source

pub fn is_empty(&self) -> bool

Returns true if no extensions are stored.

Source

pub fn clear(&mut self)

Removes all extensions.

Trait Implementations§

Source§

impl Clone for Extensions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Extensions

Source§

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

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

impl Default for Extensions

Source§

fn default() -> Extensions

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

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.