From 78e094632ec6160c3d2cfaad777c16a27ce08609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Fri, 21 Aug 2020 00:00:00 +0000 Subject: [PATCH] Remove wrapper type handling absent raw standard streams Raw standard streams are always available. Remove unused wrapper type that was supposed to be responsible for handling their absence. --- library/std/src/io/stdio.rs | 133 +++++++++--------------------------- 1 file changed, 33 insertions(+), 100 deletions(-) diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index c68d598080087..3943c66aad53a 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -83,11 +83,11 @@ const fn stderr_raw() -> StderrRaw { impl Read for StdinRaw { fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.0.read(buf) + handle_ebadf(self.0.read(buf), 0) } fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result { - self.0.read_vectored(bufs) + handle_ebadf(self.0.read_vectored(bufs), 0) } #[inline] @@ -101,25 +101,22 @@ impl Read for StdinRaw { } fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - self.0.read_to_end(buf) + handle_ebadf(self.0.read_to_end(buf), 0) } fn read_to_string(&mut self, buf: &mut String) -> io::Result { - self.0.read_to_string(buf) - } - - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - self.0.read_exact(buf) + handle_ebadf(self.0.read_to_string(buf), 0) } } impl Write for StdoutRaw { fn write(&mut self, buf: &[u8]) -> io::Result { - self.0.write(buf) + handle_ebadf(self.0.write(buf), buf.len()) } fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { - self.0.write_vectored(bufs) + let total = bufs.iter().map(|b| b.len()).sum(); + handle_ebadf(self.0.write_vectored(bufs), total) } #[inline] @@ -128,29 +125,30 @@ impl Write for StdoutRaw { } fn flush(&mut self) -> io::Result<()> { - self.0.flush() + handle_ebadf(self.0.flush(), ()) } fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { - self.0.write_all(buf) + handle_ebadf(self.0.write_all(buf), ()) } fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> { - self.0.write_all_vectored(bufs) + handle_ebadf(self.0.write_all_vectored(bufs), ()) } fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { - self.0.write_fmt(fmt) + handle_ebadf(self.0.write_fmt(fmt), ()) } } impl Write for StderrRaw { fn write(&mut self, buf: &[u8]) -> io::Result { - self.0.write(buf) + handle_ebadf(self.0.write(buf), buf.len()) } fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { - self.0.write_vectored(bufs) + let total = bufs.iter().map(|b| b.len()).sum(); + handle_ebadf(self.0.write_vectored(bufs), total) } #[inline] @@ -159,80 +157,19 @@ impl Write for StderrRaw { } fn flush(&mut self) -> io::Result<()> { - self.0.flush() + handle_ebadf(self.0.flush(), ()) } fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { - self.0.write_all(buf) + handle_ebadf(self.0.write_all(buf), ()) } fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> { - self.0.write_all_vectored(bufs) + handle_ebadf(self.0.write_all_vectored(bufs), ()) } fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { - self.0.write_fmt(fmt) - } -} - -enum Maybe { - Real(T), - Fake, -} - -impl io::Write for Maybe { - fn write(&mut self, buf: &[u8]) -> io::Result { - match *self { - Maybe::Real(ref mut w) => handle_ebadf(w.write(buf), buf.len()), - Maybe::Fake => Ok(buf.len()), - } - } - - fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { - 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), - } - } - - #[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(), ()), - Maybe::Fake => Ok(()), - } - } -} - -impl io::Read for Maybe { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - match *self { - Maybe::Real(ref mut r) => handle_ebadf(r.read(buf), 0), - Maybe::Fake => Ok(0), - } - } - - fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result { - match self { - Maybe::Real(r) => handle_ebadf(r.read_vectored(bufs), 0), - Maybe::Fake => Ok(0), - } - } - - #[inline] - fn is_read_vectored(&self) -> bool { - match self { - Maybe::Real(w) => w.is_read_vectored(), - Maybe::Fake => true, - } + handle_ebadf(self.0.write_fmt(fmt), ()) } } @@ -277,7 +214,7 @@ fn handle_ebadf(r: io::Result, default: T) -> io::Result { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub struct Stdin { - inner: Arc>>>, + inner: Arc>>, } /// A locked reference to the `Stdin` handle. @@ -308,7 +245,7 @@ pub struct Stdin { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub struct StdinLock<'a> { - inner: MutexGuard<'a, BufReader>>, + inner: MutexGuard<'a, BufReader>, } /// Constructs a new handle to the standard input of the current process. @@ -352,14 +289,14 @@ pub struct StdinLock<'a> { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn stdin() -> Stdin { - static INSTANCE: Lazy>>> = Lazy::new(); + static INSTANCE: Lazy>> = Lazy::new(); return Stdin { inner: unsafe { INSTANCE.get(stdin_init).expect("cannot access stdin during shutdown") }, }; - fn stdin_init() -> Arc>>> { + fn stdin_init() -> Arc>> { // This must not reentrantly access `INSTANCE` - let stdin = Maybe::Real(stdin_raw()); + let stdin = stdin_raw(); Arc::new(Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin))) } } @@ -536,7 +473,7 @@ pub struct Stdout { // FIXME: this should be LineWriter or BufWriter depending on the state of // stdout (tty or not). Note that if this is not line buffered it // should also flush-on-panic or some form of flush-on-abort. - inner: Arc>>>>, + inner: Arc>>>, } /// A locked reference to the `Stdout` handle. @@ -550,7 +487,7 @@ pub struct Stdout { /// an error. #[stable(feature = "rust1", since = "1.0.0")] pub struct StdoutLock<'a> { - inner: ReentrantMutexGuard<'a, RefCell>>>, + inner: ReentrantMutexGuard<'a, RefCell>>, } /// Constructs a new handle to the standard output of the current process. @@ -594,14 +531,14 @@ pub struct StdoutLock<'a> { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn stdout() -> Stdout { - static INSTANCE: Lazy>>>> = Lazy::new(); + static INSTANCE: Lazy>>> = Lazy::new(); return Stdout { inner: unsafe { INSTANCE.get(stdout_init).expect("cannot access stdout during shutdown") }, }; - fn stdout_init() -> Arc>>>> { + fn stdout_init() -> Arc>>> { // This must not reentrantly access `INSTANCE` - let stdout = Maybe::Real(stdout_raw()); + let stdout = stdout_raw(); unsafe { let ret = Arc::new(ReentrantMutex::new(RefCell::new(LineWriter::new(stdout)))); ret.init(); @@ -711,7 +648,7 @@ impl fmt::Debug for StdoutLock<'_> { /// an error. #[stable(feature = "rust1", since = "1.0.0")] pub struct Stderr { - inner: &'static ReentrantMutex>>, + inner: &'static ReentrantMutex>, } /// A locked reference to the `Stderr` handle. @@ -725,7 +662,7 @@ pub struct Stderr { /// an error. #[stable(feature = "rust1", since = "1.0.0")] pub struct StderrLock<'a> { - inner: ReentrantMutexGuard<'a, RefCell>>, + inner: ReentrantMutexGuard<'a, RefCell>, } /// Constructs a new handle to the standard error of the current process. @@ -774,18 +711,14 @@ pub fn stderr() -> Stderr { // // This has the added benefit of allowing `stderr` to be usable during // process shutdown as well! - static INSTANCE: ReentrantMutex>> = - unsafe { ReentrantMutex::new(RefCell::new(Maybe::Fake)) }; + static INSTANCE: ReentrantMutex> = + unsafe { ReentrantMutex::new(RefCell::new(stderr_raw())) }; // When accessing stderr we need one-time initialization of the reentrant - // mutex, followed by one-time detection of whether we actually have a - // stderr handle or not. Afterwards we can just always use the now-filled-in - // `INSTANCE` value. + // mutex. Afterwards we can just always use the now-filled-in `INSTANCE` value. static INIT: Once = Once::new(); INIT.call_once(|| unsafe { INSTANCE.init(); - let stderr = stderr_raw(); - *INSTANCE.lock().borrow_mut() = Maybe::Real(stderr); }); Stderr { inner: &INSTANCE } }