Skip to content

Commit

Permalink
Rollup merge of rust-lang#59852 - alexcrichton:more-vectored, r=sfackler
Browse files Browse the repository at this point in the history
std: Add `{read,write}_vectored` for more types

This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:

* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`

Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
  • Loading branch information
Centril authored Apr 12, 2019
2 parents 2a24c8f + acf3ddb commit 23c824e
Show file tree
Hide file tree
Showing 19 changed files with 247 additions and 37 deletions.
20 changes: 19 additions & 1 deletion src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use crate::fmt;
use crate::ffi::OsString;
use crate::io::{self, SeekFrom, Seek, Read, Initializer, Write};
use crate::io::{self, SeekFrom, Seek, Read, Initializer, Write, IoVec, IoVecMut};
use crate::path::{Path, PathBuf};
use crate::sys::fs as fs_imp;
use crate::sys_common::{AsInnerMut, FromInner, AsInner, IntoInner};
Expand Down Expand Up @@ -615,6 +615,10 @@ impl Read for File {
self.inner.read(buf)
}

fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
self.inner.read_vectored(bufs)
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand All @@ -625,6 +629,11 @@ impl Write for File {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}

fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
self.inner.write_vectored(bufs)
}

fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
}
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -639,6 +648,10 @@ impl Read for &File {
self.inner.read(buf)
}

fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
self.inner.read_vectored(bufs)
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand All @@ -649,6 +662,11 @@ impl Write for &File {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}

fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
self.inner.write_vectored(bufs)
}

fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
}
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
51 changes: 50 additions & 1 deletion src/libstd/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::io::prelude::*;
use crate::cell::RefCell;
use crate::fmt;
use crate::io::lazy::Lazy;
use crate::io::{self, Initializer, BufReader, LineWriter};
use crate::io::{self, Initializer, BufReader, LineWriter, IoVec, IoVecMut};
use crate::sync::{Arc, Mutex, MutexGuard};
use crate::sys::stdio;
use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
Expand Down Expand Up @@ -75,17 +75,31 @@ fn stderr_raw() -> io::Result<StderrRaw> { stdio::Stderr::new().map(StderrRaw) }
impl Read for StdinRaw {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.0.read(buf) }

fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
self.0.read_vectored(bufs)
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
}
}
impl Write for StdoutRaw {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.0.write(buf) }

fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
self.0.write_vectored(bufs)
}

fn flush(&mut self) -> io::Result<()> { self.0.flush() }
}
impl Write for StderrRaw {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.0.write(buf) }

fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
self.0.write_vectored(bufs)
}

fn flush(&mut self) -> io::Result<()> { self.0.flush() }
}

Expand All @@ -102,6 +116,14 @@ impl<W: io::Write> io::Write for Maybe<W> {
}
}

fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
let total = bufs.iter().map(|b| b.len()).sum();
match self {
Maybe::Real(w) => handle_ebadf(w.write_vectored(bufs), total),
Maybe::Fake => Ok(total),
}
}

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

fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
match self {
Maybe::Real(r) => handle_ebadf(r.read_vectored(bufs), 0),
Maybe::Fake => Ok(0)
}
}
}

fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
Expand Down Expand Up @@ -305,6 +334,9 @@ impl Read for Stdin {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.lock().read(buf)
}
fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
self.lock().read_vectored(bufs)
}
#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand All @@ -325,6 +357,11 @@ impl Read for StdinLock<'_> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}

fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
self.inner.read_vectored(bufs)
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand Down Expand Up @@ -483,6 +520,9 @@ impl Write for Stdout {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.lock().write(buf)
}
fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
self.lock().write_vectored(bufs)
}
fn flush(&mut self) -> io::Result<()> {
self.lock().flush()
}
Expand All @@ -498,6 +538,9 @@ impl Write for StdoutLock<'_> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.borrow_mut().write(buf)
}
fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
self.inner.borrow_mut().write_vectored(bufs)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.borrow_mut().flush()
}
Expand Down Expand Up @@ -636,6 +679,9 @@ impl Write for Stderr {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.lock().write(buf)
}
fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
self.lock().write_vectored(bufs)
}
fn flush(&mut self) -> io::Result<()> {
self.lock().flush()
}
Expand All @@ -651,6 +697,9 @@ impl Write for StderrLock<'_> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.borrow_mut().write(buf)
}
fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
self.inner.borrow_mut().write_vectored(bufs)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.borrow_mut().flush()
}
Expand Down
16 changes: 15 additions & 1 deletion src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ use crate::io::prelude::*;
use crate::ffi::OsStr;
use crate::fmt;
use crate::fs;
use crate::io::{self, Initializer};
use crate::io::{self, Initializer, IoVec, IoVecMut};
use crate::path::Path;
use crate::str;
use crate::sys::pipe::{read2, AnonPipe};
Expand Down Expand Up @@ -225,6 +225,10 @@ impl Write for ChildStdin {
self.inner.write(buf)
}

fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
self.inner.write_vectored(bufs)
}

fn flush(&mut self) -> io::Result<()> {
Ok(())
}
Expand Down Expand Up @@ -271,6 +275,11 @@ impl Read for ChildStdout {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}

fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
self.inner.read_vectored(bufs)
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand Down Expand Up @@ -318,6 +327,11 @@ impl Read for ChildStderr {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}

fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
self.inner.read_vectored(bufs)
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand Down
10 changes: 9 additions & 1 deletion src/libstd/sys/cloudabi/shims/fs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ffi::OsString;
use crate::fmt;
use crate::hash::{Hash, Hasher};
use crate::io::{self, SeekFrom};
use crate::io::{self, SeekFrom, IoVec, IoVecMut};
use crate::path::{Path, PathBuf};
use crate::sys::time::SystemTime;
use crate::sys::{unsupported, Void};
Expand Down Expand Up @@ -198,10 +198,18 @@ impl File {
match self.0 {}
}

pub fn read_vectored(&self, _bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
match self.0 {}
}

pub fn write(&self, _buf: &[u8]) -> io::Result<usize> {
match self.0 {}
}

pub fn write_vectored(&self, _bufs: &[IoVec<'_>]) -> io::Result<usize> {
match self.0 {}
}

pub fn flush(&self) -> io::Result<()> {
match self.0 {}
}
Expand Down
10 changes: 9 additions & 1 deletion src/libstd/sys/cloudabi/shims/pipe.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::io;
use crate::io::{self, IoVec, IoVecMut};
use crate::sys::Void;

pub struct AnonPipe(Void);
Expand All @@ -8,10 +8,18 @@ impl AnonPipe {
match self.0 {}
}

pub fn read_vectored(&self, _bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
match self.0 {}
}

pub fn write(&self, _buf: &[u8]) -> io::Result<usize> {
match self.0 {}
}

pub fn write_vectored(&self, _bufs: &[IoVec<'_>]) -> io::Result<usize> {
match self.0 {}
}

pub fn diverge(&self) -> ! {
match self.0 {}
}
Expand Down
10 changes: 9 additions & 1 deletion src/libstd/sys/redox/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::os::unix::prelude::*;

use crate::ffi::{OsString, OsStr};
use crate::fmt;
use crate::io::{self, Error, SeekFrom};
use crate::io::{self, Error, SeekFrom, IoVec, IoVecMut};
use crate::path::{Path, PathBuf};
use crate::sync::Arc;
use crate::sys::fd::FileDesc;
Expand Down Expand Up @@ -278,10 +278,18 @@ impl File {
self.0.read(buf)
}

pub fn read_vectored(&self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
crate::io::default_read_vectored(|buf| self.read(buf), bufs)
}

pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}

pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
crate::io::default_write_vectored(|buf| self.write(buf), bufs)
}

pub fn flush(&self) -> io::Result<()> { Ok(()) }

pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
Expand Down
10 changes: 9 additions & 1 deletion src/libstd/sys/redox/pipe.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::io;
use crate::io::{self, IoVec, IoVecMut};
use crate::sys::{cvt, syscall};
use crate::sys::fd::FileDesc;

Expand All @@ -24,10 +24,18 @@ impl AnonPipe {
self.0.read(buf)
}

pub fn read_vectored(&self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
crate::io::default_read_vectored(|buf| self.read(buf), bufs)
}

pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}

pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
crate::io::default_write_vectored(|buf| self.write(buf), bufs)
}

pub fn fd(&self) -> &FileDesc { &self.0 }
pub fn into_fd(self) -> FileDesc { self.0 }
}
Expand Down
10 changes: 9 additions & 1 deletion src/libstd/sys/sgx/fs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ffi::OsString;
use crate::fmt;
use crate::hash::{Hash, Hasher};
use crate::io::{self, SeekFrom};
use crate::io::{self, SeekFrom, IoVec, IoVecMut};
use crate::path::{Path, PathBuf};
use crate::sys::time::SystemTime;
use crate::sys::{unsupported, Void};
Expand Down Expand Up @@ -200,10 +200,18 @@ impl File {
match self.0 {}
}

pub fn read_vectored(&self, _bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
match self.0 {}
}

pub fn write(&self, _buf: &[u8]) -> io::Result<usize> {
match self.0 {}
}

pub fn write_vectored(&self, _bufs: &[IoVec<'_>]) -> io::Result<usize> {
match self.0 {}
}

pub fn flush(&self) -> io::Result<()> {
match self.0 {}
}
Expand Down
15 changes: 10 additions & 5 deletions src/libstd/sys/sgx/pipe.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::io;
use crate::io::{self, IoVec, IoVecMut};
use crate::sys::Void;

pub struct AnonPipe(Void);
Expand All @@ -8,18 +8,23 @@ impl AnonPipe {
match self.0 {}
}

pub fn read_vectored(&self, _bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
match self.0 {}
}

pub fn write(&self, _buf: &[u8]) -> io::Result<usize> {
match self.0 {}
}

pub fn write_vectored(&self, _bufs: &[IoVec<'_>]) -> io::Result<usize> {
match self.0 {}
}

pub fn diverge(&self) -> ! {
match self.0 {}
}
}

pub fn read2(p1: AnonPipe,
_v1: &mut Vec<u8>,
_p2: AnonPipe,
_v2: &mut Vec<u8>) -> io::Result<()> {
pub fn read2(p1: AnonPipe, _v1: &mut Vec<u8>, _p2: AnonPipe, _v2: &mut Vec<u8>) -> io::Result<()> {
match p1.0 {}
}
Loading

0 comments on commit 23c824e

Please sign in to comment.