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 ;
use Arc;
// Some shared state used throughout our application
async
let state = new;
let app = new.route
// Add middleware that inserts the state into all incoming request's
// extensions.
.layer;
# 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 ;
async
;
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) -> TReturns 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) -> u32fn next_u64(&mut self) -> u64fn 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) -> Tfn 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) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses 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