use crate::context::ContextualPayload;
use futures::Future;
use hyper::{Error, Request};
use std::io;
use std::marker::PhantomData;
#[derive(Debug)]
pub struct DropContextMakeService<T, C>
where
C: Send + 'static,
{
inner: T,
marker: PhantomData<C>,
}
impl<T, C> DropContextMakeService<T, C>
where
C: Send + 'static,
{
pub fn new(inner: T) -> Self {
Self {
inner,
marker: PhantomData,
}
}
}
impl<'a, SC, RC, T, S, F> hyper::service::MakeService<&'a SC> for DropContextMakeService<T, RC>
where
RC: Send + 'static,
T: hyper::service::MakeService<
&'a SC,
ReqBody = hyper::Body,
ResBody = hyper::Body,
Error = Error,
MakeError = io::Error,
Service = S,
Future = F,
>,
T::Future: 'static,
S: hyper::service::Service<ReqBody = hyper::Body, ResBody = hyper::Body, Error = Error>
+ 'static,
F: Future<Item = S, Error = io::Error>,
{
type ReqBody = ContextualPayload<hyper::Body, RC>;
type ResBody = hyper::Body;
type Error = Error;
type MakeError = io::Error;
type Future = Box<dyn Future<Item = Self::Service, Error = io::Error>>;
type Service = DropContextService<S, RC>;
fn make_service(&mut self, service_ctx: &'a SC) -> Self::Future {
Box::new(
self.inner
.make_service(service_ctx)
.map(DropContextService::new),
)
}
}
#[derive(Debug, Clone)]
pub struct DropContextService<T, C>
where
C: Send + 'static,
{
inner: T,
marker: PhantomData<C>,
}
impl<T, C> DropContextService<T, C>
where
C: Send + 'static,
{
pub fn new(inner: T) -> Self {
Self {
inner,
marker: PhantomData,
}
}
}
impl<T, C> hyper::service::Service for DropContextService<T, C>
where
C: Send + 'static,
T: hyper::service::Service<ReqBody = hyper::Body, ResBody = hyper::Body, Error = Error>,
{
type ReqBody = ContextualPayload<hyper::Body, C>;
type ResBody = hyper::Body;
type Error = Error;
type Future = T::Future;
fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future {
let (head, body) = req.into_parts();
let body = body.inner;
self.inner.call(Request::from_parts(head, body))
}
}
impl<T, C> crate::client::Service for DropContextService<T, C>
where
C: Send + 'static,
T: crate::client::Service<ReqBody = hyper::Body>,
{
type ReqBody = ContextualPayload<hyper::Body, C>;
type Future = T::Future;
fn request(&self, request: Request<Self::ReqBody>) -> Self::Future {
let (head, body) = request.into_parts();
self.inner.request(Request::from_parts(head, body.inner))
}
}