Skip to content

Commit

Permalink
Move utilities and combinators to a separate package
Browse files Browse the repository at this point in the history
Hyper works with all combinators removed.

Removing methods from the trait is a breaking change, but it is possible
to do the semver trick (release 0.5, then release 0.4.x with a
dependency on the 0.5 trait) to not split the ecosystem.

Addresses #52.
  • Loading branch information
g2p committed Feb 23, 2022
1 parent a97da64 commit 3c09567
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
- name: Install Rust
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- name: Run tests
run: cargo test
run: cargo test --workspace
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ Trait representing an asynchronous, streaming, HTTP request or response body.
"""
keywords = ["http"]
categories = ["web-programming"]
resolver = "2"

[workspace]
members = ["util"]

[dependencies]
bytes = "1"
http = "0.2"
pin-project-lite = "0.2"

[dev-dependencies]
tokio = { version = "1", features = ["macros", "rt"] }
42 changes: 0 additions & 42 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,12 @@
//!
//! [`Body`]: trait.Body.html

mod empty;
mod full;
mod next;
mod size_hint;

pub mod combinators;

pub use self::empty::Empty;
pub use self::full::Full;
pub use self::next::{Data, Trailers};
pub use self::size_hint::SizeHint;

use self::combinators::{BoxBody, MapData, MapErr, UnsyncBoxBody};
use bytes::Buf;
use http::HeaderMap;
use std::ops;
Expand Down Expand Up @@ -95,41 +88,6 @@ pub trait Body {
{
Trailers(self)
}

/// Maps this body's data value to a different value.
fn map_data<F, B>(self, f: F) -> MapData<Self, F>
where
Self: Sized,
F: FnMut(Self::Data) -> B,
B: Buf,
{
MapData::new(self, f)
}

/// Maps this body's error value to a different value.
fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
where
Self: Sized,
F: FnMut(Self::Error) -> E,
{
MapErr::new(self, f)
}

/// Turn this body into a boxed trait object.
fn boxed(self) -> BoxBody<Self::Data, Self::Error>
where
Self: Sized + Send + Sync + 'static,
{
BoxBody::new(self)
}

/// Turn this body into a boxed trait object that is !Sync.
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
where
Self: Sized + Send + 'static,
{
UnsyncBoxBody::new(self)
}
}

impl<T: Body + Unpin + ?Sized> Body for &mut T {
Expand Down
28 changes: 28 additions & 0 deletions util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "http-body-util"
version = "0.4.4"
authors = [
"Carl Lerche <me@carllerche.com>",
"Lucio Franco <luciofranco14@gmail.com>",
"Sean McArthur <sean@seanmonstar.com>",
]
edition = "2018"
readme = "README.md"
documentation = "https://docs.rs/http-body-util"
repository = "https://github.com/hyperium/http-body"
license = "MIT"
description = """
Combinators and adapters for HTTP request or response bodies.
"""
keywords = ["http"]
categories = ["web-programming"]
resolver = "2"

[dependencies]
bytes = "1"
http = "0.2"
http-body = { path = ".." }
pin-project-lite = "0.2"

[dev-dependencies]
tokio = { version = "1", features = ["macros", "rt"] }
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::Body;
use crate::BodyExt as _;

use http_body::{Body, SizeHint};
use bytes::Buf;
use std::{
fmt,
Expand Down Expand Up @@ -60,7 +62,7 @@ where
self.inner.is_end_stream()
}

fn size_hint(&self) -> crate::SizeHint {
fn size_hint(&self) -> SizeHint {
self.inner.size_hint()
}
}
Expand Down Expand Up @@ -119,7 +121,7 @@ where
self.inner.is_end_stream()
}

fn size_hint(&self) -> crate::SizeHint {
fn size_hint(&self) -> SizeHint {
self.inner.size_hint()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Body;
use http_body::Body;
use bytes::Buf;
use pin_project_lite::pin_project;
use std::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Body;
use http_body::{Body, SizeHint};
use pin_project_lite::pin_project;
use std::{
any::type_name,
Expand Down Expand Up @@ -79,7 +79,7 @@ where
self.inner.is_end_stream()
}

fn size_hint(&self) -> crate::SizeHint {
fn size_hint(&self) -> SizeHint {
self.inner.size_hint()
}
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/empty.rs → util/src/empty.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Body, SizeHint};
use http_body::{Body, SizeHint};
use bytes::Buf;
use http::HeaderMap;
use std::{
Expand Down
2 changes: 1 addition & 1 deletion src/full.rs → util/src/full.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Body, SizeHint};
use http_body::{Body, SizeHint};
use bytes::{Buf, Bytes};
use http::HeaderMap;
use pin_project_lite::pin_project;
Expand Down
61 changes: 61 additions & 0 deletions util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#![deny(
missing_debug_implementations,
missing_docs,
unreachable_pub,
broken_intra_doc_links
)]
#![cfg_attr(test, deny(warnings))]

//! Utilities for [`http_body::Body`].
//!
//! [`BodyExt`] adds extensions to the common trait.
//!
//! [`Empty`] and [`Full`] provide simple implementations.

pub mod combinators;
mod empty;
mod full;

pub use self::empty::Empty;
pub use self::full::Full;
use self::combinators::{BoxBody, MapData, MapErr, UnsyncBoxBody};

/// An extension trait for [`http_body::Body`] adding various combinators and adapters
pub trait BodyExt: http_body::Body {
/// Maps this body's data value to a different value.
fn map_data<F, B>(self, f: F) -> MapData<Self, F>
where
Self: Sized,
F: FnMut(Self::Data) -> B,
B: bytes::Buf,
{
MapData::new(self, f)
}

/// Maps this body's error value to a different value.
fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
where
Self: Sized,
F: FnMut(Self::Error) -> E,
{
MapErr::new(self, f)
}

/// Turn this body into a boxed trait object.
fn boxed(self) -> BoxBody<Self::Data, Self::Error>
where
Self: Sized + Send + Sync + 'static,
{
BoxBody::new(self)
}

/// Turn this body into a boxed trait object that is !Sync.
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
where
Self: Sized + Send + 'static,
{
UnsyncBoxBody::new(self)
}
}

impl<T: ?Sized> BodyExt for T where T: http_body::Body {}

0 comments on commit 3c09567

Please sign in to comment.