pub trait Extract<T, E: Error>: Sized {
// Required method
fn extract<'a>(&'a self) -> Pin<Box<dyn Future<Output = Result<T, E>> + 'a>>;
}Expand description
Implemented on a provider, it extracts a future that resolves to Ok(T) on success or Err(Self::Err) on error.
§Example
use extractors::Extract;
use std::{error::Error, fmt::Display};
struct Example(i32);
#[derive(Debug)]
struct Err;
impl Display for Err {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Err")
}
}
impl Error for Err {}
impl Extract<i32, Err> for Example {
fn extract<'a>(&'a self) -> Pin<Box<dyn Future<Output = Result<i32, Err>> + 'a>> {
Box::pin(std::future::ready(Ok(self.0)))
}
}Required Methods§
Sourcefn extract<'a>(&'a self) -> Pin<Box<dyn Future<Output = Result<T, E>> + 'a>>
fn extract<'a>(&'a self) -> Pin<Box<dyn Future<Output = Result<T, E>> + 'a>>
Extracts a value of type T.
This method should return a future that resolves to Ok(T) on success or Err(Self::Error) on error.
§Example
use extractors::Extract;
// uses the `Example` struct and impl from the `Extract` trait documentation above.
let example = Example(1);
let extracted: i32 = example.extract().await.unwrap();
assert_eq!(extracted, 1);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.