Struct Extension

struct Extension<T>(2)

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);

Implementations

impl<H, T> HandlerWithoutStateExt for Extension<T>

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

impl<P, T> Receiver for Extension<T>

impl<R> Rng for Extension<T>

impl<R> TryCryptoRng for Extension<T>

impl<R> TryRngCore for Extension<T>

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

impl<S, T> FromRequest for Extension<T>

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

impl<S, T> Layer for Extension<T>

fn layer(self: &Self, inner: S) -> <Self as >::Service

impl<T> Any for Extension<T>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Extension<T>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Extension<T>

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

impl<T> CloneToUninit for Extension<T>

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

impl<T> CryptoRng for Extension<T>

impl<T> Deref for Extension<T>

fn deref(self: &Self) -> &<Self as >::Target

impl<T> DerefMut for Extension<T>

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

impl<T> Freeze for Extension<T>

impl<T> From for Extension<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> FromRef for Extension<T>

fn from_ref(input: &T) -> T

impl<T> Instrument for Extension<T>

impl<T> IntoResponse for Extension<T>

fn into_response(self: Self) -> Response

impl<T> IntoResponseParts for Extension<T>

fn into_response_parts(self: Self, res: ResponseParts) -> Result<ResponseParts, <Self as >::Error>

impl<T> RefUnwindSafe for Extension<T>

impl<T> RngCore for Extension<T>

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

impl<T> Same for Extension<T>

impl<T> Send for Extension<T>

impl<T> Sync for Extension<T>

impl<T> ToOwned for Extension<T>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> Unpin for Extension<T>

impl<T> UnwindSafe for Extension<T>

impl<T> WithSubscriber for Extension<T>

impl<T, S> FromRequestParts for Extension<T>

async fn from_request_parts(req: &mut Parts, _state: &S) -> Result<Self, <Self as >::Rejection>

impl<T, S> OptionalFromRequestParts for Extension<T>

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

impl<T, U> Into for Extension<T>

fn into(self: 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 for Extension<T>

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

impl<T, U> TryInto for Extension<T>

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

impl<T: $crate::clone::Clone> Clone for Extension<T>

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

impl<T: $crate::default::Default> Default for Extension<T>

fn default() -> Extension<T>

impl<T: $crate::fmt::Debug> Debug for Extension<T>

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

impl<T: $crate::marker::Copy> Copy for Extension<T>

impl<V, T> VZip for Extension<T>

fn vzip(self: Self) -> V