pub trait FromRequest<'a, Req>: Sized {
type Type<'b>: FromRequest<'b, Req, Error = Self::Error>;
type Error;
// Required method
fn from_request(
req: &'a Req,
) -> impl Future<Output = Result<Self, Self::Error>>;
}
Expand description
Extract type from Req asynchronously and receive them with function passed to handler_service.
'a
is the lifetime of the extracted type.
When Req
is also a borrowed type, the lifetimes of Req
type and of the extracted type should
be kept separate. See example below of extracting &str from &String:
§Examples
// new type for implementing FromRequest trait to &str.
struct Str<'a>(&'a str);
// borrowed Req type has a named lifetime of it self while trait implementor has the same lifetime
// from FromRequest's lifetime param.
impl<'a, 'r> FromRequest<'a, &'r String> for Str<'a> {
type Type<'b> = Str<'b>; // use GAT lifetime to output a named lifetime instance of implementor.
type Error = ();
async fn from_request(req: &'a &'r String) -> Result<Self, Self::Error> {
Ok(Str(req))
}
}
let input = &String::from("996");
let extract = Str::from_request(&input).await.unwrap();
assert_eq!(extract.0, input.as_str());
Required Associated Types§
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.