Skip to content
This repository was archived by the owner on Jun 21, 2020. It is now read-only.

Rename Fut to ResponseFuture #29

Merged
merged 3 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ impl Server {
impl HttpService for Server {
type Connection = ();
type ConnectionFuture = future::Ready<Result<(), std::io::Error>>;
type Fut = FutureObj<'static, Result<http_service::Response, std::io::Error>>;
type ResponseFuture = FutureObj<'static, Result<http_service::Response, std::io::Error>>;

fn connect(&self) -> Self::ConnectionFuture {
future::ok(())
}

fn respond(&self, _conn: &mut (), _req: http_service::Request) -> Self::Fut {
fn respond(&self, _conn: &mut (), _req: http_service::Request) -> Self::ResponseFuture {
let message = self.message.clone();
FutureObj::new(Box::new(async move {
Ok(Response::new(http_service::Body::from(message)))
Expand Down
4 changes: 2 additions & 2 deletions examples/simple_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ impl Server {
impl HttpService for Server {
type Connection = ();
type ConnectionFuture = future::Ready<Result<(), std::io::Error>>;
type Fut = FutureObj<'static, Result<http_service::Response, std::io::Error>>;
type ResponseFuture = FutureObj<'static, Result<http_service::Response, std::io::Error>>;

fn connect(&self) -> Self::ConnectionFuture {
future::ok(())
}

fn respond(&self, _conn: &mut (), _req: http_service::Request) -> Self::Fut {
fn respond(&self, _conn: &mut (), _req: http_service::Request) -> Self::ResponseFuture {
let message = self.message.clone();
FutureObj::new(Box::new(async move {
Ok(Response::new(http_service::Body::from(message)))
Expand Down
5 changes: 4 additions & 1 deletion http-service-mock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ impl<T: HttpService> TestBackend<T> {
}

/// Send a request to the simulated server
pub fn simulate(&mut self, req: Request) -> Result<Response, <T::Fut as TryFuture>::Error> {
pub fn simulate(
&mut self,
req: Request,
) -> Result<Response, <T::ResponseFuture as TryFuture>::Error> {
block_on(
self.service
.respond(&mut self.connection, req)
Expand Down
23 changes: 12 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
//! impl HttpService for Server {
//! type Connection = ();
//! type ConnectionFuture = future::Ready<Result<(), std::io::Error>>;
//! type Fut = FutureObj<'static, Result<http_service::Response, std::io::Error>>;
//! type ResponseFuture = FutureObj<'static, Result<http_service::Response, std::io::Error>>;
//!
//! fn connect(&self) -> Self::ConnectionFuture {
//! future::ok(())
//! }
//!
//! fn respond(&self, _conn: &mut (), _req: http_service::Request) -> Self::Fut {
//! fn respond(&self, _conn: &mut (), _req: http_service::Request) -> Self::ResponseFuture {
//! let message = self.message.clone();
//! FutureObj::new(Box::new(
//! async move {
Expand Down Expand Up @@ -96,6 +96,7 @@ impl Body {
}

/// Reads the stream into a new `Vec`.
#[allow(unused_mut)]
#[allow(clippy::wrong_self_convention)] // https://github.com/rust-lang/rust-clippy/issues/4037
pub async fn into_vec(mut self) -> std::io::Result<Vec<u8>> {
let mut bytes = Vec::new();
Expand Down Expand Up @@ -164,29 +165,29 @@ pub trait HttpService: Send + Sync + 'static {
/// Returning an error will result in the server immediately dropping
/// the connection. It is usually preferable to instead return an HTTP response
/// with an error status code.
type Fut: Send + 'static + TryFuture<Ok = Response>;
type ResponseFuture: Send + 'static + TryFuture<Ok = Response>;

/// Begin handling a single request.
///
/// The handler is given shared access to the service itself, and mutable access
/// to the state for the connection where the request is taking place.
fn respond(&self, conn: &mut Self::Connection, req: Request) -> Self::Fut;
fn respond(&self, conn: &mut Self::Connection, req: Request) -> Self::ResponseFuture;
}

impl<F, Fut> HttpService for F
impl<F, R> HttpService for F
where
F: Send + Sync + 'static + Fn(Request) -> Fut,
Fut: Send + 'static + TryFuture<Ok = Response>,
Fut::Error: Send,
F: Send + Sync + 'static + Fn(Request) -> R,
R: Send + 'static + TryFuture<Ok = Response>,
R::Error: Send,
{
type Connection = ();
type ConnectionFuture = future::Ready<Result<(), Fut::Error>>;
type ConnectionFuture = future::Ready<Result<(), R::Error>>;
fn connect(&self) -> Self::ConnectionFuture {
future::ok(())
}

type Fut = Fut;
fn respond(&self, _: &mut (), req: Request) -> Self::Fut {
type ResponseFuture = R;
fn respond(&self, _: &mut (), req: Request) -> Self::ResponseFuture {
(self)(req)
}
}