Skip to main content

Extensions

Struct Extensions 

Source
pub struct Extensions { /* private fields */ }
Expand description

A type-indexed bag of arbitrary values carried by a Context.

This is how work threaded through a context accumulates live state that the backend generic doesn’t model: a request id set by an HTTP layer, a policy decision recorded by an authorization check, a database or cache client to be reused downstream, a logger or span. Each value is keyed by its own Rust type, so there is at most one value per type and reads are fully typed — no string keys, no downcasting at the call site. This is the same pattern as http::Extensions.

Values must be Send + Sync + 'static so the context (and everything it carries) can cross await points and move between threads.

use ash_domain::Extensions;

#[derive(Debug, PartialEq)]
struct RequestId(String);

let mut ext = Extensions::new();
ext.insert(RequestId("abc-123".into()));

assert_eq!(ext.get::<RequestId>(), Some(&RequestId("abc-123".into())));
assert!(ext.get::<u64>().is_none());

Implementations§

Source§

impl Extensions

Source

pub fn new() -> Self

An empty bag.

Source

pub fn insert<T: Send + Sync + 'static>(&mut self, value: T) -> Option<T>

Insert a value, keyed by its type. Returns the previous value of the same type, if one was present.

Source

pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>

Borrow the value of type T, if one is present.

Source

pub fn get_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T>

Mutably borrow the value of type T, if one is present.

Source

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

Whether a value of type T is present.

Source

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

Remove and return the value of type T, if one is present.

Source

pub fn merge(&mut self, other: Extensions)

Move every value out of other into this bag, consuming it.

A type present in both is overwritten by other’s value: the bag being merged in is the newer, more specific one (a handler’s scratch merging back into the request context), so it wins.

use ash_domain::Extensions;
#[derive(Debug, PartialEq)] struct Timing(u64);
#[derive(Debug, PartialEq)] struct Tag(&'static str);

let mut parent = Extensions::new();
parent.insert(Timing(1));
parent.insert(Tag("parent"));

let mut scratch = Extensions::new();
scratch.insert(Timing(99));

parent.merge(scratch);
assert_eq!(parent.get::<Timing>(), Some(&Timing(99))); // scratch wins
assert_eq!(parent.get::<Tag>(), Some(&Tag("parent"))); // untouched
Source

pub fn is_empty(&self) -> bool

Whether the bag holds no values.

Source

pub fn len(&self) -> usize

How many values the bag holds.

Trait Implementations§

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() -> Self

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.