pub trait FromReq<Encoding, Request, CustErr>: Sized {
    // Required method
    fn from_req(
        req: Request,
    ) -> impl Future<Output = Result<Self, ServerFnError<CustErr>>> + Send;
}
Expand description

Deserializes an HTTP request into the data type, on the server.

Implementations use the methods of the Req trait to access whatever is needed from the request. They are often quite short, usually consisting of just two steps:

  1. Extracting the request body into some String, Bytes, or Stream.
  2. Deserializing that data into the data type.

For example, here’s the implementation for Json.

impl<CustErr, T, Request> FromReq<Json, Request, CustErr> for T
where
    // require the Request implement `Req`
    Request: Req<CustErr> + Send + 'static,
    // require that the type can be deserialized with `serde`
    T: DeserializeOwned,
{
    async fn from_req(
        req: Request,
    ) -> Result<Self, ServerFnError<CustErr>> {
        // try to convert the body of the request into a `String`
        let string_data = req.try_into_string().await?;
        // deserialize the data
        serde_json::from_str::<Self>(&string_data)
            .map_err(|e| ServerFnError::Args(e.to_string()))
    }
}

Required Methods§

Source

fn from_req( req: Request, ) -> impl Future<Output = Result<Self, ServerFnError<CustErr>>> + Send

Attempts to deserialize the arguments from a request.

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<CustErr, T, Request> FromReq<GetUrl, Request, CustErr> for T
where Request: Req<CustErr> + Send + 'static, T: DeserializeOwned,

Source§

impl<CustErr, T, Request> FromReq<Json, Request, CustErr> for T
where Request: Req<CustErr> + Send + 'static, T: DeserializeOwned,

Source§

impl<CustErr, T, Request> FromReq<PostUrl, Request, CustErr> for T
where Request: Req<CustErr> + Send + 'static, T: DeserializeOwned,

Source§

impl<CustErr, T, Request> FromReq<Streaming, Request, CustErr> for T
where Request: Req<CustErr> + Send + 'static, T: From<ByteStream> + 'static,

Source§

impl<CustErr, T, Request> FromReq<StreamingText, Request, CustErr> for T
where Request: Req<CustErr> + Send + 'static, T: From<TextStream> + 'static,

Source§

impl<CustErr, T, S, Request> FromReq<StreamingJson, Request, CustErr> for S
where Request: Req<CustErr> + Send + 'static, S: Stream<Item = T> + From<JsonStream<T>> + Send + 'static, T: DeserializeOwned + 'static,