Skip to content

Commit

Permalink
refactor: use HttpBody with extra bounds instead of Payload trait
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Apr 20, 2020
1 parent e08a271 commit 2f0b828
Show file tree
Hide file tree
Showing 20 changed files with 143 additions and 244 deletions.
5 changes: 1 addition & 4 deletions src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ pub use self::aggregate::aggregate;
pub use self::body::{Body, Sender};
pub use self::to_bytes::to_bytes;

pub(crate) use self::payload::Payload;

mod aggregate;
mod body;
mod payload;
mod to_bytes;

/// An optimization to try to take a full body if immediately available.
///
/// This is currently limited to *only* `hyper::Body`s.
pub(crate) fn take_full_data<T: Payload + 'static>(body: &mut T) -> Option<T::Data> {
pub(crate) fn take_full_data<T: HttpBody + 'static>(body: &mut T) -> Option<T::Data> {
use std::any::{Any, TypeId};

// This static type check can be optimized at compile-time.
Expand Down
139 changes: 0 additions & 139 deletions src/body/payload.rs

This file was deleted.

36 changes: 23 additions & 13 deletions src/client/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! If don't have need to manage connections yourself, consider using the
//! higher-level [Client](super) API.
use std::error::Error as StdError;
use std::fmt;
use std::mem;
use std::sync::Arc;
Expand All @@ -21,7 +22,7 @@ use tokio::io::{AsyncRead, AsyncWrite};
use tower_service::Service;

use super::dispatch;
use crate::body::Payload;
use crate::body::HttpBody;
use crate::common::{task, BoxSendFuture, Exec, Executor, Future, Pin, Poll};
use crate::proto;
use crate::upgrade::Upgraded;
Expand All @@ -32,7 +33,7 @@ type Http1Dispatcher<T, B, R> = proto::dispatch::Dispatcher<proto::dispatch::Cli
#[pin_project]
enum ProtoClient<T, B>
where
B: Payload,
B: HttpBody,
{
H1(#[pin] Http1Dispatcher<T, B, proto::h1::ClientTransaction>),
H2(#[pin] proto::h2::ClientTask<B>),
Expand Down Expand Up @@ -63,7 +64,7 @@ pub struct SendRequest<B> {
pub struct Connection<T, B>
where
T: AsyncRead + AsyncWrite + Send + 'static,
B: Payload + 'static,
B: HttpBody + 'static,
{
inner: Option<ProtoClient<T, B>>,
}
Expand Down Expand Up @@ -160,7 +161,7 @@ impl<B> SendRequest<B> {

impl<B> SendRequest<B>
where
B: Payload + 'static,
B: HttpBody + 'static,
{
/// Sends a `Request` on the associated connection.
///
Expand Down Expand Up @@ -245,7 +246,7 @@ where

impl<B> Service<Request<B>> for SendRequest<B>
where
B: Payload + 'static,
B: HttpBody + 'static,
{
type Response = Response<Body>;
type Error = crate::Error;
Expand Down Expand Up @@ -280,7 +281,7 @@ impl<B> Http2SendRequest<B> {

impl<B> Http2SendRequest<B>
where
B: Payload + 'static,
B: HttpBody + 'static,
{
pub(super) fn send_request_retryable(
&mut self,
Expand Down Expand Up @@ -328,7 +329,9 @@ impl<B> Clone for Http2SendRequest<B> {
impl<T, B> Connection<T, B>
where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
B: Payload + Unpin + 'static,
B: HttpBody + Unpin + Send + 'static,
B::Data: Send,
B::Error: Into<Box<dyn StdError + Send + Sync>> + Send,
{
/// Return the inner IO object, and additional information.
///
Expand Down Expand Up @@ -380,7 +383,9 @@ where
impl<T, B> Future for Connection<T, B>
where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
B: Payload + 'static,
B: HttpBody + Send + 'static,
B::Data: Send,
B::Error: Into<Box<dyn StdError + Send + Sync>> + Send,
{
type Output = crate::Result<()>;

Expand All @@ -404,7 +409,7 @@ where
impl<T, B> fmt::Debug for Connection<T, B>
where
T: AsyncRead + AsyncWrite + fmt::Debug + Send + 'static,
B: Payload + 'static,
B: HttpBody + 'static,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Connection").finish()
Expand Down Expand Up @@ -580,7 +585,9 @@ impl Builder {
) -> impl Future<Output = crate::Result<(SendRequest<B>, Connection<T, B>)>>
where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
B: Payload + 'static,
B: HttpBody + 'static,
B::Data: Send,
B::Error: Into<Box<dyn StdError + Send + Sync>> + Send,
{
let opts = self.clone();

Expand Down Expand Up @@ -652,7 +659,9 @@ impl fmt::Debug for ResponseFuture {
impl<T, B> Future for ProtoClient<T, B>
where
T: AsyncRead + AsyncWrite + Send + Unpin + 'static,
B: Payload + 'static,
B: HttpBody + Send + 'static,
B::Data: Send,
B::Error: Into<Box<dyn StdError + Send + Sync>> + Send,
{
type Output = crate::Result<proto::Dispatched>;

Expand All @@ -678,15 +687,16 @@ impl<B: Send> AssertSendSync for SendRequest<B> {}
impl<T: Send, B: Send> AssertSend for Connection<T, B>
where
T: AsyncRead + AsyncWrite + Send + 'static,
B: Payload + 'static,
B: HttpBody + 'static,
B::Data: Send,
{
}

#[doc(hidden)]
impl<T: Send + Sync, B: Send + Sync> AssertSendSync for Connection<T, B>
where
T: AsyncRead + AsyncWrite + Send + 'static,
B: Payload + 'static,
B: HttpBody + 'static,
B::Data: Send + Sync + 'static,
{
}
Expand Down
21 changes: 12 additions & 9 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
//! # fn main () {}
//! ```
use std::error::Error as StdError;
use std::fmt;
use std::mem;
use std::time::Duration;
Expand All @@ -60,7 +61,7 @@ use http::{Method, Request, Response, Uri, Version};

use self::connect::{sealed::Connect, Alpn, Connected, Connection};
use self::pool::{Key as PoolKey, Pool, Poolable, Pooled, Reservation};
use crate::body::{Body, Payload};
use crate::body::{Body, HttpBody};
use crate::common::{lazy as hyper_lazy, task, BoxSendFuture, Executor, Future, Lazy, Pin, Poll};

#[cfg(feature = "tcp")]
Expand Down Expand Up @@ -150,16 +151,17 @@ impl Client<(), Body> {
impl<C, B> Client<C, B>
where
C: Connect + Clone + Send + Sync + 'static,
B: Payload + Send + 'static,
B: HttpBody + Send + 'static,
B::Data: Send,
B::Error: Into<Box<dyn StdError + Send + Sync>> + Send,
{
/// Send a `GET` request to the supplied `Uri`.
///
/// # Note
///
/// This requires that the `Payload` type have a `Default` implementation.
/// This requires that the `HttpBody` type have a `Default` implementation.
/// It *should* return an "empty" version of itself, such that
/// `Payload::is_end_stream` is `true`.
/// `HttpBody::is_end_stream` is `true`.
///
/// # Example
///
Expand All @@ -180,7 +182,7 @@ where
{
let body = B::default();
if !body.is_end_stream() {
warn!("default Payload used for get() does not return true for is_end_stream");
warn!("default HttpBody used for get() does not return true for is_end_stream");
}

let mut req = Request::new(body);
Expand Down Expand Up @@ -543,8 +545,9 @@ where
impl<C, B> tower_service::Service<Request<B>> for Client<C, B>
where
C: Connect + Clone + Send + Sync + 'static,
B: Payload + Send + 'static,
B: HttpBody + Send + 'static,
B::Data: Send,
B::Error: Into<Box<dyn StdError + Send + Sync>> + Send,
{
type Response = Response<Body>;
type Error = crate::Error;
Expand Down Expand Up @@ -653,7 +656,7 @@ impl<B> PoolClient<B> {
}
}

impl<B: Payload + 'static> PoolClient<B> {
impl<B: HttpBody + 'static> PoolClient<B> {
fn send_request_retryable(
&mut self,
req: Request<B>,
Expand Down Expand Up @@ -1132,7 +1135,7 @@ impl Builder {
#[cfg(feature = "tcp")]
pub fn build_http<B>(&self) -> Client<HttpConnector, B>
where
B: Payload + Send,
B: HttpBody + Send,
B::Data: Send,
{
let mut connector = HttpConnector::new();
Expand All @@ -1146,7 +1149,7 @@ impl Builder {
pub fn build<C, B>(&self, connector: C) -> Client<C, B>
where
C: Connect + Clone,
B: Payload + Send,
B: HttpBody + Send,
B::Data: Send,
{
Client {
Expand Down
7 changes: 4 additions & 3 deletions src/client/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::marker::PhantomData;

use super::conn::{Builder, SendRequest};
use crate::{
body::Payload,
body::HttpBody,
common::{task, Pin, Poll},
service::{MakeConnection, Service},
};
Expand Down Expand Up @@ -43,8 +43,9 @@ where
C::Connection: Unpin + Send + 'static,
C::Future: Send + 'static,
C::Error: Into<Box<dyn StdError + Send + Sync>> + Send,
B: Payload + Unpin + 'static,
B::Data: Unpin,
B: HttpBody + Unpin + Send + 'static,
B::Data: Send + Unpin,
B::Error: Into<Box<dyn StdError + Send + Sync>> + Send + Sync,
{
type Response = SendRequest<B>;
type Error = crate::Error;
Expand Down
Loading

0 comments on commit 2f0b828

Please sign in to comment.