Skip to content

Commit

Permalink
feat(service): rename Service to HttpService, re-export tower::Se…
Browse files Browse the repository at this point in the history
…rvice`

The only important trait for a user is the `tower::Service` trait, which
is now available also at `hyper::service::Service`. The other "trait
aliases" are no longer publicly exported, as people thought they had to
implement them.

Also removes dependency on `tower-make`, which is trivial but otherwise
shouldn't affect anyone.

Closes #1959
  • Loading branch information
seanmonstar committed Oct 18, 2019
1 parent ca5836f commit 7a28893
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 266 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -34,7 +34,6 @@ log = "0.4"
pin-project = "0.4"
time = "0.1"
tower-service = "=0.3.0-alpha.2"
tower-make = { version = "=0.3.0-alpha.2a", features = ['io'] }
tokio-executor = "=0.2.0-alpha.6"
tokio-io = "=0.2.0-alpha.6"
tokio-sync = "=0.2.0-alpha.6"
Expand Down
10 changes: 3 additions & 7 deletions src/client/service.rs
@@ -1,17 +1,13 @@
//! Utilities used to interact with the Tower ecosystem.
//!
//! This module provides exports of `Service`, `MakeService` and `Connect` which
//! all provide hook-ins into the Tower ecosystem.
//! This module provides `Connect` which hook-ins into the Tower ecosystem.

use super::conn::{SendRequest, Builder};
use std::marker::PhantomData;
use crate::{common::{Poll, task, Pin}, body::Payload};
use std::future::Future;
use std::error::Error as StdError;
use tower_make::MakeConnection;

pub use tower_service::Service;
pub use tower_make::MakeService;
use crate::{common::{Poll, task, Pin}, body::Payload, service::{MakeConnection, Service}};
use super::conn::{SendRequest, Builder};

/// Creates a connection via `SendRequest`.
///
Expand Down
8 changes: 4 additions & 4 deletions src/common/exec.rs
Expand Up @@ -8,13 +8,13 @@ use tokio_executor::{SpawnError, TypedExecutor};
use crate::body::{Payload, Body};
use crate::proto::h2::server::H2Stream;
use crate::server::conn::spawn_all::{NewSvcTask, Watcher};
use crate::service::Service;
use crate::service::HttpService;

pub trait H2Exec<F, B: Payload>: Clone {
fn execute_h2stream(&mut self, fut: H2Stream<F, B>) -> crate::Result<()>;
}

pub trait NewSvcExec<I, N, S: Service<Body>, E, W: Watcher<I, S, E>>: Clone {
pub trait NewSvcExec<I, N, S: HttpService<Body>, E, W: Watcher<I, S, E>>: Clone {
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>) -> crate::Result<()>;
}

Expand Down Expand Up @@ -119,7 +119,7 @@ where
impl<I, N, S, E, W> NewSvcExec<I, N, S, E, W> for Exec
where
NewSvcTask<I, N, S, E, W>: Future<Output=()> + Send + 'static,
S: Service<Body>,
S: HttpService<Body>,
W: Watcher<I, S, E>,
{
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>) -> crate::Result<()> {
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<I, N, S, E, W> NewSvcExec<I, N, S, E, W> for E
where
E: TypedExecutor<NewSvcTask<I, N, S, E, W>> + Clone,
NewSvcTask<I, N, S, E, W>: Future<Output=()>,
S: Service<Body>,
S: HttpService<Body>,
W: Watcher<I, S, E>,
{
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>) -> crate::Result<()> {
Expand Down
10 changes: 5 additions & 5 deletions src/proto/h1/dispatch.rs
Expand Up @@ -8,7 +8,7 @@ use crate::body::{Body, Payload};
use crate::common::{Future, Never, Poll, Pin, Unpin, task};
use crate::proto::{BodyLength, DecodedLength, Conn, Dispatched, MessageHead, RequestHead, RequestLine, ResponseHead};
use super::Http1Transaction;
use crate::service::Service;
use crate::service::HttpService;

pub(crate) struct Dispatcher<D, Bs: Payload, I, T> {
conn: Conn<I, Bs::Data, T>,
Expand All @@ -29,7 +29,7 @@ pub(crate) trait Dispatch {
fn should_poll(&self) -> bool;
}

pub struct Server<S: Service<B>, B> {
pub struct Server<S: HttpService<B>, B> {
in_flight: Pin<Box<Option<S::Future>>>,
pub(crate) service: S,
}
Expand Down Expand Up @@ -407,7 +407,7 @@ impl<'a, T> Drop for OptGuard<'a, T> {

impl<S, B> Server<S, B>
where
S: Service<B>,
S: HttpService<B>,
{
pub fn new(service: S) -> Server<S, B> {
Server {
Expand All @@ -422,11 +422,11 @@ where
}

// Service is never pinned
impl<S: Service<B>, B> Unpin for Server<S, B> {}
impl<S: HttpService<B>, B> Unpin for Server<S, B> {}

impl<S, Bs> Dispatch for Server<S, Body>
where
S: Service<Body, ResBody=Bs>,
S: HttpService<Body, ResBody=Bs>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
Bs: Payload,
{
Expand Down
12 changes: 6 additions & 6 deletions src/proto/h2/server.rs
Expand Up @@ -11,15 +11,15 @@ use crate::common::exec::H2Exec;
use crate::common::{Future, Pin, Poll, task};
use crate::headers;
use crate::headers::content_length_parse_all;
use crate::service::Service;
use crate::service::HttpService;
use crate::proto::Dispatched;
use super::{PipeToSendStream, SendBuf};

use crate::{Body, Response};

pub(crate) struct Server<T, S, B, E>
where
S: Service<Body>,
S: HttpService<Body>,
B: Payload,
{
exec: E,
Expand All @@ -28,7 +28,7 @@ where
}

// TODO: fix me
impl<T, S: Service<Body>, B: Payload, E> Unpin for Server<T, S, B, E> {}
impl<T, S: HttpService<Body>, B: Payload, E> Unpin for Server<T, S, B, E> {}

enum State<T, B>
where
Expand All @@ -51,7 +51,7 @@ where
impl<T, S, B, E> Server<T, S, B, E>
where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Body, ResBody=B>,
S: HttpService<Body, ResBody=B>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
B: Payload,
B::Data: Unpin,
Expand Down Expand Up @@ -89,7 +89,7 @@ where
impl<T, S, B, E> Future for Server<T, S, B, E>
where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Body, ResBody=B>,
S: HttpService<Body, ResBody=B>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
B: Payload,
B::Data: Unpin,
Expand Down Expand Up @@ -131,7 +131,7 @@ where
{
fn poll_server<S, E>(&mut self, cx: &mut task::Context<'_>, service: &mut S, exec: &mut E) -> Poll<crate::Result<()>>
where
S: Service<
S: HttpService<
Body,
ResBody=B,
>,
Expand Down
53 changes: 27 additions & 26 deletions src/server/conn.rs
Expand Up @@ -26,7 +26,7 @@ use crate::common::io::Rewind;
use crate::common::{Future, Pin, Poll, Unpin, task};
use crate::error::{Kind, Parse};
use crate::proto;
use crate::service::{MakeServiceRef, Service};
use crate::service::{MakeServiceRef, HttpService};
use crate::upgrade::Upgraded;
use super::Accept;

Expand Down Expand Up @@ -117,7 +117,7 @@ pub(super) struct SpawnAll<I, S, E> {
#[pin_project]
pub struct Connection<T, S, E = Exec>
where
S: Service<Body>,
S: HttpService<Body>,
{
pub(super) conn: Option<Either<
proto::h1::Dispatcher<
Expand Down Expand Up @@ -356,13 +356,13 @@ impl<E> Http<E> {
///
/// ```
/// # use hyper::{Body, Request, Response};
/// # use hyper::service::Service;
/// # use hyper::service::HttpService;
/// # use hyper::server::conn::Http;
/// # use tokio_io::{AsyncRead, AsyncWrite};
/// # async fn run<I, S>(some_io: I, some_service: S)
/// # where
/// # I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
/// # S: Service<Body, ResBody=Body> + Send + 'static,
/// # S: HttpService<Body, ResBody=Body> + Send + 'static,
/// # S::Future: Send
/// # {
/// let http = Http::new();
Expand All @@ -376,7 +376,7 @@ impl<E> Http<E> {
/// ```
pub fn serve_connection<S, I, Bd>(&self, io: I, service: S) -> Connection<I, S, E>
where
S: Service<Body, ResBody=Bd>,
S: HttpService<Body, ResBody=Bd>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
Bd: Payload,
Bd::Data: Unpin,
Expand Down Expand Up @@ -431,8 +431,9 @@ impl<E> Http<E> {
ResBody=Bd,
>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
S::Service: HttpService<Body>,
Bd: Payload,
E: H2Exec<<S::Service as Service<Body>>::Future, Bd>,
E: H2Exec<<S::Service as HttpService<Body>>::Future, Bd>,
{
let mut incoming = AddrIncoming::new(addr, None)?;
if self.keep_alive {
Expand All @@ -454,7 +455,7 @@ impl<E> Http<E> {
>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
Bd: Payload,
E: H2Exec<<S::Service as Service<Body>>::Future, Bd>,
E: H2Exec<<S::Service as HttpService<Body>>::Future, Bd>,
{
let mut incoming = AddrIncoming::new(addr, Some(handle))?;
if self.keep_alive {
Expand All @@ -477,7 +478,7 @@ impl<E> Http<E> {
>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
Bd: Payload,
E: H2Exec<<S::Service as Service<Body>>::Future, Bd>,
E: H2Exec<<S::Service as HttpService<Body>>::Future, Bd>,
{
Serve {
incoming,
Expand All @@ -498,7 +499,7 @@ impl<E> Http<E> {
>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
Bd: Payload,
E: H2Exec<<S::Service as Service<Body>>::Future, Bd>,
E: H2Exec<<S::Service as HttpService<Body>>::Future, Bd>,
{
Serve {
incoming,
Expand All @@ -513,7 +514,7 @@ impl<E> Http<E> {

impl<I, B, S, E> Connection<I, S, E>
where
S: Service<Body, ResBody=B>,
S: HttpService<Body, ResBody=B>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
I: AsyncRead + AsyncWrite + Unpin,
B: Payload + 'static,
Expand Down Expand Up @@ -662,7 +663,7 @@ where

impl<I, B, S, E> Future for Connection<I, S, E>
where
S: Service<Body, ResBody=B>,
S: HttpService<Body, ResBody=B>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
I: AsyncRead + AsyncWrite + Unpin + 'static,
B: Payload + 'static,
Expand Down Expand Up @@ -700,7 +701,7 @@ where

impl<I, S> fmt::Debug for Connection<I, S>
where
S: Service<Body>,
S: HttpService<Body>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Connection")
Expand Down Expand Up @@ -740,7 +741,7 @@ where
IE: Into<Box<dyn StdError + Send + Sync>>,
S: MakeServiceRef<IO, Body, ResBody=B>,
B: Payload,
E: H2Exec<<S::Service as Service<Body>>::Future, B>,
E: H2Exec<<S::Service as HttpService<Body>>::Future, B>,
{
fn poll_next_(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<crate::Result<Connecting<IO, S::Future, E>>>> {
let me = self.project();
Expand Down Expand Up @@ -774,7 +775,7 @@ where
IE: Into<Box<dyn StdError + Send + Sync>>,
S: MakeServiceRef<IO, Body, ResBody=B>,
B: Payload,
E: H2Exec<<S::Service as Service<Body>>::Future, B>,
E: H2Exec<<S::Service as HttpService<Body>>::Future, B>,
{
type Item = crate::Result<Connecting<IO, S::Future, E>>;

Expand All @@ -790,7 +791,7 @@ impl<I, F, S, FE, E, B> Future for Connecting<I, F, E>
where
I: AsyncRead + AsyncWrite + Unpin,
F: Future<Output=Result<S, FE>>,
S: Service<Body, ResBody=B>,
S: HttpService<Body, ResBody=B>,
B: Payload,
B::Data: Unpin,
E: H2Exec<S::Future, B>,
Expand Down Expand Up @@ -831,7 +832,7 @@ where
ResBody=B,
>,
B: Payload,
E: H2Exec<<S::Service as Service<Body>>::Future, B>,
E: H2Exec<<S::Service as HttpService<Body>>::Future, B>,
{
pub(super) fn poll_watch<W>(self: Pin<&mut Self>, cx: &mut task::Context<'_>, watcher: &W) -> Poll<crate::Result<()>>
where
Expand Down Expand Up @@ -875,7 +876,7 @@ pub(crate) mod spawn_all {
use crate::body::{Body, Payload};
use crate::common::exec::H2Exec;
use crate::common::{Future, Pin, Poll, Unpin, task};
use crate::service::Service;
use crate::service::HttpService;
use super::{Connecting, UpgradeableConnection};
use pin_project::{pin_project, project};

Expand All @@ -887,7 +888,7 @@ pub(crate) mod spawn_all {
// The `Server::with_graceful_shutdown` needs to keep track of all active
// connections, and signal that they start to shutdown when prompted, so
// it has a `GracefulWatcher` implementation to do that.
pub trait Watcher<I, S: Service<Body>, E>: Clone {
pub trait Watcher<I, S: HttpService<Body>, E>: Clone {
type Future: Future<Output = crate::Result<()>>;

fn watch(&self, conn: UpgradeableConnection<I, S, E>) -> Self::Future;
Expand All @@ -900,7 +901,7 @@ pub(crate) mod spawn_all {
impl<I, S, E> Watcher<I, S, E> for NoopWatcher
where
I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
S: Service<Body> + 'static,
S: HttpService<Body> + 'static,
<S::ResBody as Payload>::Data: Unpin,
E: H2Exec<S::Future, S::ResBody>,
{
Expand All @@ -923,18 +924,18 @@ pub(crate) mod spawn_all {

#[pin_project]
#[allow(missing_debug_implementations)]
pub struct NewSvcTask<I, N, S: Service<Body>, E, W: Watcher<I, S, E>> {
pub struct NewSvcTask<I, N, S: HttpService<Body>, E, W: Watcher<I, S, E>> {
#[pin]
state: State<I, N, S, E, W>,
}

#[pin_project]
pub enum State<I, N, S: Service<Body>, E, W: Watcher<I, S, E>> {
pub enum State<I, N, S: HttpService<Body>, E, W: Watcher<I, S, E>> {
Connecting(#[pin] Connecting<I, N, E>, W),
Connected(#[pin] W::Future),
}

impl<I, N, S: Service<Body>, E, W: Watcher<I, S, E>> NewSvcTask<I, N, S, E, W> {
impl<I, N, S: HttpService<Body>, E, W: Watcher<I, S, E>> NewSvcTask<I, N, S, E, W> {
pub(super) fn new(connecting: Connecting<I, N, E>, watcher: W) -> Self {
NewSvcTask {
state: State::Connecting(connecting, watcher),
Expand All @@ -947,7 +948,7 @@ pub(crate) mod spawn_all {
I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
N: Future<Output=Result<S, NE>>,
NE: Into<Box<dyn StdError + Send + Sync>>,
S: Service<Body, ResBody=B>,
S: HttpService<Body, ResBody=B>,
B: Payload,
B::Data: Unpin,
E: H2Exec<S::Future, B>,
Expand Down Expand Up @@ -1008,14 +1009,14 @@ mod upgrades {
#[allow(missing_debug_implementations)]
pub struct UpgradeableConnection<T, S, E>
where
S: Service<Body>,
S: HttpService<Body>,
{
pub(super) inner: Connection<T, S, E>,
}

impl<I, B, S, E> UpgradeableConnection<I, S, E>
where
S: Service<Body, ResBody=B>,// + 'static,
S: HttpService<Body, ResBody=B>,// + 'static,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
I: AsyncRead + AsyncWrite + Unpin,
B: Payload + 'static,
Expand All @@ -1033,7 +1034,7 @@ mod upgrades {

impl<I, B, S, E> Future for UpgradeableConnection<I, S, E>
where
S: Service<Body, ResBody=B> + 'static,
S: HttpService<Body, ResBody=B> + 'static,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
B: Payload + 'static,
Expand Down

0 comments on commit 7a28893

Please sign in to comment.