Struct axum::extract::Extension [−][src]
pub struct Extension<T>(pub T);
Expand description
Extractor that gets a value from request extensions.
This is commonly used to share state across handlers.
Example
use axum::{AddExtensionLayer, prelude::*}; use std::sync::Arc; // Some shared state used throughout our application struct State { // ... } async fn handler(state: extract::Extension<Arc<State>>) { // ... } let state = Arc::new(State { /* ... */ }); let app = route("/", get(handler)) // Add middleware that inserts the state into all incoming request's // extensions. .layer(AddExtensionLayer::new(state));
If the extension is missing it will reject the request with a 500 Interal Server Error
response.
Tuple Fields
0: T
Trait Implementations
type Rejection = ExtensionRejection
type Rejection = ExtensionRejection
If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response. Read more
fn from_request<'life0, 'async_trait>(
req: &'life0 mut RequestParts<B>
) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'async_trait>(
req: &'life0 mut RequestParts<B>
) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
Perform the extraction.
Auto Trait Implementations
impl<T> RefUnwindSafe for Extension<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for Extension<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Create a new Policy
that returns Action::Follow
only if self
and other
return
Action::Follow
. Read more
type Output = T
type Output = T
Should always be Self