#![warn(missing_debug_implementations, rust_2018_idioms)]
#![allow(clippy::mutex_atomic, clippy::module_inception)]
#![doc(test(attr(deny(rust_2018_idioms, warnings))))]
#![doc(test(attr(allow(unused_extern_crates, unused_variables))))]
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
pub type Body = http_types::Body;
pub type Request = http_types::Request;
pub type Response = http_types::Response;
pub type Error = http_types::Error;
pub trait HttpService: Send + Sync + 'static {
type Connection: Send + 'static + Clone;
type ConnectionError: Into<Error> + Send;
type ConnectionFuture: Send
+ 'static
+ Future<Output = Result<Self::Connection, Self::ConnectionError>>;
fn connect(&self) -> Self::ConnectionFuture;
type ResponseError: Into<Error> + Send;
type ResponseFuture: Send + 'static + Future<Output = Result<Response, Self::ResponseError>>;
fn respond(&self, conn: Self::Connection, req: Request) -> Self::ResponseFuture;
}
impl<F, R, E> HttpService for F
where
F: Send + Sync + 'static + Fn(Request) -> R,
R: Send + 'static + Future<Output = Result<Response, E>>,
E: Send + Into<Error>,
{
type Connection = ();
type ConnectionError = Error;
type ConnectionFuture = OkFuture;
type ResponseFuture = R;
type ResponseError = E;
fn connect(&self) -> Self::ConnectionFuture {
OkFuture(true)
}
fn respond(&self, _conn: Self::Connection, req: Request) -> Self::ResponseFuture {
(self)(req)
}
}
#[derive(Debug)]
pub struct OkFuture(bool);
impl Unpin for OkFuture {}
impl Future for OkFuture {
type Output = Result<(), Error>;
#[inline]
fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
self.0 = false;
Poll::Ready(Ok(()))
}
}