pub trait Has<T> {
// Required methods
fn get(&self) -> &T;
fn get_mut(&mut self) -> &mut T;
fn set(&mut self, value: T);
}
Expand description
Defines methods for accessing, modifying, adding and removing the data stored in a context. Used to specify the requirements that a hyper service makes on a generic context type that it receives with a request, e.g.
struct MyService<C> {
marker: PhantomData<C>,
}
impl<C> hyper::service::Service<(hyper::Request<hyper::Body>, C)> for MyService<C>
where C: Has<MyItem> + Send + 'static
{
type Response = hyper::Response<hyper::Body>;
type Error = std::io::Error;
type Future = Pin<Box<dyn Future<Output=Result<Self::Response, Self::Error>>>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req : (hyper::Request<hyper::Body>, C)) -> Self::Future {
let (_, context) = req;
do_something_with_my_item(Has::<MyItem>::get(&context));
Box::pin(ok(hyper::Response::new(hyper::Body::empty())))
}
}