Skip to content

Commit

Permalink
io: remove zeroing for AsyncRead implementors (#2525)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikailBag authored May 21, 2020
1 parent 4f4f480 commit 1e54a35
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 1 deletion.
5 changes: 5 additions & 0 deletions tokio/src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,11 @@ impl File {
}

impl AsyncRead for File {
unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
// https://github.com/rust-lang/rust/blob/09c817eeb29e764cfc12d0a8d94841e3ffe34023/src/libstd/fs.rs#L668
false
}

fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/io/async_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub trait AsyncRead {
/// [`poll_read_buf`]: #method.poll_read_buf
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [MaybeUninit<u8>]) -> bool {
for x in buf {
*x.as_mut_ptr() = 0;
*x = MaybeUninit::new(0);
}

true
Expand Down
1 change: 1 addition & 0 deletions tokio/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ mod async_buf_read;
pub use self::async_buf_read::AsyncBufRead;

mod async_read;

pub use self::async_read::AsyncRead;

mod async_seek;
Expand Down
5 changes: 5 additions & 0 deletions tokio/src/io/stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ impl std::os::windows::io::AsRawHandle for Stdin {
}

impl AsyncRead for Stdin {
unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
// https://github.com/rust-lang/rust/blob/09c817eeb29e764cfc12d0a8d94841e3ffe34023/src/libstd/io/stdio.rs#L97
false
}

fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
9 changes: 9 additions & 0 deletions tokio/src/io/util/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ where
T: AsyncRead,
U: AsyncRead,
{
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
if self.first.prepare_uninitialized_buffer(buf) {
return true;
}
if self.second.prepare_uninitialized_buffer(buf) {
return true;
}
false
}
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
3 changes: 3 additions & 0 deletions tokio/src/io/util/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ cfg_io_util! {
}

impl AsyncRead for Empty {
unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
false
}
#[inline]
fn poll_read(
self: Pin<&mut Self>,
Expand Down
3 changes: 3 additions & 0 deletions tokio/src/io/util/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ cfg_io_util! {
}

impl AsyncRead for Repeat {
unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
false
}
#[inline]
fn poll_read(
self: Pin<&mut Self>,
Expand Down
10 changes: 10 additions & 0 deletions tokio/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,11 @@ impl AsyncWrite for ChildStdin {
}

impl AsyncRead for ChildStdout {
unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
// https://github.com/rust-lang/rust/blob/09c817eeb29e764cfc12d0a8d94841e3ffe34023/src/libstd/process.rs#L314
false
}

fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand All @@ -889,6 +894,11 @@ impl AsyncRead for ChildStdout {
}

impl AsyncRead for ChildStderr {
unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
// https://github.com/rust-lang/rust/blob/09c817eeb29e764cfc12d0a8d94841e3ffe34023/src/libstd/process.rs#L375
false
}

fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down

0 comments on commit 1e54a35

Please sign in to comment.