From 6a21decae2a8e0b13742cd80b453b90cf4009377 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 12 May 2020 12:53:49 +0300 Subject: [PATCH 01/18] Fix unnecessary zeroing for AsyncRead implementors --- tokio/src/fs/file.rs | 6 ++++++ tokio/src/io/async_read.rs | 22 ++++++++++++++++++++++ tokio/src/io/blocking.rs | 4 ++++ tokio/src/io/mod.rs | 1 + tokio/src/io/poll_evented.rs | 4 ++++ tokio/src/io/stdin.rs | 5 +++++ tokio/src/io/util/chain.rs | 9 +++++++++ tokio/src/io/util/empty.rs | 3 +++ tokio/src/io/util/repeat.rs | 3 +++ tokio/src/process/mod.rs | 10 ++++++++++ 10 files changed, 67 insertions(+) diff --git a/tokio/src/fs/file.rs b/tokio/src/fs/file.rs index cc4a187d78f..b45d2f2445c 100644 --- a/tokio/src/fs/file.rs +++ b/tokio/src/fs/file.rs @@ -537,6 +537,12 @@ impl File { } impl AsyncRead for File { + unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [std::mem::MaybeUninit]) -> bool { + // std::fs::File does not need zeroing + // 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<'_>, diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index cc9091c99c1..5e94a4fb1cd 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -5,6 +5,28 @@ use std::ops::DerefMut; use std::pin::Pin; use std::task::{Context, Poll}; +/// This utility function shoyld be used to override `AsyncRead::prepare_uninitialized_buffer` +/// for any AsyncRead impl which wraps an `std::io::Read` instance. +pub(crate) fn prepare_uninitialized_buffer_std_read( + buf: &mut [MaybeUninit], +) -> bool { + // TODO: when std::io::Initializer is stable, it should be used + // to override `prepare_uninitialized_buffer` + /*use std::io::Read; + if !T::initializer::should_initialize() { + return false; + } + */ + for x in buf { + // we could use safe method x.write(0) here, but it is unstable. + // Safety: as_mut_ptr() returns valid writeable pointer. + unsafe { + x.as_mut_ptr().write(0); + } + } + true +} + /// Reads bytes from a source. /// /// This trait is analogous to the [`std::io::Read`] trait, but integrates with diff --git a/tokio/src/io/blocking.rs b/tokio/src/io/blocking.rs index 2491039a3f3..ff4445e76d0 100644 --- a/tokio/src/io/blocking.rs +++ b/tokio/src/io/blocking.rs @@ -50,6 +50,10 @@ impl AsyncRead for Blocking where T: Read + Unpin + Send + 'static, { + unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit]) -> bool { + crate::io::prepare_uninitialized_buffer_std_read::(buf) + } + fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index 29d8bc5554e..59e278fd42b 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -163,6 +163,7 @@ pub use self::async_buf_read::AsyncBufRead; mod async_read; pub use self::async_read::AsyncRead; +pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; mod async_seek; pub use self::async_seek::AsyncSeek; diff --git a/tokio/src/io/poll_evented.rs b/tokio/src/io/poll_evented.rs index f8be6f2f755..c02302b4cb0 100644 --- a/tokio/src/io/poll_evented.rs +++ b/tokio/src/io/poll_evented.rs @@ -381,6 +381,10 @@ impl AsyncRead for PollEvented where E: Evented + Read + Unpin, { + unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit]) -> bool { + crate::io::prepare_uninitialized_buffer_std_read::(buf) + } + fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/tokio/src/io/stdin.rs b/tokio/src/io/stdin.rs index d986d3abeaf..325b8757ec1 100644 --- a/tokio/src/io/stdin.rs +++ b/tokio/src/io/stdin.rs @@ -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]) -> 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<'_>, diff --git a/tokio/src/io/util/chain.rs b/tokio/src/io/util/chain.rs index bc76af341da..8ba9194f5de 100644 --- a/tokio/src/io/util/chain.rs +++ b/tokio/src/io/util/chain.rs @@ -84,6 +84,15 @@ where T: AsyncRead, U: AsyncRead, { + unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit]) -> 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<'_>, diff --git a/tokio/src/io/util/empty.rs b/tokio/src/io/util/empty.rs index 121102c78f2..576058d52d1 100644 --- a/tokio/src/io/util/empty.rs +++ b/tokio/src/io/util/empty.rs @@ -47,6 +47,9 @@ cfg_io_util! { } impl AsyncRead for Empty { + unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [std::mem::MaybeUninit]) -> bool { + false + } #[inline] fn poll_read( self: Pin<&mut Self>, diff --git a/tokio/src/io/util/repeat.rs b/tokio/src/io/util/repeat.rs index 6b9067e8534..eeef7cc187b 100644 --- a/tokio/src/io/util/repeat.rs +++ b/tokio/src/io/util/repeat.rs @@ -47,6 +47,9 @@ cfg_io_util! { } impl AsyncRead for Repeat { + unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [std::mem::MaybeUninit]) -> bool { + false + } #[inline] fn poll_read( self: Pin<&mut Self>, diff --git a/tokio/src/process/mod.rs b/tokio/src/process/mod.rs index 7231511235e..f32b8cbe3aa 100644 --- a/tokio/src/process/mod.rs +++ b/tokio/src/process/mod.rs @@ -872,6 +872,11 @@ impl AsyncWrite for ChildStdin { } impl AsyncRead for ChildStdout { + unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [std::mem::MaybeUninit]) -> 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<'_>, @@ -882,6 +887,11 @@ impl AsyncRead for ChildStdout { } impl AsyncRead for ChildStderr { + unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [std::mem::MaybeUninit]) -> 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<'_>, From 572d4436fba8d386ba5c0f7887d8327c8fb5a19b Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 12 May 2020 14:22:44 +0300 Subject: [PATCH 02/18] Less unsafe --- tokio/src/io/async_read.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index 5e94a4fb1cd..8b8e2023717 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -18,11 +18,7 @@ pub(crate) fn prepare_uninitialized_buffer_std_read( } */ for x in buf { - // we could use safe method x.write(0) here, but it is unstable. - // Safety: as_mut_ptr() returns valid writeable pointer. - unsafe { - x.as_mut_ptr().write(0); - } + *x = MaybeUninit::new(0); } true } @@ -98,7 +94,7 @@ pub trait AsyncRead { /// [`poll_read_buf`]: #method.poll_read_buf unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [MaybeUninit]) -> bool { for x in buf { - *x.as_mut_ptr() = 0; + *x = MaybeUninit::new(0); } true From 70ed7669a4f97a1663319ea1bcda5d7856184ecf Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 12 May 2020 14:29:10 +0300 Subject: [PATCH 03/18] Fix typo --- tokio/src/io/async_read.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index 8b8e2023717..5cf5faa7e69 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -5,7 +5,7 @@ use std::ops::DerefMut; use std::pin::Pin; use std::task::{Context, Poll}; -/// This utility function shoyld be used to override `AsyncRead::prepare_uninitialized_buffer` +/// This utility function should be used to override `AsyncRead::prepare_uninitialized_buffer` /// for any AsyncRead impl which wraps an `std::io::Read` instance. pub(crate) fn prepare_uninitialized_buffer_std_read( buf: &mut [MaybeUninit], From 1bd23485d09358c90ee46963e8213981a180ae8c Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 12 May 2020 18:23:00 +0300 Subject: [PATCH 04/18] try to fix features --- tokio/src/io/async_read.rs | 1 + tokio/src/io/mod.rs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index 5cf5faa7e69..93a767b446b 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -7,6 +7,7 @@ use std::task::{Context, Poll}; /// This utility function should be used to override `AsyncRead::prepare_uninitialized_buffer` /// for any AsyncRead impl which wraps an `std::io::Read` instance. +#[cfg(any(feature = "io-std", features = "fs"))] pub(crate) fn prepare_uninitialized_buffer_std_read( buf: &mut [MaybeUninit], ) -> bool { diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index 59e278fd42b..9e798f969ad 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -162,8 +162,9 @@ mod async_buf_read; pub use self::async_buf_read::AsyncBufRead; mod async_read; -pub use self::async_read::AsyncRead; +#[cfg(any(feature = "io-std", features = "fs"))] pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; +pub use self::async_read::AsyncRead; mod async_seek; pub use self::async_seek::AsyncSeek; From a00fc0c0e228e2b22254616d45b0002bb88033ac Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Wed, 13 May 2020 19:00:43 +0300 Subject: [PATCH 05/18] Expose prepare_uninitialized_buffer_std_read --- tokio/src/io/async_read.rs | 3 +-- tokio/src/io/mod.rs | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index 93a767b446b..ef26c10b6f3 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -7,8 +7,7 @@ use std::task::{Context, Poll}; /// This utility function should be used to override `AsyncRead::prepare_uninitialized_buffer` /// for any AsyncRead impl which wraps an `std::io::Read` instance. -#[cfg(any(feature = "io-std", features = "fs"))] -pub(crate) fn prepare_uninitialized_buffer_std_read( +pub fn prepare_uninitialized_buffer_std_read( buf: &mut [MaybeUninit], ) -> bool { // TODO: when std::io::Initializer is stable, it should be used diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index 9e798f969ad..489688f899a 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -162,8 +162,7 @@ mod async_buf_read; pub use self::async_buf_read::AsyncBufRead; mod async_read; -#[cfg(any(feature = "io-std", features = "fs"))] -pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; +pub use self::async_read::prepare_uninitialized_buffer_std_read; pub use self::async_read::AsyncRead; mod async_seek; From 28ad6205a3b8f71ccab10f2377b52a283f99474a Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Wed, 13 May 2020 19:13:42 +0300 Subject: [PATCH 06/18] Fix bug --- tokio/src/io/util/chain.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tokio/src/io/util/chain.rs b/tokio/src/io/util/chain.rs index 8ba9194f5de..fd33b682dcf 100644 --- a/tokio/src/io/util/chain.rs +++ b/tokio/src/io/util/chain.rs @@ -91,6 +91,9 @@ where if self.second.prepare_uninitialized_buffer(buf) { return true; } + for x in buf { + *x = std::mem::MaybeUninit::new(0); + } false } fn poll_read( From 87169e4179f1102b42114ee091c270783e49bd0b Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Wed, 13 May 2020 19:17:12 +0300 Subject: [PATCH 07/18] Cleanup --- tokio/src/fs/file.rs | 1 - tokio/src/io/async_read.rs | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tokio/src/fs/file.rs b/tokio/src/fs/file.rs index b45d2f2445c..f3bc98546a9 100644 --- a/tokio/src/fs/file.rs +++ b/tokio/src/fs/file.rs @@ -538,7 +538,6 @@ impl File { impl AsyncRead for File { unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [std::mem::MaybeUninit]) -> bool { - // std::fs::File does not need zeroing // https://github.com/rust-lang/rust/blob/09c817eeb29e764cfc12d0a8d94841e3ffe34023/src/libstd/fs.rs#L668 false } diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index ef26c10b6f3..af70d83a428 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -5,7 +5,9 @@ use std::ops::DerefMut; use std::pin::Pin; use std::task::{Context, Poll}; -/// This utility function should be used to override `AsyncRead::prepare_uninitialized_buffer` +/// Implements `AsyncRead::prepare_uninitialized_buffer` in a such a way +/// that is valid for `R` requirements. +/// Should be used to override `AsyncRead::prepare_uninitialized_buffer` /// for any AsyncRead impl which wraps an `std::io::Read` instance. pub fn prepare_uninitialized_buffer_std_read( buf: &mut [MaybeUninit], From 3c153aa80acd3e93faa7449c060b16caadf59c1b Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 19 May 2020 17:46:52 +0300 Subject: [PATCH 08/18] [fixup] unpub, attempt 1 --- tokio/src/io/async_read.rs | 4 +++- tokio/src/io/mod.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index af70d83a428..fe9c016d18b 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -9,7 +9,9 @@ use std::task::{Context, Poll}; /// that is valid for `R` requirements. /// Should be used to override `AsyncRead::prepare_uninitialized_buffer` /// for any AsyncRead impl which wraps an `std::io::Read` instance. -pub fn prepare_uninitialized_buffer_std_read( +// cfg-io-std +#[cfg(feature = "io-std")] +pub(crate) fn prepare_uninitialized_buffer_std_read( buf: &mut [MaybeUninit], ) -> bool { // TODO: when std::io::Initializer is stable, it should be used diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index 489688f899a..53bcdbcb554 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -162,7 +162,7 @@ mod async_buf_read; pub use self::async_buf_read::AsyncBufRead; mod async_read; -pub use self::async_read::prepare_uninitialized_buffer_std_read; +pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; pub use self::async_read::AsyncRead; mod async_seek; From 777597a6e59e882ecf6586b40ef4fabba4327e97 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 19 May 2020 17:51:03 +0300 Subject: [PATCH 09/18] attempt=2 --- tokio/src/io/async_read.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index fe9c016d18b..204093f9742 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -10,7 +10,7 @@ use std::task::{Context, Poll}; /// Should be used to override `AsyncRead::prepare_uninitialized_buffer` /// for any AsyncRead impl which wraps an `std::io::Read` instance. // cfg-io-std -#[cfg(feature = "io-std")] +#[cfg(any(feature = "io-std", feature = "fs"))] pub(crate) fn prepare_uninitialized_buffer_std_read( buf: &mut [MaybeUninit], ) -> bool { From caa170d5f6cf1f872f903ea8d25631c036268f45 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 19 May 2020 17:56:03 +0300 Subject: [PATCH 10/18] attempt=3 --- tokio/src/io/async_read.rs | 2 +- tokio/src/io/mod.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index 204093f9742..91432016b9e 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -10,7 +10,7 @@ use std::task::{Context, Poll}; /// Should be used to override `AsyncRead::prepare_uninitialized_buffer` /// for any AsyncRead impl which wraps an `std::io::Read` instance. // cfg-io-std -#[cfg(any(feature = "io-std", feature = "fs"))] +//#[cfg(any(feature = "io-std", feature = "fs"))] pub(crate) fn prepare_uninitialized_buffer_std_read( buf: &mut [MaybeUninit], ) -> bool { diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index 53bcdbcb554..6abc03abf08 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -162,6 +162,7 @@ mod async_buf_read; pub use self::async_buf_read::AsyncBufRead; mod async_read; +#[cfg(any(feature = "io-std", feature = "fs"))] pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; pub use self::async_read::AsyncRead; From a256072c753d769774e352d79924399c4019503d Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 19 May 2020 18:13:01 +0300 Subject: [PATCH 11/18] attempt=4 --- tokio/src/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index 6abc03abf08..2dfa3a79070 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -162,7 +162,7 @@ mod async_buf_read; pub use self::async_buf_read::AsyncBufRead; mod async_read; -#[cfg(any(feature = "io-std", feature = "fs"))] +#[cfg(any(feature = "io-std", feature = "fs", test))] pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; pub use self::async_read::AsyncRead; From b6344988cc329b2d12c8f75fda81619a8e37203a Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 19 May 2020 18:25:49 +0300 Subject: [PATCH 12/18] attempt=5 --- tokio/src/io/async_read.rs | 40 +++++++++++++++++++------------------- tokio/src/io/mod.rs | 5 +++-- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index 91432016b9e..b43a86d9d5a 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -4,29 +4,29 @@ use std::mem::MaybeUninit; use std::ops::DerefMut; use std::pin::Pin; use std::task::{Context, Poll}; - -/// Implements `AsyncRead::prepare_uninitialized_buffer` in a such a way -/// that is valid for `R` requirements. -/// Should be used to override `AsyncRead::prepare_uninitialized_buffer` -/// for any AsyncRead impl which wraps an `std::io::Read` instance. -// cfg-io-std -//#[cfg(any(feature = "io-std", feature = "fs"))] -pub(crate) fn prepare_uninitialized_buffer_std_read( - buf: &mut [MaybeUninit], -) -> bool { - // TODO: when std::io::Initializer is stable, it should be used - // to override `prepare_uninitialized_buffer` - /*use std::io::Read; - if !T::initializer::should_initialize() { - return false; - } - */ - for x in buf { - *x = MaybeUninit::new(0); +cfg_io_blocking! { + /// Implements `AsyncRead::prepare_uninitialized_buffer` in a such a way + /// that is valid for `R` requirements. + /// Should be used to override `AsyncRead::prepare_uninitialized_buffer` + /// for any AsyncRead impl which wraps an `std::io::Read` instance. + pub(crate) fn prepare_uninitialized_buffer_std_read( + buf: &mut [MaybeUninit], + ) -> bool { + // TODO: when std::io::Initializer is stable, it should be used + // to override `prepare_uninitialized_buffer` + /*use std::io::Read; + if !T::initializer::should_initialize() { + return false; + } + */ + for x in buf { + *x = MaybeUninit::new(0); + } + true } - true } + /// Reads bytes from a source. /// /// This trait is analogous to the [`std::io::Read`] trait, but integrates with diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index 2dfa3a79070..057d412e4f1 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -162,8 +162,9 @@ mod async_buf_read; pub use self::async_buf_read::AsyncBufRead; mod async_read; -#[cfg(any(feature = "io-std", feature = "fs", test))] -pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; +cfg_io_blocking! { + pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; +} pub use self::async_read::AsyncRead; mod async_seek; From 2cb3ea8ef7eb827f022c67188af247d5b58fe0d9 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 21 May 2020 16:24:44 +0300 Subject: [PATCH 13/18] allow(dead_code) --- tokio/src/io/async_read.rs | 43 ++++++++++++++++++++------------------ tokio/src/io/mod.rs | 7 ++++--- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index b43a86d9d5a..1d8f1a3505e 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -4,29 +4,32 @@ use std::mem::MaybeUninit; use std::ops::DerefMut; use std::pin::Pin; use std::task::{Context, Poll}; -cfg_io_blocking! { - /// Implements `AsyncRead::prepare_uninitialized_buffer` in a such a way - /// that is valid for `R` requirements. - /// Should be used to override `AsyncRead::prepare_uninitialized_buffer` - /// for any AsyncRead impl which wraps an `std::io::Read` instance. - pub(crate) fn prepare_uninitialized_buffer_std_read( - buf: &mut [MaybeUninit], - ) -> bool { - // TODO: when std::io::Initializer is stable, it should be used - // to override `prepare_uninitialized_buffer` - /*use std::io::Read; - if !T::initializer::should_initialize() { - return false; - } - */ - for x in buf { - *x = MaybeUninit::new(0); - } - true + +/// Implements `AsyncRead::prepare_uninitialized_buffer` in a such a way +/// that is valid for `R` requirements. Currently, it will always +/// zero buffer. In future, it will skip zeroing if +/// `R::initializer` returns Initializer::nop() +/// +/// Should be used to override `AsyncRead::prepare_uninitialized_buffer` +/// for any AsyncRead impl which wraps an `std::io::Read` instance. +// TODO: use `cfg` bounds instead. +#[allow(dead_code)] +pub(crate) fn prepare_uninitialized_buffer_std_read( + buf: &mut [MaybeUninit], +) -> bool { + // TODO: when std::io::Initializer is stable, it should be used + // to override `prepare_uninitialized_buffer` + /*use std::io::Read; + if !T::initializer::should_initialize() { + return false; } + */ + for x in buf { + *x = MaybeUninit::new(0); + } + true } - /// Reads bytes from a source. /// /// This trait is analogous to the [`std::io::Read`] trait, but integrates with diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index 057d412e4f1..a9337ef1496 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -162,9 +162,10 @@ mod async_buf_read; pub use self::async_buf_read::AsyncBufRead; mod async_read; -cfg_io_blocking! { - pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; -} +// TODO: use `cfg` bounds instead. +#[allow(dead_code)] +pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; + pub use self::async_read::AsyncRead; mod async_seek; From e8eeb853ed1697630a053e25321a5b0af12aedd3 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 21 May 2020 16:48:00 +0300 Subject: [PATCH 14/18] Fix allow on use --- tokio/src/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index a9337ef1496..628e93c7b97 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -163,7 +163,7 @@ pub use self::async_buf_read::AsyncBufRead; mod async_read; // TODO: use `cfg` bounds instead. -#[allow(dead_code)] +#[allow(unused_imports)] pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; pub use self::async_read::AsyncRead; From fa9299dec20ea6c5e4107c3a62871d7fe6c5257a Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 21 May 2020 17:21:05 +0300 Subject: [PATCH 15/18] Expose the function for tests --- tokio/src/io/async_read.rs | 6 ++---- tokio/src/io/mod.rs | 9 ++++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index 1d8f1a3505e..e757d444e33 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -11,10 +11,8 @@ use std::task::{Context, Poll}; /// `R::initializer` returns Initializer::nop() /// /// Should be used to override `AsyncRead::prepare_uninitialized_buffer` -/// for any AsyncRead impl which wraps an `std::io::Read` instance. -// TODO: use `cfg` bounds instead. -#[allow(dead_code)] -pub(crate) fn prepare_uninitialized_buffer_std_read( +/// for any AsyncRead impl which wraps an `std::io::Read` instance.] +pub fn prepare_uninitialized_buffer_std_read( buf: &mut [MaybeUninit], ) -> bool { // TODO: when std::io::Initializer is stable, it should be used diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index 628e93c7b97..daaf0393a40 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -162,9 +162,12 @@ mod async_buf_read; pub use self::async_buf_read::AsyncBufRead; mod async_read; -// TODO: use `cfg` bounds instead. -#[allow(unused_imports)] -pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; + +// This function is only `pub` to be used in `fs_file_mocked` test. +// It is considered implementation detail, and should not be used +// outside of Tokio. +#[doc(hidden)] +pub use self::async_read::prepare_uninitialized_buffer_std_read; pub use self::async_read::AsyncRead; From 65dc798276a31c23ce0c7db4f598b6a77765deb9 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 21 May 2020 18:37:01 +0300 Subject: [PATCH 16/18] switch to better approach --- tokio/src/io/async_read.rs | 3 ++- tokio/src/io/mod.rs | 7 ++----- tokio/tests/fs_file_mocked.rs | 10 ++++++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index e757d444e33..29031598fa0 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -12,7 +12,8 @@ use std::task::{Context, Poll}; /// /// Should be used to override `AsyncRead::prepare_uninitialized_buffer` /// for any AsyncRead impl which wraps an `std::io::Read` instance.] -pub fn prepare_uninitialized_buffer_std_read( +#[allow(dead_code)] +pub(crate) fn prepare_uninitialized_buffer_std_read( buf: &mut [MaybeUninit], ) -> bool { // TODO: when std::io::Initializer is stable, it should be used diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index daaf0393a40..a4acc6f0967 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -163,11 +163,8 @@ pub use self::async_buf_read::AsyncBufRead; mod async_read; -// This function is only `pub` to be used in `fs_file_mocked` test. -// It is considered implementation detail, and should not be used -// outside of Tokio. -#[doc(hidden)] -pub use self::async_read::prepare_uninitialized_buffer_std_read; +#[allow(unused_imports)] +pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; pub use self::async_read::AsyncRead; diff --git a/tokio/tests/fs_file_mocked.rs b/tokio/tests/fs_file_mocked.rs index 0c5722404ea..1fdec1b365c 100644 --- a/tokio/tests/fs_file_mocked.rs +++ b/tokio/tests/fs_file_mocked.rs @@ -43,6 +43,16 @@ pub(crate) use support::mock_pool as pool; pub(crate) mod io { pub(crate) use tokio::io::*; + // this function duplicated here, because it is pub(crate) so not imported. + pub(crate) fn prepare_uninitialized_buffer_std_read( + buf: &mut [std::mem::MaybeUninit], + ) -> bool { + for x in buf { + *x = std::mem::MaybeUninit::new(0); + } + true + } + pub(crate) use crate::blocking; pub(crate) mod sys { From 549860f269b88042a6b6d73abc70fdd4ffc8639c Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 21 May 2020 19:17:17 +0300 Subject: [PATCH 17/18] Fix zeroing in Chain --- tokio/src/io/util/chain.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/tokio/src/io/util/chain.rs b/tokio/src/io/util/chain.rs index fd33b682dcf..8ba9194f5de 100644 --- a/tokio/src/io/util/chain.rs +++ b/tokio/src/io/util/chain.rs @@ -91,9 +91,6 @@ where if self.second.prepare_uninitialized_buffer(buf) { return true; } - for x in buf { - *x = std::mem::MaybeUninit::new(0); - } false } fn poll_read( From aeb924acf0376678b8cb1cd4f1e30e2ce377ddf6 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 21 May 2020 19:41:42 +0300 Subject: [PATCH 18/18] Rollback useless machinery --- tokio/src/io/async_read.rs | 24 ------------------------ tokio/src/io/blocking.rs | 4 ---- tokio/src/io/mod.rs | 3 --- tokio/src/io/poll_evented.rs | 4 ---- tokio/tests/fs_file_mocked.rs | 10 ---------- 5 files changed, 45 deletions(-) diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index 29031598fa0..a42c4ff24bc 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -5,30 +5,6 @@ use std::ops::DerefMut; use std::pin::Pin; use std::task::{Context, Poll}; -/// Implements `AsyncRead::prepare_uninitialized_buffer` in a such a way -/// that is valid for `R` requirements. Currently, it will always -/// zero buffer. In future, it will skip zeroing if -/// `R::initializer` returns Initializer::nop() -/// -/// Should be used to override `AsyncRead::prepare_uninitialized_buffer` -/// for any AsyncRead impl which wraps an `std::io::Read` instance.] -#[allow(dead_code)] -pub(crate) fn prepare_uninitialized_buffer_std_read( - buf: &mut [MaybeUninit], -) -> bool { - // TODO: when std::io::Initializer is stable, it should be used - // to override `prepare_uninitialized_buffer` - /*use std::io::Read; - if !T::initializer::should_initialize() { - return false; - } - */ - for x in buf { - *x = MaybeUninit::new(0); - } - true -} - /// Reads bytes from a source. /// /// This trait is analogous to the [`std::io::Read`] trait, but integrates with diff --git a/tokio/src/io/blocking.rs b/tokio/src/io/blocking.rs index ff4445e76d0..2491039a3f3 100644 --- a/tokio/src/io/blocking.rs +++ b/tokio/src/io/blocking.rs @@ -50,10 +50,6 @@ impl AsyncRead for Blocking where T: Read + Unpin + Send + 'static, { - unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit]) -> bool { - crate::io::prepare_uninitialized_buffer_std_read::(buf) - } - fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index a4acc6f0967..69441ae155a 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -163,9 +163,6 @@ pub use self::async_buf_read::AsyncBufRead; mod async_read; -#[allow(unused_imports)] -pub(crate) use self::async_read::prepare_uninitialized_buffer_std_read; - pub use self::async_read::AsyncRead; mod async_seek; diff --git a/tokio/src/io/poll_evented.rs b/tokio/src/io/poll_evented.rs index c02302b4cb0..f8be6f2f755 100644 --- a/tokio/src/io/poll_evented.rs +++ b/tokio/src/io/poll_evented.rs @@ -381,10 +381,6 @@ impl AsyncRead for PollEvented where E: Evented + Read + Unpin, { - unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit]) -> bool { - crate::io::prepare_uninitialized_buffer_std_read::(buf) - } - fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/tokio/tests/fs_file_mocked.rs b/tokio/tests/fs_file_mocked.rs index 1fdec1b365c..0c5722404ea 100644 --- a/tokio/tests/fs_file_mocked.rs +++ b/tokio/tests/fs_file_mocked.rs @@ -43,16 +43,6 @@ pub(crate) use support::mock_pool as pool; pub(crate) mod io { pub(crate) use tokio::io::*; - // this function duplicated here, because it is pub(crate) so not imported. - pub(crate) fn prepare_uninitialized_buffer_std_read( - buf: &mut [std::mem::MaybeUninit], - ) -> bool { - for x in buf { - *x = std::mem::MaybeUninit::new(0); - } - true - } - pub(crate) use crate::blocking; pub(crate) mod sys {