refactor(body): use HttpBody with extra bounds instead of Payload trait

This commit is contained in:
Dirkjan Ochtman
2020-04-19 21:59:52 +02:00
committed by Sean McArthur
parent 203621e3be
commit aac0e2dd57
20 changed files with 142 additions and 243 deletions

View File

@@ -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;
@@ -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;
@@ -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>),
@@ -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>>,
}
@@ -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.
///
@@ -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;
@@ -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,
@@ -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>>,
{
/// Return the inner IO object, and additional information.
///
@@ -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>>,
{
type Output = crate::Result<()>;
@@ -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()
@@ -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>>,
{
let opts = self.clone();
@@ -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>>,
{
type Output = crate::Result<proto::Dispatched>;
@@ -678,7 +687,8 @@ 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,
{
}
@@ -686,7 +696,7 @@ where
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,
{
}

View File

@@ -48,6 +48,7 @@
//! # fn main () {}
//! ```
use std::error::Error as StdError;
use std::fmt;
use std::mem;
use std::time::Duration;
@@ -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")]
@@ -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 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
///
@@ -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);
@@ -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>>,
{
type Response = Response<Body>;
type Error = crate::Error;
@@ -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>,
@@ -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();
@@ -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 {

View File

@@ -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},
};
@@ -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>>,
{
type Response = SendRequest<B>;
type Error = crate::Error;