Skip to content

Commit

Permalink
Move adapters to http-body-util (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
g2p committed May 23, 2022
1 parent 6f722a1 commit aa90d1d
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
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
37 changes: 3 additions & 34 deletions Cargo.toml
@@ -1,34 +1,3 @@
[package]
name = "http-body"
# When releasing to crates.io:
# - Remove path dependencies
# - Update html_root_url.
# - Update doc url
# - Cargo.toml
# - README.md
# - Update CHANGELOG.md.
# - Create "vx.y.z" git tag.
version = "0.4.5"
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"
repository = "https://github.com/hyperium/http-body"
license = "MIT"
description = """
Trait representing an asynchronous, streaming, HTTP request or response body.
"""
keywords = ["http"]
categories = ["web-programming"]

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

[dev-dependencies]
tokio = { version = "1", features = ["macros", "rt"] }
[workspace]
members = ["http-body", "http-body-util"]
resolver = "2"
3 changes: 3 additions & 0 deletions http-body-util/CHANGELOG.md
@@ -0,0 +1,3 @@
# Unreleased

- Initial release, split from http-body 0.4.5.
35 changes: 35 additions & 0 deletions http-body-util/Cargo.toml
@@ -0,0 +1,35 @@
[package]
name = "http-body-util"
# When releasing to crates.io:
# - Remove path dependencies
# - Update html_root_url.
# - Update doc url
# - Cargo.toml
# - README.md
# - Update CHANGELOG.md.
# - Create "http-body-util-x.y.z" git tag.
version = "0.1.0"
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"]

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

[dev-dependencies]
tokio = { version = "1", features = ["macros", "rt"] }
@@ -1,5 +1,7 @@
use crate::Body;
use crate::BodyExt as _;

use bytes::Buf;
use http_body::{Body, SizeHint};
use std::{
fmt,
pin::Pin,
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
@@ -1,5 +1,5 @@
use crate::Body;
use bytes::Buf;
use http_body::Body;
use pin_project_lite::pin_project;
use std::{
any::type_name,
Expand Down
@@ -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 → http-body-util/src/empty.rs
@@ -1,6 +1,6 @@
use super::{Body, SizeHint};
use bytes::Buf;
use http::HeaderMap;
use http_body::{Body, SizeHint};
use std::{
convert::Infallible,
fmt,
Expand Down
2 changes: 1 addition & 1 deletion src/full.rs → http-body-util/src/full.rs
@@ -1,6 +1,6 @@
use crate::{Body, SizeHint};
use bytes::{Buf, Bytes};
use http::HeaderMap;
use http_body::{Body, SizeHint};
use pin_project_lite::pin_project;
use std::borrow::Cow;
use std::convert::{Infallible, TryFrom};
Expand Down
63 changes: 63 additions & 0 deletions http-body-util/src/lib.rs
@@ -0,0 +1,63 @@
#![deny(
missing_debug_implementations,
missing_docs,
unreachable_pub,
rustdoc::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;
mod limited;

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

/// 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 {}
2 changes: 1 addition & 1 deletion src/limited.rs → http-body-util/src/limited.rs
@@ -1,6 +1,6 @@
use crate::{Body, SizeHint};
use bytes::Buf;
use http::HeaderMap;
use http_body::{Body, SizeHint};
use pin_project_lite::pin_project;
use std::error::Error;
use std::fmt;
Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions http-body/Cargo.toml
@@ -0,0 +1,30 @@
[package]
name = "http-body"
# When releasing to crates.io:
# - Remove path dependencies
# - Update html_root_url.
# - Update doc url
# - Cargo.toml
# - README.md
# - Update CHANGELOG.md.
# - Create "http-body-x.y.z" git tag.
version = "0.4.5"
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"
repository = "https://github.com/hyperium/http-body"
license = "MIT"
description = """
Trait representing an asynchronous, streaming, HTTP request or response body.
"""
keywords = ["http"]
categories = ["web-programming"]

[dependencies]
bytes = "1"
http = "0.2"
46 changes: 1 addition & 45 deletions src/lib.rs → http-body/src/lib.rs
Expand Up @@ -3,7 +3,7 @@
missing_debug_implementations,
missing_docs,
unreachable_pub,
broken_intra_doc_links
rustdoc::broken_intra_doc_links
)]
#![cfg_attr(test, deny(warnings))]

Expand All @@ -13,21 +13,12 @@
//!
//! [`Body`]: trait.Body.html

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

pub mod combinators;

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

use self::combinators::{BoxBody, MapData, MapErr, UnsyncBoxBody};
use bytes::{Buf, Bytes};
use http::HeaderMap;
use std::convert::Infallible;
Expand Down Expand Up @@ -98,41 +89,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
File renamed without changes.
File renamed without changes.
5 changes: 2 additions & 3 deletions tests/is_end_stream.rs → http-body/tests/is_end_stream.rs
Expand Up @@ -70,9 +70,8 @@ fn is_end_stream_default_false() {
size_hint: SizeHint::default(),
};

assert_eq!(
false,
Pin::new(&mut mock).is_end_stream(),
assert!(
!Pin::new(&mut mock).is_end_stream(),
"size_hint = {:?}",
mock.size_hint.clone()
);
Expand Down

0 comments on commit aa90d1d

Please sign in to comment.