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:
- Extracting the request body into some
String
,Bytes
, orStream
. - 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§
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.