pub struct ReqData<T>(T)
where
T: Clone + 'static;
Expand description
Request-local data extractor.
Request-local data is arbitrary data attached to an individual request, usually
by middleware. It can be set via extensions_mut
on HttpRequest
or ServiceRequest
.
Unlike app data, request data is dropped when the request has finished processing. This makes it useful as a kind of messaging system between middleware and request handlers. It uses the same types-as-keys storage system as app data.
§Mutating Request Data
Note that since extractors must output owned data, only types that impl Clone
can use this
extractor. A clone is taken of the required request data and can, therefore, not be directly
mutated in-place. To mutate request data, continue to use HttpRequest::extensions_mut
or
re-insert the cloned data back into the extensions map. A DerefMut
impl is intentionally not
provided to make this potential foot-gun more obvious.
§Examples
#[derive(Debug, Clone, PartialEq)]
struct FlagFromMiddleware(String);
/// Use the `ReqData<T>` extractor to access request data in a handler.
async fn handler(
req: HttpRequest,
opt_flag: Option<web::ReqData<FlagFromMiddleware>>,
) -> impl Responder {
// use an option extractor if middleware is not guaranteed to add this type of req data
if let Some(flag) = opt_flag {
assert_eq!(&flag.into_inner(), req.extensions().get::<FlagFromMiddleware>().unwrap());
}
HttpResponse::Ok()
}
Tuple Fields§
§0: T
Implementations§
Trait Implementations§
Source§impl<T> FromRequest for ReqData<T>where
T: Clone + 'static,
impl<T> FromRequest for ReqData<T>where
T: Clone + 'static,
Source§fn from_request(
req: &HttpRequest,
_: &mut Payload,
) -> <ReqData<T> as FromRequest>::Future
fn from_request( req: &HttpRequest, _: &mut Payload, ) -> <ReqData<T> as FromRequest>::Future
Self
from request parts asynchronously.Auto Trait Implementations§
impl<T> Freeze for ReqData<T>where
T: Freeze,
impl<T> RefUnwindSafe for ReqData<T>where
T: RefUnwindSafe,
impl<T> Send for ReqData<T>where
T: Send,
impl<T> Sync for ReqData<T>where
T: Sync,
impl<T> Unpin for ReqData<T>where
T: Unpin,
impl<T> UnwindSafe for ReqData<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, A> DynAccess<T> for A
impl<T, A> DynAccess<T> for A
Source§fn load(&self) -> DynGuard<T>
fn load(&self) -> DynGuard<T>
Access::load
.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more