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

Add Read/Write::can_read/write_vectored #67841

Merged
merged 6 commits into from
Apr 28, 2020
Merged
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
20 changes: 20 additions & 0 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,11 @@ impl Read for File {
self.inner.read_vectored(bufs)
}

#[inline]
fn is_read_vectored(&self) -> bool {
self.inner.is_read_vectored()
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand All @@ -674,6 +679,11 @@ impl Write for File {
self.inner.write_vectored(bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
self.inner.is_write_vectored()
}

fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
Expand All @@ -694,6 +704,11 @@ impl Read for &File {
self.inner.read_vectored(bufs)
}

#[inline]
fn is_read_vectored(&self) -> bool {
self.inner.is_read_vectored()
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand All @@ -709,6 +724,11 @@ impl Write for &File {
self.inner.write_vectored(bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
self.inner.is_write_vectored()
}

fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
Expand Down
8 changes: 8 additions & 0 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ impl<R: Read> Read for BufReader<R> {
Ok(nread)
}

fn is_read_vectored(&self) -> bool {
self.inner.is_read_vectored()
}

// we can't skip unconditionally because of the large buffer case in read.
unsafe fn initializer(&self) -> Initializer {
self.inner.initializer()
Expand Down Expand Up @@ -680,6 +684,10 @@ impl<W: Write> Write for BufWriter<W> {
}
}

fn is_write_vectored(&self) -> bool {
self.get_ref().is_write_vectored()
}

fn flush(&mut self) -> io::Result<()> {
self.flush_buf().and_then(|()| self.get_mut().flush())
}
Expand Down
24 changes: 24 additions & 0 deletions src/libstd/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ where
Ok(nread)
}

fn is_read_vectored(&self) -> bool {
true
}

fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
let n = buf.len();
Read::read_exact(&mut self.fill_buf()?, buf)?;
Expand Down Expand Up @@ -372,6 +376,11 @@ impl Write for Cursor<&mut [u8]> {
slice_write_vectored(&mut self.pos, self.inner, bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
true
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
Ok(())
Expand All @@ -388,6 +397,11 @@ impl Write for Cursor<&mut Vec<u8>> {
vec_write_vectored(&mut self.pos, self.inner, bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
true
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
Ok(())
Expand All @@ -404,6 +418,11 @@ impl Write for Cursor<Vec<u8>> {
vec_write_vectored(&mut self.pos, &mut self.inner, bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
true
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
Ok(())
Expand All @@ -422,6 +441,11 @@ impl Write for Cursor<Box<[u8]>> {
slice_write_vectored(&mut self.pos, &mut self.inner, bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
true
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
Ok(())
Expand Down
35 changes: 35 additions & 0 deletions src/libstd/io/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ impl<R: Read + ?Sized> Read for &mut R {
(**self).read_vectored(bufs)
}

#[inline]
fn is_read_vectored(&self) -> bool {
(**self).is_read_vectored()
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
(**self).initializer()
Expand Down Expand Up @@ -52,6 +57,11 @@ impl<W: Write + ?Sized> Write for &mut W {
(**self).write_vectored(bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
(**self).is_write_vectored()
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
(**self).flush()
Expand Down Expand Up @@ -109,6 +119,11 @@ impl<R: Read + ?Sized> Read for Box<R> {
(**self).read_vectored(bufs)
}

#[inline]
fn is_read_vectored(&self) -> bool {
(**self).is_read_vectored()
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
(**self).initializer()
Expand Down Expand Up @@ -141,6 +156,11 @@ impl<W: Write + ?Sized> Write for Box<W> {
(**self).write_vectored(bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
(**self).is_write_vectored()
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
(**self).flush()
Expand Down Expand Up @@ -240,6 +260,11 @@ impl Read for &[u8] {
Ok(nread)
}

#[inline]
fn is_read_vectored(&self) -> bool {
true
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand Down Expand Up @@ -316,6 +341,11 @@ impl Write for &mut [u8] {
Ok(nwritten)
}

#[inline]
fn is_write_vectored(&self) -> bool {
true
}

#[inline]
fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
if self.write(data)? == data.len() {
Expand Down Expand Up @@ -351,6 +381,11 @@ impl Write for Vec<u8> {
Ok(len)
}

#[inline]
fn is_write_vectored(&self) -> bool {
true
}

#[inline]
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.extend_from_slice(buf);
Expand Down
27 changes: 27 additions & 0 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
//! [`Read::read`]: trait.Read.html#tymethod.read
//! [`Result`]: ../result/enum.Result.html
//! [`.unwrap()`]: ../result/enum.Result.html#method.unwrap
// ignore-tidy-filelength

#![stable(feature = "rust1", since = "1.0.0")]

Expand Down Expand Up @@ -580,6 +581,19 @@ pub trait Read {
default_read_vectored(|b| self.read(b), bufs)
}

/// Determines if this `Read`er has an efficient `read_vectored`
/// implementation.
///
/// If a `Read`er does not override the default `read_vectored`
/// implementation, code using it may want to avoid the method all together
/// and coalesce writes into a single buffer for higher performance.
///
/// The default implementation returns `false`.
#[unstable(feature = "can_vector", issue = "69941")]
fn is_read_vectored(&self) -> bool {
false
}

/// Determines if this `Read`er can work with buffers of uninitialized
/// memory.
///
Expand Down Expand Up @@ -1304,6 +1318,19 @@ pub trait Write {
default_write_vectored(|b| self.write(b), bufs)
}

/// Determines if this `Write`er has an efficient `write_vectored`
/// implementation.
///
/// If a `Write`er does not override the default `write_vectored`
/// implementation, code using it may want to avoid the method all together
/// and coalesce writes into a single buffer for higher performance.
///
/// The default implementation returns `false`.
#[unstable(feature = "can_vector", issue = "69941")]
fn is_write_vectored(&self) -> bool {
false
}

/// Flush this output stream, ensuring that all intermediately buffered
/// contents reach their destination.
///
Expand Down
56 changes: 56 additions & 0 deletions src/libstd/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ impl Read for StdinRaw {
self.0.read_vectored(bufs)
}

#[inline]
fn is_read_vectored(&self) -> bool {
self.0.is_read_vectored()
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand All @@ -101,6 +106,11 @@ impl Write for StdoutRaw {
self.0.write_vectored(bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
self.0.is_write_vectored()
}

fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
Expand All @@ -114,6 +124,11 @@ impl Write for StderrRaw {
self.0.write_vectored(bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
self.0.is_write_vectored()
}

fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
Expand All @@ -140,6 +155,14 @@ impl<W: io::Write> io::Write for Maybe<W> {
}
}

#[inline]
fn is_write_vectored(&self) -> bool {
match self {
Maybe::Real(w) => w.is_write_vectored(),
Maybe::Fake => true,
}
}

fn flush(&mut self) -> io::Result<()> {
match *self {
Maybe::Real(ref mut w) => handle_ebadf(w.flush(), ()),
Expand All @@ -162,6 +185,14 @@ impl<R: io::Read> io::Read for Maybe<R> {
Maybe::Fake => Ok(0),
}
}

#[inline]
fn is_read_vectored(&self) -> bool {
match self {
Maybe::Real(w) => w.is_read_vectored(),
Maybe::Fake => true,
}
}
}

fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
Expand Down Expand Up @@ -352,6 +383,10 @@ impl Read for Stdin {
self.lock().read_vectored(bufs)
}
#[inline]
fn is_read_vectored(&self) -> bool {
self.lock().is_read_vectored()
}
#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
}
Expand All @@ -376,6 +411,11 @@ impl Read for StdinLock<'_> {
self.inner.read_vectored(bufs)
}

#[inline]
fn is_read_vectored(&self) -> bool {
self.inner.is_read_vectored()
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand Down Expand Up @@ -543,6 +583,10 @@ impl Write for Stdout {
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
self.lock().write_vectored(bufs)
}
#[inline]
fn is_write_vectored(&self) -> bool {
self.lock().is_write_vectored()
}
fn flush(&mut self) -> io::Result<()> {
self.lock().flush()
}
Expand All @@ -561,6 +605,10 @@ impl Write for StdoutLock<'_> {
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
self.inner.borrow_mut().write_vectored(bufs)
}
#[inline]
fn is_write_vectored(&self) -> bool {
self.inner.borrow_mut().is_write_vectored()
}
fn flush(&mut self) -> io::Result<()> {
self.inner.borrow_mut().flush()
}
Expand Down Expand Up @@ -709,6 +757,10 @@ impl Write for Stderr {
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
self.lock().write_vectored(bufs)
}
#[inline]
fn is_write_vectored(&self) -> bool {
self.lock().is_write_vectored()
}
fn flush(&mut self) -> io::Result<()> {
self.lock().flush()
}
Expand All @@ -727,6 +779,10 @@ impl Write for StderrLock<'_> {
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
self.inner.borrow_mut().write_vectored(bufs)
}
#[inline]
fn is_write_vectored(&self) -> bool {
self.inner.borrow_mut().is_write_vectored()
}
fn flush(&mut self) -> io::Result<()> {
self.inner.borrow_mut().flush()
}
Expand Down
Loading