Skip to content
Permalink
Browse files

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...
alexcrichton committed Apr 10, 2019
1 parent 3750348 commit acf3ddb5ad163ea98f8935b045fc6d15faefa454
@@ -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};
@@ -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()
@@ -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")]
@@ -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()
@@ -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")]
@@ -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};
@@ -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() }
}

@@ -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(), ()),
@@ -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> {
@@ -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()
@@ -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()
@@ -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()
}
@@ -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()
}
@@ -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()
}
@@ -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()
}
@@ -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};
@@ -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(())
}
@@ -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()
@@ -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()
@@ -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};
@@ -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 {}
}
@@ -1,4 +1,4 @@
use crate::io;
use crate::io::{self, IoVec, IoVecMut};
use crate::sys::Void;

pub struct AnonPipe(Void);
@@ -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 {}
}
@@ -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;
@@ -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> {
@@ -1,4 +1,4 @@
use crate::io;
use crate::io::{self, IoVec, IoVecMut};
use crate::sys::{cvt, syscall};
use crate::sys::fd::FileDesc;

@@ -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 }
}
@@ -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};
@@ -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 {}
}
@@ -1,4 +1,4 @@
use crate::io;
use crate::io::{self, IoVec, IoVecMut};
use crate::sys::Void;

pub struct AnonPipe(Void);
@@ -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 {}
}
Oops, something went wrong.

0 comments on commit acf3ddb

Please sign in to comment.
You can’t perform that action at this time.