Struct actix_web::web::ReqData[][src]

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.

Example


#[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 optional extractor if the middleware is
    // not guaranteed to add this type of requests data
    if let Some(flag) = opt_flag {
        assert_eq!(&flag.into_inner(), req.extensions().get::<FlagFromMiddleware>().unwrap());
    }
     
    HttpResponse::Ok()
}

Implementations

Consumes the ReqData, returning its wrapped data.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Configuration for this extractor.

The associated error which can be returned.

Future that resolves to a Self.

Create a Self from request parts asynchronously.

Create a Self from request head asynchronously. Read more

Create and configure config instance.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.