Struct Extension

#[must_use]
pub struct Extension<T>(pub T);

Extractor and response for extensions.

As extractor

This is commonly used to share state across handlers.

use axum::{
    Router,
    Extension,
    routing::get,
};
use std::sync::Arc;

// Some shared state used throughout our application
struct State {
    // ...
}

async fn handler(state: Extension<Arc<State>>) {
    // ...
}

let state = Arc::new(State { /* ... */ });

let app = Router::new().route("/", get(handler))
    // Add middleware that inserts the state into all incoming request's
    // extensions.
    .layer(Extension(state));
# let _: Router = app;

If the extension is missing it will reject the request with a 500 Internal Server Error response. Alternatively, you can use Option<Extension<T>> to make the extension extractor optional.

As response

Response extensions can be used to share state with middleware.

use axum::{
    Extension,
    response::IntoResponse,
};

async fn handler() -> (Extension<Foo>, &'static str) {
    (
        Extension(Foo("foo")),
        "Hello, World!"
    )
}

#[derive(Clone)]
struct Foo(&'static str);

Fields

0: T

Trait Implementations

impl<S, T> Layer<S> for Extension<T> where T: Clone + Send + Sync + 'static,

type Service = AddExtension<S, T>;
fn layer(&self, inner: S) -> Self::Service

impl<T> Deref for Extension<T>

type Target = T;
fn deref(&self) -> &Self::Target

impl<T> DerefMut for Extension<T>

fn deref_mut(&mut self) -> &mut Self::Target

impl<T> IntoResponse for Extension<T> where T: Clone + Send + Sync + 'static,

fn into_response(self) -> Response

impl<T> IntoResponseParts for Extension<T> where T: Clone + Send + Sync + 'static,

type Error = never;
fn into_response_parts(self, res: ResponseParts) -> Result<ResponseParts, Self::Error>

impl<T, S> FromRequestParts<S> for Extension<T> where T: Clone + Send + Sync + 'static, S: Send + Sync,

type Rejection = ExtensionRejection;
async fn from_request_parts(req: &mut Parts, _state: &S) -> Result<Self, Self::Rejection>

impl<T, S> OptionalFromRequestParts<S> for Extension<T> where T: Clone + Send + Sync + 'static, S: Send + Sync,

type Rejection = never;
async fn from_request_parts(req: &mut Parts, _state: &S) -> Result<Option<Self>, Self::Rejection>

impl<T: Clone> Clone for Extension<T>

fn clone(&self) -> Extension<T>

impl<T: Copy> Copy for Extension<T>

impl<T: Debug> Debug for Extension<T>

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

impl<T: Default> Default for Extension<T>

fn default() -> Extension<T>

Auto Trait Implementations

impl<T> Freeze for Extension<T> where T: Freeze,

impl<T> RefUnwindSafe for Extension<T> where T: RefUnwindSafe,

impl<T> Send for Extension<T> where T: Send,

impl<T> Sync for Extension<T> where T: Sync,

impl<T> Unpin for Extension<T> where T: Unpin,

impl<T> UnsafeUnpin for Extension<T> where T: UnsafeUnpin,

impl<T> UnwindSafe for Extension<T> where T: UnwindSafe,

Blanket Implementations

impl<H, T> HandlerWithoutStateExt<T> for Extension<T> where H: Handler<T, ()>,

fn into_service(self) -> HandlerService<H, T, ()>
fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
fn into_make_service_with_connect_info<C>(self) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>

impl<P, T> Receiver for Extension<T> where P: Deref<Target = T> + ?Sized, T: ?Sized,

type Target = T;

impl<R> Rng for Extension<T> where R: RngCore + ?Sized,

impl<R> TryCryptoRng for Extension<T> where R: CryptoRng + ?Sized,

impl<R> TryRngCore for Extension<T> where R: RngCore + ?Sized,

type Error = never;
fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), <R as TryRngCore>::Error>

impl<S, T> FromRequest<S, ViaParts> for Extension<T> where S: Send + Sync, T: FromRequestParts<S>,

type Rejection = <T as FromRequestParts<S>>::Rejection;
fn from_request(req: Request<Body>, state: &S) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for Extension<T> where ST: ?Sized, DT: ?Sized,

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for Extension<T> where ST: ?Sized, DT: ?Sized,

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Extension<T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Extension<T> where T: ?Sized,

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

impl<T> CloneToUninit for Extension<T> where T: Clone,

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

impl<T> CryptoRng for Extension<T> where T: DerefMut, <T as Deref>::Target: CryptoRng,

impl<T> From<T> for Extension<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> FromRef<T> for Extension<T> where T: Clone,

fn from_ref(input: &T) -> T

impl<T> Instrument for Extension<T>

impl<T> Read<Exclusive, BecauseExclusive> for Extension<T> where T: ?Sized,

impl<T> RngCore for Extension<T> where T: DerefMut, <T as Deref>::Target: RngCore,

fn next_u32(&mut self) -> u32
fn next_u64(&mut self) -> u64
fn fill_bytes(&mut self, dst: &mut [u8])

impl<T> Same for Extension<T>

type Output = T;

impl<T> ToOwned for Extension<T> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> WithSubscriber for Extension<T>

impl<T, U> Into<U> for Extension<T> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

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

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Extension<T> where U: TryFrom<T>,

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

impl<V, T> VZip<V> for Extension<T> where V: MultiLane<T>,

fn vzip(self) -> V