Skip to main content

FromRequest

Trait FromRequest 

Source
pub trait FromRequest: Sized + Send {
    // Required method
    fn from_request<'async_trait>(
        req: Request,
    ) -> Pin<Box<dyn Future<Output = Result<Self, FrameworkError>> + Send + 'async_trait>>
       where Self: 'async_trait;
}
Expand description

Trait for types that can be extracted from an HTTP request

This trait is used by the #[handler] macro to automatically extract and inject typed parameters into controller handlers.

§Implementations

  • Request - passes the request through unchanged
  • Any type implementing FormRequest - automatically parses and validates

§Example

The #[handler] macro uses this trait to transform:

#[handler]
pub async fn store(form: CreateUserRequest) -> Response {
    // ...
}

Into:

pub async fn store(req: Request) -> Response {
    let form = <CreateUserRequest as FromRequest>::from_request(req).await?;
    // ...
}

Required Methods§

Source

fn from_request<'async_trait>( req: Request, ) -> Pin<Box<dyn Future<Output = Result<Self, FrameworkError>> + Send + 'async_trait>>
where Self: 'async_trait,

Extract Self from the incoming request

Returns Err(FrameworkError) if extraction fails, which will be converted to an appropriate HTTP error response.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl FromRequest for Request

Request passes through unchanged

Source§

impl<T> FromRequest for AuthUser<T>
where T: Authenticatable + Clone + 'static,

Source§

impl<T> FromRequest for OptionalUser<T>
where T: Authenticatable + Clone + 'static,

Source§

impl<T: FormRequest> FromRequest for T

Blanket implementation of FromRequest for all FormRequest types