pub struct ReqData<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()
}
Implementations
sourceimpl<T: Clone + 'static> ReqData<T>
impl<T: Clone + 'static> ReqData<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the ReqData
, returning its wrapped data.
Trait Implementations
sourceimpl<T: Clone + 'static> FromRequest for ReqData<T>
impl<T: Clone + 'static> FromRequest for ReqData<T>
sourcefn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
Create a Self from request parts asynchronously.
sourcefn extract(req: &HttpRequest) -> Self::Future
fn extract(req: &HttpRequest) -> Self::Future
Create a Self from request head asynchronously. Read more
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
pub fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more