Skip to content

Commit

Permalink
rename body to recv temporarily
Browse files Browse the repository at this point in the history
"""
We'll eventually want to bikshed the name Recv, but to free up the name Body for #2839, this can be done quickly.
"""

Resolve #2963
  • Loading branch information
Michael-J-Ward committed Aug 25, 2022
1 parent 9e8fc8f commit 7039392
Show file tree
Hide file tree
Showing 29 changed files with 155 additions and 155 deletions.
4 changes: 2 additions & 2 deletions examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full};
use hyper::body::HttpBody as _;
use hyper::server::conn::Http;
use hyper::service::service_fn;
use hyper::{Body, Method, Request, Response, StatusCode};
use hyper::{Recv, Method, Request, Response, StatusCode};
use tokio::net::TcpListener;

/// This is our service handler. It receives a Request, routes on its
/// path, and returns a Future of a Response.
async fn echo(req: Request<Body>) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
async fn echo(req: Request<Recv>) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
match (req.method(), req.uri().path()) {
// Serve some instructions at /
(&Method::GET, "/") => Ok(Response::new(full(
Expand Down
4 changes: 2 additions & 2 deletions examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use bytes::Bytes;
use http_body_util::Full;
use hyper::server::conn::Http;
use hyper::service::service_fn;
use hyper::{Body, Request, Response};
use hyper::{Recv, Request, Response};
use tokio::net::TcpListener;

async fn hello(_: Request<Body>) -> Result<Response<Full<Bytes>>, Infallible> {
async fn hello(_: Request<Recv>) -> Result<Response<Full<Bytes>>, Infallible> {
Ok(Response::new(Full::new(Bytes::from("Hello World!"))))
}

Expand Down
4 changes: 2 additions & 2 deletions examples/http_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use hyper::client::conn::Builder;
use hyper::server::conn::Http;
use hyper::service::service_fn;
use hyper::upgrade::Upgraded;
use hyper::{Body, Method, Request, Response};
use hyper::{Recv, Method, Request, Response};

use tokio::net::{TcpListener, TcpStream};

Expand Down Expand Up @@ -43,7 +43,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}

async fn proxy(req: Request<Body>) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
async fn proxy(req: Request<Recv>) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
println!("req: {:?}", req);

if Method::CONNECT == req.method() {
Expand Down
6 changes: 3 additions & 3 deletions examples/multi_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ use futures_util::future::join;
use http_body_util::Full;
use hyper::server::conn::Http;
use hyper::service::service_fn;
use hyper::{Body, Request, Response};
use hyper::{Recv, Request, Response};
use tokio::net::TcpListener;

static INDEX1: &[u8] = b"The 1st service!";
static INDEX2: &[u8] = b"The 2nd service!";

async fn index1(_: Request<Body>) -> Result<Response<Full<Bytes>>, hyper::Error> {
async fn index1(_: Request<Recv>) -> Result<Response<Full<Bytes>>, hyper::Error> {
Ok(Response::new(Full::new(Bytes::from(INDEX1))))
}

async fn index2(_: Request<Body>) -> Result<Response<Full<Bytes>>, hyper::Error> {
async fn index2(_: Request<Recv>) -> Result<Response<Full<Bytes>>, hyper::Error> {
Ok(Response::new(Full::new(Bytes::from(INDEX2))))
}

Expand Down
4 changes: 2 additions & 2 deletions examples/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bytes::Bytes;
use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full};
use hyper::server::conn::Http;
use hyper::service::service_fn;
use hyper::{Body, Method, Request, Response, StatusCode};
use hyper::{Recv, Method, Request, Response, StatusCode};
use tokio::net::TcpListener;

use std::collections::HashMap;
Expand All @@ -19,7 +19,7 @@ static NOTNUMERIC: &[u8] = b"Number field is not numeric";

// Using service_fn, we can turn this function into a `Service`.
async fn param_example(
req: Request<Body>,
req: Request<Recv>,
) -> Result<Response<BoxBody<Bytes, Infallible>>, hyper::Error> {
match (req.method(), req.uri().path()) {
(&Method::GET, "/") | (&Method::GET, "/post") => Ok(Response::new(full(INDEX))),
Expand Down
4 changes: 2 additions & 2 deletions examples/send_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio::net::TcpListener;
use bytes::Bytes;
use http_body_util::Full;
use hyper::service::service_fn;
use hyper::{Body, Method, Request, Response, Result, StatusCode};
use hyper::{Recv, Method, Request, Response, Result, StatusCode};

static INDEX: &str = "examples/send_file_index.html";
static NOTFOUND: &[u8] = b"Not Found";
Expand Down Expand Up @@ -36,7 +36,7 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
}
}

async fn response_examples(req: Request<Body>) -> Result<Response<Full<Bytes>>> {
async fn response_examples(req: Request<Recv>) -> Result<Response<Full<Bytes>>> {
match (req.method(), req.uri().path()) {
(&Method::GET, "/") | (&Method::GET, "/index.html") => simple_file_send(INDEX).await,
(&Method::GET, "/no_file.html") => {
Expand Down
6 changes: 3 additions & 3 deletions examples/service_struct_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bytes::Bytes;
use http_body_util::Full;
use hyper::server::conn::Http;
use hyper::service::Service;
use hyper::{Body, Request, Response};
use hyper::{Recv, Request, Response};
use tokio::net::TcpListener;

use std::future::Future;
Expand Down Expand Up @@ -37,7 +37,7 @@ struct Svc {
counter: Counter,
}

impl Service<Request<Body>> for Svc {
impl Service<Request<Recv>> for Svc {
type Response = Response<Full<Bytes>>;
type Error = hyper::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
Expand All @@ -46,7 +46,7 @@ impl Service<Request<Body>> for Svc {
Poll::Ready(Ok(()))
}

fn call(&mut self, req: Request<Body>) -> Self::Future {
fn call(&mut self, req: Request<Recv>) -> Self::Future {
fn mk_response(s: String) -> Result<Response<Full<Bytes>>, hyper::Error> {
Ok(Response::builder().body(Full::new(Bytes::from(s))).unwrap())
}
Expand Down
4 changes: 2 additions & 2 deletions examples/upgrades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use hyper::header::{HeaderValue, UPGRADE};
use hyper::server::conn::Http;
use hyper::service::service_fn;
use hyper::upgrade::Upgraded;
use hyper::{Body, Request, Response, StatusCode};
use hyper::{Recv, Request, Response, StatusCode};

// A simple type alias so as to DRY.
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
Expand All @@ -38,7 +38,7 @@ async fn server_upgraded_io(mut upgraded: Upgraded) -> Result<()> {
}

/// Our server HTTP handler to initiate HTTP upgrades.
async fn server_upgrade(mut req: Request<Body>) -> Result<Response<Empty<Bytes>>> {
async fn server_upgrade(mut req: Request<Recv>) -> Result<Response<Empty<Bytes>>> {
let mut res = Response::new(Empty::new());

// Send a 400 to any request that doesn't have
Expand Down
6 changes: 3 additions & 3 deletions examples/web_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bytes::{Buf, Bytes};
use http_body_util::{BodyExt, Full};
use hyper::server::conn::Http;
use hyper::service::service_fn;
use hyper::{header, Body, Method, Request, Response, StatusCode};
use hyper::{header, Recv, Method, Request, Response, StatusCode};
use tokio::net::{TcpListener, TcpStream};

type GenericError = Box<dyn std::error::Error + Send + Sync>;
Expand Down Expand Up @@ -46,7 +46,7 @@ async fn client_request_response() -> Result<Response<BoxBody>> {
Ok(Response::new(res_body))
}

async fn api_post_response(req: Request<Body>) -> Result<Response<BoxBody>> {
async fn api_post_response(req: Request<Recv>) -> Result<Response<BoxBody>> {
// Aggregate the body...
let whole_body = hyper::body::aggregate(req).await?;
// Decode as JSON...
Expand Down Expand Up @@ -77,7 +77,7 @@ async fn api_get_response() -> Result<Response<BoxBody>> {
Ok(res)
}

async fn response_examples(req: Request<Body>) -> Result<Response<BoxBody>> {
async fn response_examples(req: Request<Recv>) -> Result<Response<BoxBody>> {
match (req.method(), req.uri().path()) {
(&Method::GET, "/") | (&Method::GET, "/index.html") => Ok(Response::new(full(INDEX))),
(&Method::GET, "/test.html") => client_request_response().await,
Expand Down
56 changes: 28 additions & 28 deletions src/body/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type TrailersSender = oneshot::Sender<HeaderMap>;
/// Note: To read the full body, use [`body::to_bytes`](crate::body::to_bytes)
/// or [`body::aggregate`](crate::body::aggregate).
#[must_use = "streams do nothing unless polled"]
pub struct Body {
pub struct Recv {
kind: Kind,
}

Expand Down Expand Up @@ -70,16 +70,16 @@ pub struct Sender {
const WANT_PENDING: usize = 1;
const WANT_READY: usize = 2;

impl Body {
impl Recv {
/// Create a `Body` stream with an associated sender half.
///
/// Useful when wanting to stream chunks from another thread.
#[inline]
pub fn channel() -> (Sender, Body) {
pub fn channel() -> (Sender, Recv) {
Self::new_channel(DecodedLength::CHUNKED, /*wanter =*/ false)
}

pub(crate) fn new_channel(content_length: DecodedLength, wanter: bool) -> (Sender, Body) {
pub(crate) fn new_channel(content_length: DecodedLength, wanter: bool) -> (Sender, Recv) {
let (data_tx, data_rx) = mpsc::channel(0);
let (trailers_tx, trailers_rx) = oneshot::channel();

Expand All @@ -94,7 +94,7 @@ impl Body {
data_tx,
trailers_tx: Some(trailers_tx),
};
let rx = Body::new(Kind::Chan {
let rx = Recv::new(Kind::Chan {
content_length,
want_tx,
data_rx,
Expand All @@ -104,18 +104,18 @@ impl Body {
(tx, rx)
}

fn new(kind: Kind) -> Body {
Body { kind }
fn new(kind: Kind) -> Recv {
Recv { kind }
}

#[allow(dead_code)]
pub(crate) fn empty() -> Body {
Body::new(Kind::Empty)
pub(crate) fn empty() -> Recv {
Recv::new(Kind::Empty)
}

#[cfg(feature = "ffi")]
pub(crate) fn ffi() -> Body {
Body::new(Kind::Ffi(crate::ffi::UserBody::new()))
pub(crate) fn ffi() -> Recv {
Recv::new(Kind::Ffi(crate::ffi::UserBody::new()))
}

#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
Expand All @@ -129,7 +129,7 @@ impl Body {
if !content_length.is_exact() && recv.is_end_stream() {
content_length = DecodedLength::ZERO;
}
let body = Body::new(Kind::H2 {
let body = Recv::new(Kind::H2 {
ping,
content_length,
recv,
Expand Down Expand Up @@ -194,7 +194,7 @@ impl Body {
}
}

impl HttpBody for Body {
impl HttpBody for Recv {
type Data = Bytes;
type Error = crate::Error;

Expand Down Expand Up @@ -270,7 +270,7 @@ impl HttpBody for Body {
}
}

impl fmt::Debug for Body {
impl fmt::Debug for Recv {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[derive(Debug)]
struct Streaming;
Expand Down Expand Up @@ -382,14 +382,14 @@ mod tests {
use std::mem;
use std::task::Poll;

use super::{Body, DecodedLength, HttpBody, Sender, SizeHint};
use super::{Recv, DecodedLength, HttpBody, Sender, SizeHint};

#[test]
fn test_size_of() {
// These are mostly to help catch *accidentally* increasing
// the size by too much.

let body_size = mem::size_of::<Body>();
let body_size = mem::size_of::<Recv>();
let body_expected_size = mem::size_of::<u64>() * 6;
assert!(
body_size <= body_expected_size,
Expand All @@ -398,7 +398,7 @@ mod tests {
body_expected_size,
);

assert_eq!(body_size, mem::size_of::<Option<Body>>(), "Option<Body>");
assert_eq!(body_size, mem::size_of::<Option<Recv>>(), "Option<Body>");

assert_eq!(
mem::size_of::<Sender>(),
Expand All @@ -415,18 +415,18 @@ mod tests {

#[test]
fn size_hint() {
fn eq(body: Body, b: SizeHint, note: &str) {
fn eq(body: Recv, b: SizeHint, note: &str) {
let a = body.size_hint();
assert_eq!(a.lower(), b.lower(), "lower for {:?}", note);
assert_eq!(a.upper(), b.upper(), "upper for {:?}", note);
}

eq(Body::empty(), SizeHint::with_exact(0), "empty");
eq(Recv::empty(), SizeHint::with_exact(0), "empty");

eq(Body::channel().1, SizeHint::new(), "channel");
eq(Recv::channel().1, SizeHint::new(), "channel");

eq(
Body::new_channel(DecodedLength::new(4), /*wanter =*/ false).1,
Recv::new_channel(DecodedLength::new(4), /*wanter =*/ false).1,
SizeHint::with_exact(4),
"channel with length",
);
Expand All @@ -435,7 +435,7 @@ mod tests {
#[cfg(not(miri))]
#[tokio::test]
async fn channel_abort() {
let (tx, mut rx) = Body::channel();
let (tx, mut rx) = Recv::channel();

tx.abort();

Expand All @@ -446,7 +446,7 @@ mod tests {
#[cfg(not(miri))]
#[tokio::test]
async fn channel_abort_when_buffer_is_full() {
let (mut tx, mut rx) = Body::channel();
let (mut tx, mut rx) = Recv::channel();

tx.try_send_data("chunk 1".into()).expect("send 1");
// buffer is full, but can still send abort
Expand All @@ -461,7 +461,7 @@ mod tests {

#[test]
fn channel_buffers_one() {
let (mut tx, _rx) = Body::channel();
let (mut tx, _rx) = Recv::channel();

tx.try_send_data("chunk 1".into()).expect("send 1");

Expand All @@ -473,14 +473,14 @@ mod tests {
#[cfg(not(miri))]
#[tokio::test]
async fn channel_empty() {
let (_, mut rx) = Body::channel();
let (_, mut rx) = Recv::channel();

assert!(rx.data().await.is_none());
}

#[test]
fn channel_ready() {
let (mut tx, _rx) = Body::new_channel(DecodedLength::CHUNKED, /*wanter = */ false);
let (mut tx, _rx) = Recv::new_channel(DecodedLength::CHUNKED, /*wanter = */ false);

let mut tx_ready = tokio_test::task::spawn(tx.ready());

Expand All @@ -489,7 +489,7 @@ mod tests {

#[test]
fn channel_wanter() {
let (mut tx, mut rx) = Body::new_channel(DecodedLength::CHUNKED, /*wanter = */ true);
let (mut tx, mut rx) = Recv::new_channel(DecodedLength::CHUNKED, /*wanter = */ true);

let mut tx_ready = tokio_test::task::spawn(tx.ready());
let mut rx_data = tokio_test::task::spawn(rx.data());
Expand All @@ -510,7 +510,7 @@ mod tests {

#[test]
fn channel_notices_closure() {
let (mut tx, rx) = Body::new_channel(DecodedLength::CHUNKED, /*wanter = */ true);
let (mut tx, rx) = Recv::new_channel(DecodedLength::CHUNKED, /*wanter = */ true);

let mut tx_ready = tokio_test::task::spawn(tx.ready());

Expand Down
6 changes: 3 additions & 3 deletions src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use http_body::Body as HttpBody;
pub use http_body::SizeHint;

pub use self::aggregate::aggregate;
pub use self::body::{Body, Sender};
pub use self::body::{Recv, Sender};
pub(crate) use self::length::DecodedLength;
pub use self::to_bytes::to_bytes;

Expand All @@ -33,6 +33,6 @@ fn _assert_send_sync() {
fn _assert_send<T: Send>() {}
fn _assert_sync<T: Sync>() {}

_assert_send::<Body>();
_assert_sync::<Body>();
_assert_send::<Recv>();
_assert_sync::<Recv>();
}
Loading

0 comments on commit 7039392

Please sign in to comment.