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

Extensions is a type map: values are stored and retrieved using their TypeId.

This allows storing arbitrary data that implements Sync + Send + 'static. This is useful when you need to share data between different middlewares in the middleware chain or make some values available from the handler to middlewares on the outgoing path (e.g. error class).

Implementations§

source§

impl Extensions

source

pub fn new() -> Self

Create an empty Extensions.

source

pub fn with<T: Send + Sync + 'static>(self, val: T) -> Self

Insert a value ino this Extensions, returning self instead of any pre-inserted values.

This is useful for any builder style patterns

let ext = Extensions::new().with(true).with(5_i32);
assert_eq!(ext.get(), Some(&true));
assert_eq!(ext.get(), Some(&5_i32));
source

pub fn append(&mut self, other: &mut Self)

Removes the values from other and inserts them into self.

source

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

Insert a value into this Extensions.

If a value of this type already exists, it will be returned.

source

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

Check if container contains value for type

source

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

Get a reference to a value previously inserted on this Extensions.

source

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

Get a mutable reference to a value previously inserted on this Extensions.

source

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

Remove a value from this Extensions.

If a value of this type exists, it will be returned.

source

pub fn clear(&mut self)

Clear the Extensions of all inserted values.

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

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

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.