Skip to content

Commit 3965d91

Browse files
authored
util: update to bytes 0.6 (#3071)
Copies the implementation of poll_read_buf() from tokio::io::util::read_buf.
1 parent a3ef4e4 commit 3965d91

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

examples/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ tokio = { version = "0.3.0", path = "../tokio", features = ["full", "tracing"] }
1111
tracing = "0.1"
1212
tracing-subscriber = { version = "0.2.7", default-features = false, features = ["fmt", "ansi", "env-filter", "chrono", "tracing-log"] }
1313
tokio-util = { version = "0.4.0", path = "../tokio-util", features = ["full"] }
14-
bytes = "0.5"
14+
bytes = "0.6"
1515
futures = "0.3.0"
1616
http = "0.2"
1717
serde = "1.0"

tokio-util/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ rt = ["tokio/rt"]
3535
[dependencies]
3636
tokio = { version = "0.3.0", path = "../tokio" }
3737

38-
bytes = "0.5.0"
38+
bytes = "0.6.0"
3939
futures-core = "0.3.0"
4040
futures-sink = "0.3.0"
4141
futures-io = { version = "0.3.0", optional = true }

tokio-util/src/lib.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ mod util {
6565
use bytes::BufMut;
6666
use futures_core::ready;
6767
use std::io;
68+
use std::mem::MaybeUninit;
6869
use std::pin::Pin;
6970
use std::task::{Context, Poll};
7071

@@ -77,17 +78,24 @@ mod util {
7778
return Poll::Ready(Ok(0));
7879
}
7980

80-
let orig = buf.bytes_mut().as_ptr() as *const u8;
81-
let mut b = ReadBuf::uninit(buf.bytes_mut());
81+
let n = {
82+
let dst = buf.bytes_mut();
83+
let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit<u8>]) };
84+
let mut buf = ReadBuf::uninit(dst);
85+
let ptr = buf.filled().as_ptr();
86+
ready!(io.poll_read(cx, &mut buf)?);
8287

83-
ready!(io.poll_read(cx, &mut b))?;
84-
let n = b.filled().len();
88+
// Ensure the pointer does not change from under us
89+
assert_eq!(ptr, buf.filled().as_ptr());
90+
buf.filled().len()
91+
};
8592

86-
// Safety: we can assume `n` bytes were read, since they are in`filled`.
87-
assert_eq!(orig, b.filled().as_ptr());
93+
// Safety: This is guaranteed to be the number of initialized (and read)
94+
// bytes due to the invariants provided by `ReadBuf::filled`.
8895
unsafe {
8996
buf.advance_mut(n);
9097
}
98+
9199
Poll::Ready(Ok(n))
92100
}
93101
}

0 commit comments

Comments
 (0)