Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update bytes to v0.6 #2323

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ include = [
]

[dependencies]
bytes = "0.5"
bytes = "0.6"
futures-core = { version = "0.3", default-features = false }
futures-channel = "0.3"
futures-util = { version = "0.3", default-features = false }
Expand Down Expand Up @@ -64,7 +64,7 @@ tokio = { version = "0.3", features = [
"test-util",
] }
tokio-test = "0.3"
tokio-util = { version = "0.4", features = ["codec"] }
tokio-util = { version = "0.5", features = ["codec"] }
tower-util = "0.3"
url = "1.0"

Expand Down Expand Up @@ -244,3 +244,10 @@ required-features = ["runtime", "stream"]
name = "server"
path = "tests/server.rs"
required-features = ["runtime"]

[patch.crates-io]
http = { git = "https://github.com/olix0r/http", branch = "ver/bytes-0.6" }
http-body = { git = "https://github.com/olix0r/http-body", branch = "ver/bytes-0.6" }

[patch."https://github.com/hyperium/h2"]
h2 = { git = "https://github.com/olix0r/h2", branch = "ver/bytes-0.6" }
2 changes: 1 addition & 1 deletion examples/client_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#[macro_use]
extern crate serde_derive;

use bytes::buf::BufExt as _;
use bytes::Buf as _;
use hyper::Client;

// A simple type alias so as to DRY.
Expand Down
2 changes: 1 addition & 1 deletion examples/web_api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![deny(warnings)]

use bytes::buf::BufExt;
use bytes::Buf;
use futures_util::{stream, StreamExt};
use hyper::client::HttpConnector;
use hyper::service::{make_service_fn, service_fn};
Expand Down
2 changes: 1 addition & 1 deletion src/body/to_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ where
let second = if let Some(buf) = body.data().await {
buf?
} else {
return Ok(first.to_bytes());
return Ok(first.copy_to_bytes(first.remaining()));
};

// With more than 1 buf, we gotta flatten into a Vec first.
Expand Down
2 changes: 1 addition & 1 deletion src/proto/h1/encode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;
use std::io::IoSlice;

use bytes::buf::ext::{BufExt, Chain, Take};
use bytes::buf::{Chain, Take};
use bytes::Buf;

use super::io::WriteBuf;
Expand Down
7 changes: 6 additions & 1 deletion src/proto/h1/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::cell::Cell;
use std::cmp;
use std::fmt;
use std::io::{self, IoSlice};
use std::mem::MaybeUninit;

use bytes::{Buf, BufMut, Bytes, BytesMut};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
Expand Down Expand Up @@ -188,7 +189,11 @@ where
if self.read_buf_remaining_mut() < next {
self.read_buf.reserve(next);
}
let mut buf = ReadBuf::uninit(&mut self.read_buf.bytes_mut()[..]);
let dst = self.read_buf.bytes_mut();
// Safety: `dst` may include uninitialized memory, but `ReadBuf` will
// never uninitialize memory that has already been initialized.
let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit<u8>]) };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That line looks threatening..

Can you please add a comment saying what it is doing?

Copy link
Contributor Author

@olix0r olix0r Nov 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually know what it's doing ;)

I borrowed this from https://github.com/tokio-rs/tokio/blob/d78655337a68bded305782a8a8b4ac7be42aa6a7/tokio/src/io/util/read_buf.rs#L53-L55, which was the only updated use of ReadBuf::uninit I could find.

I would appreciate feedback from someone who understands these APIs better than I do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But let's see who introduced it 🙂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, added a comment summarizing that discussion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect 😄

let mut buf = ReadBuf::uninit(dst);
match Pin::new(&mut self.io).poll_read(cx, &mut buf) {
Comment on lines +192 to 197
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative to all of this (which avoids the scary-looking pointer cast that @Urhengulas was concerned about) is to just use tokio_util::io::poll_read_buf from the latest tokio-util here:

Suggested change
let dst = self.read_buf.bytes_mut();
// Safety: `dst` may include uninitialized memory, but `ReadBuf` will
// never uninitialize memory that has already been initialized.
let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit<u8>]) };
let mut buf = ReadBuf::uninit(dst);
match Pin::new(&mut self.io).poll_read(cx, &mut buf) {
match tokio_util::io::poll_read_buf(Pin::new(&mut self.io), cx, &mut self.read_buf) {

That would mean adding a tokio-util dependency, but it's already a transitive dependency via h2.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like a good solution!

And this is a useful function which can't be found in the docs... (code: https://github.com/tokio-rs/tokio/blob/master/tokio-util/src/lib.rs#L67-L134).

Poll::Ready(Ok(_)) => {
let n = buf.filled().len();
Expand Down