From 479c23bb493e4ea801c125cfc54e70723a9aeeb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Thu, 20 Aug 2020 00:00:00 +0000 Subject: [PATCH 1/3] Remove result type from raw standard streams constructors Raw standard streams constructors are infallible. Remove unnecessary result type. --- library/std/src/io/stdio.rs | 28 +++++++++--------------- library/std/src/sys/cloudabi/stdio.rs | 14 ++++++------ library/std/src/sys/hermit/stdio.rs | 14 ++++++------ library/std/src/sys/sgx/stdio.rs | 12 +++++----- library/std/src/sys/unix/stdio.rs | 14 ++++++------ library/std/src/sys/unsupported/stdio.rs | 12 +++++----- library/std/src/sys/vxworks/stdio.rs | 14 ++++++------ library/std/src/sys/wasi/stdio.rs | 14 ++++++------ library/std/src/sys/windows/stdio.rs | 14 ++++++------ library/std/src/sys/windows/stdio_uwp.rs | 14 ++++++------ 10 files changed, 71 insertions(+), 79 deletions(-) diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index 286eb92915e49..f91264af531d5 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -50,8 +50,8 @@ struct StderrRaw(stdio::Stderr); /// handles is **not** available to raw handles returned from this function. /// /// The returned handle has no external synchronization or buffering. -fn stdin_raw() -> io::Result { - stdio::Stdin::new().map(StdinRaw) +fn stdin_raw() -> StdinRaw { + StdinRaw(stdio::Stdin::new()) } /// Constructs a new raw handle to the standard output stream of this process. @@ -63,8 +63,8 @@ fn stdin_raw() -> io::Result { /// /// The returned handle has no external synchronization or buffering layered on /// top. -fn stdout_raw() -> io::Result { - stdio::Stdout::new().map(StdoutRaw) +fn stdout_raw() -> StdoutRaw { + StdoutRaw(stdio::Stdout::new()) } /// Constructs a new raw handle to the standard error stream of this process. @@ -74,8 +74,8 @@ fn stdout_raw() -> io::Result { /// /// The returned handle has no external synchronization or buffering layered on /// top. -fn stderr_raw() -> io::Result { - stdio::Stderr::new().map(StderrRaw) +fn stderr_raw() -> StderrRaw { + StderrRaw(stdio::Stderr::new()) } impl Read for StdinRaw { @@ -356,11 +356,7 @@ pub fn stdin() -> Stdin { fn stdin_init() -> Arc>>> { // This must not reentrantly access `INSTANCE` - let stdin = match stdin_raw() { - Ok(stdin) => Maybe::Real(stdin), - _ => Maybe::Fake, - }; - + let stdin = Maybe::Real(stdin_raw()); Arc::new(Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin))) } } @@ -602,10 +598,7 @@ pub fn stdout() -> Stdout { fn stdout_init() -> Arc>>>> { // This must not reentrantly access `INSTANCE` - let stdout = match stdout_raw() { - Ok(stdout) => Maybe::Real(stdout), - _ => Maybe::Fake, - }; + let stdout = Maybe::Real(stdout_raw()); unsafe { let ret = Arc::new(ReentrantMutex::new(RefCell::new(LineWriter::new(stdout)))); ret.init(); @@ -788,9 +781,8 @@ pub fn stderr() -> Stderr { static INIT: Once = Once::new(); INIT.call_once(|| unsafe { INSTANCE.init(); - if let Ok(stderr) = stderr_raw() { - *INSTANCE.lock().borrow_mut() = Maybe::Real(stderr); - } + let stderr = stderr_raw(); + *INSTANCE.lock().borrow_mut() = Maybe::Real(stderr); }); Stderr { inner: &INSTANCE } } diff --git a/library/std/src/sys/cloudabi/stdio.rs b/library/std/src/sys/cloudabi/stdio.rs index 601563c5b1fcb..d564f4f7f40b2 100644 --- a/library/std/src/sys/cloudabi/stdio.rs +++ b/library/std/src/sys/cloudabi/stdio.rs @@ -6,8 +6,8 @@ pub struct Stdout(()); pub struct Stderr(()); impl Stdin { - pub fn new() -> io::Result { - Ok(Stdin(())) + pub fn new() -> Stdin { + Stdin(()) } } @@ -18,8 +18,8 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> io::Result { - Ok(Stdout(())) + pub fn new() -> Stdout { + Stdout(()) } } @@ -37,8 +37,8 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> io::Result { - Ok(Stderr(())) + pub fn new() -> Stderr { + Stderr(()) } } @@ -62,5 +62,5 @@ pub fn is_ebadf(err: &io::Error) -> bool { pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE; pub fn panic_output() -> Option { - Stderr::new().ok() + Some(Stderr::new()) } diff --git a/library/std/src/sys/hermit/stdio.rs b/library/std/src/sys/hermit/stdio.rs index f3654ee38716c..359ea13c2befc 100644 --- a/library/std/src/sys/hermit/stdio.rs +++ b/library/std/src/sys/hermit/stdio.rs @@ -7,8 +7,8 @@ pub struct Stdout; pub struct Stderr; impl Stdin { - pub fn new() -> io::Result { - Ok(Stdin) + pub fn new() -> Stdin { + Stdin } } @@ -28,8 +28,8 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> io::Result { - Ok(Stdout) + pub fn new() -> Stdout { + Stdout } } @@ -69,8 +69,8 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> io::Result { - Ok(Stderr) + pub fn new() -> Stderr { + Stderr } } @@ -116,5 +116,5 @@ pub fn is_ebadf(_err: &io::Error) -> bool { } pub fn panic_output() -> Option { - Stderr::new().ok() + Some(Stderr::new()) } diff --git a/library/std/src/sys/sgx/stdio.rs b/library/std/src/sys/sgx/stdio.rs index 716c174bd53b6..d771a39ea851e 100644 --- a/library/std/src/sys/sgx/stdio.rs +++ b/library/std/src/sys/sgx/stdio.rs @@ -19,8 +19,8 @@ fn with_std_fd R, R>(fd: abi::Fd, f: F) -> R { } impl Stdin { - pub fn new() -> io::Result { - Ok(Stdin(())) + pub fn new() -> Stdin { + Stdin(()) } } @@ -31,8 +31,8 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> io::Result { - Ok(Stdout(())) + pub fn new() -> Stdout { + Stdout(()) } } @@ -47,8 +47,8 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> io::Result { - Ok(Stderr(())) + pub fn new() -> Stderr { + Stderr(()) } } diff --git a/library/std/src/sys/unix/stdio.rs b/library/std/src/sys/unix/stdio.rs index f8353214cbca0..bbf5dd65fa5d9 100644 --- a/library/std/src/sys/unix/stdio.rs +++ b/library/std/src/sys/unix/stdio.rs @@ -7,8 +7,8 @@ pub struct Stdout(()); pub struct Stderr(()); impl Stdin { - pub fn new() -> io::Result { - Ok(Stdin(())) + pub fn new() -> Stdin { + Stdin(()) } } @@ -28,8 +28,8 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> io::Result { - Ok(Stdout(())) + pub fn new() -> Stdout { + Stdout(()) } } @@ -53,8 +53,8 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> io::Result { - Ok(Stderr(())) + pub fn new() -> Stderr { + Stderr(()) } } @@ -84,5 +84,5 @@ pub fn is_ebadf(err: &io::Error) -> bool { pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE; pub fn panic_output() -> Option { - Stderr::new().ok() + Some(Stderr::new()) } diff --git a/library/std/src/sys/unsupported/stdio.rs b/library/std/src/sys/unsupported/stdio.rs index 5a4e4505e93bd..7e60e0712dd9d 100644 --- a/library/std/src/sys/unsupported/stdio.rs +++ b/library/std/src/sys/unsupported/stdio.rs @@ -5,8 +5,8 @@ pub struct Stdout; pub struct Stderr; impl Stdin { - pub fn new() -> io::Result { - Ok(Stdin) + pub fn new() -> Stdin { + Stdin } } @@ -17,8 +17,8 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> io::Result { - Ok(Stdout) + pub fn new() -> Stdout { + Stdout } } @@ -33,8 +33,8 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> io::Result { - Ok(Stderr) + pub fn new() -> Stderr { + Stderr } } diff --git a/library/std/src/sys/vxworks/stdio.rs b/library/std/src/sys/vxworks/stdio.rs index 622444ccafd3c..e99d2d583467e 100644 --- a/library/std/src/sys/vxworks/stdio.rs +++ b/library/std/src/sys/vxworks/stdio.rs @@ -6,8 +6,8 @@ pub struct Stdout(()); pub struct Stderr(()); impl Stdin { - pub fn new() -> io::Result { - Ok(Stdin(())) + pub fn new() -> Stdin { + Stdin(()) } } @@ -21,8 +21,8 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> io::Result { - Ok(Stdout(())) + pub fn new() -> Stdout { + Stdout(()) } } @@ -40,8 +40,8 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> io::Result { - Ok(Stderr(())) + pub fn new() -> Stderr { + Stderr(()) } } @@ -65,5 +65,5 @@ pub fn is_ebadf(err: &io::Error) -> bool { pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE; pub fn panic_output() -> Option { - Stderr::new().ok() + Some(Stderr::new()) } diff --git a/library/std/src/sys/wasi/stdio.rs b/library/std/src/sys/wasi/stdio.rs index 78e3911dc4efe..cc27e2ee5833c 100644 --- a/library/std/src/sys/wasi/stdio.rs +++ b/library/std/src/sys/wasi/stdio.rs @@ -7,8 +7,8 @@ pub struct Stdout; pub struct Stderr; impl Stdin { - pub fn new() -> io::Result { - Ok(Stdin) + pub fn new() -> Stdin { + Stdin } #[inline] @@ -33,8 +33,8 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> io::Result { - Ok(Stdout) + pub fn new() -> Stdout { + Stdout } #[inline] @@ -62,8 +62,8 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> io::Result { - Ok(Stderr) + pub fn new() -> Stderr { + Stderr } #[inline] @@ -98,5 +98,5 @@ pub fn is_ebadf(err: &io::Error) -> bool { } pub fn panic_output() -> Option { - Stderr::new().ok() + Some(Stderr::new()) } diff --git a/library/std/src/sys/windows/stdio.rs b/library/std/src/sys/windows/stdio.rs index c84896296ecb9..b2e5458c0d222 100644 --- a/library/std/src/sys/windows/stdio.rs +++ b/library/std/src/sys/windows/stdio.rs @@ -131,8 +131,8 @@ fn write_u16s(handle: c::HANDLE, data: &[u16]) -> io::Result { } impl Stdin { - pub fn new() -> io::Result { - Ok(Stdin { surrogate: 0 }) + pub fn new() -> Stdin { + Stdin { surrogate: 0 } } } @@ -255,8 +255,8 @@ fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result { } impl Stdout { - pub fn new() -> io::Result { - Ok(Stdout) + pub fn new() -> Stdout { + Stdout } } @@ -271,8 +271,8 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> io::Result { - Ok(Stderr) + pub fn new() -> Stderr { + Stderr } } @@ -291,5 +291,5 @@ pub fn is_ebadf(err: &io::Error) -> bool { } pub fn panic_output() -> Option { - Stderr::new().ok() + Some(Stderr::new()) } diff --git a/library/std/src/sys/windows/stdio_uwp.rs b/library/std/src/sys/windows/stdio_uwp.rs index 5bdabf6d4b78e..0016f5dcd0112 100644 --- a/library/std/src/sys/windows/stdio_uwp.rs +++ b/library/std/src/sys/windows/stdio_uwp.rs @@ -30,8 +30,8 @@ fn write(handle_id: c::DWORD, data: &[u8]) -> io::Result { } impl Stdin { - pub fn new() -> io::Result { - Ok(Stdin {}) + pub fn new() -> Stdin { + Stdin {} } } @@ -44,8 +44,8 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> io::Result { - Ok(Stdout) + pub fn new() -> Stdout { + Stdout } } @@ -60,8 +60,8 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> io::Result { - Ok(Stderr) + pub fn new() -> Stderr { + Stderr } } @@ -80,5 +80,5 @@ pub fn is_ebadf(err: &io::Error) -> bool { } pub fn panic_output() -> Option { - Stderr::new().ok() + Some(Stderr::new()) } From 4a00421ba4daae419d06b67bd1bb46d7930b0dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Thu, 20 Aug 2020 00:00:00 +0000 Subject: [PATCH 2/3] Make raw standard stream constructors const --- library/std/src/io/stdio.rs | 9 ++++++--- library/std/src/sys/cloudabi/stdio.rs | 6 +++--- library/std/src/sys/hermit/stdio.rs | 6 +++--- library/std/src/sys/sgx/stdio.rs | 6 +++--- library/std/src/sys/unix/stdio.rs | 6 +++--- library/std/src/sys/unsupported/stdio.rs | 6 +++--- library/std/src/sys/vxworks/stdio.rs | 6 +++--- library/std/src/sys/wasi/stdio.rs | 6 +++--- library/std/src/sys/windows/stdio.rs | 6 +++--- library/std/src/sys/windows/stdio_uwp.rs | 6 +++--- 10 files changed, 33 insertions(+), 30 deletions(-) diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index f91264af531d5..c68d598080087 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -50,7 +50,8 @@ struct StderrRaw(stdio::Stderr); /// handles is **not** available to raw handles returned from this function. /// /// The returned handle has no external synchronization or buffering. -fn stdin_raw() -> StdinRaw { +#[unstable(feature = "libstd_sys_internals", issue = "none")] +const fn stdin_raw() -> StdinRaw { StdinRaw(stdio::Stdin::new()) } @@ -63,7 +64,8 @@ fn stdin_raw() -> StdinRaw { /// /// The returned handle has no external synchronization or buffering layered on /// top. -fn stdout_raw() -> StdoutRaw { +#[unstable(feature = "libstd_sys_internals", issue = "none")] +const fn stdout_raw() -> StdoutRaw { StdoutRaw(stdio::Stdout::new()) } @@ -74,7 +76,8 @@ fn stdout_raw() -> StdoutRaw { /// /// The returned handle has no external synchronization or buffering layered on /// top. -fn stderr_raw() -> StderrRaw { +#[unstable(feature = "libstd_sys_internals", issue = "none")] +const fn stderr_raw() -> StderrRaw { StderrRaw(stdio::Stderr::new()) } diff --git a/library/std/src/sys/cloudabi/stdio.rs b/library/std/src/sys/cloudabi/stdio.rs index d564f4f7f40b2..7fec4731a462c 100644 --- a/library/std/src/sys/cloudabi/stdio.rs +++ b/library/std/src/sys/cloudabi/stdio.rs @@ -6,7 +6,7 @@ pub struct Stdout(()); pub struct Stderr(()); impl Stdin { - pub fn new() -> Stdin { + pub const fn new() -> Stdin { Stdin(()) } } @@ -18,7 +18,7 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> Stdout { + pub const fn new() -> Stdout { Stdout(()) } } @@ -37,7 +37,7 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> Stderr { + pub const fn new() -> Stderr { Stderr(()) } } diff --git a/library/std/src/sys/hermit/stdio.rs b/library/std/src/sys/hermit/stdio.rs index 359ea13c2befc..82304dd6dc293 100644 --- a/library/std/src/sys/hermit/stdio.rs +++ b/library/std/src/sys/hermit/stdio.rs @@ -7,7 +7,7 @@ pub struct Stdout; pub struct Stderr; impl Stdin { - pub fn new() -> Stdin { + pub const fn new() -> Stdin { Stdin } } @@ -28,7 +28,7 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> Stdout { + pub const fn new() -> Stdout { Stdout } } @@ -69,7 +69,7 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> Stderr { + pub const fn new() -> Stderr { Stderr } } diff --git a/library/std/src/sys/sgx/stdio.rs b/library/std/src/sys/sgx/stdio.rs index d771a39ea851e..49f44f9f498ac 100644 --- a/library/std/src/sys/sgx/stdio.rs +++ b/library/std/src/sys/sgx/stdio.rs @@ -19,7 +19,7 @@ fn with_std_fd R, R>(fd: abi::Fd, f: F) -> R { } impl Stdin { - pub fn new() -> Stdin { + pub const fn new() -> Stdin { Stdin(()) } } @@ -31,7 +31,7 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> Stdout { + pub const fn new() -> Stdout { Stdout(()) } } @@ -47,7 +47,7 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> Stderr { + pub const fn new() -> Stderr { Stderr(()) } } diff --git a/library/std/src/sys/unix/stdio.rs b/library/std/src/sys/unix/stdio.rs index bbf5dd65fa5d9..a05fe8165cff2 100644 --- a/library/std/src/sys/unix/stdio.rs +++ b/library/std/src/sys/unix/stdio.rs @@ -7,7 +7,7 @@ pub struct Stdout(()); pub struct Stderr(()); impl Stdin { - pub fn new() -> Stdin { + pub const fn new() -> Stdin { Stdin(()) } } @@ -28,7 +28,7 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> Stdout { + pub const fn new() -> Stdout { Stdout(()) } } @@ -53,7 +53,7 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> Stderr { + pub const fn new() -> Stderr { Stderr(()) } } diff --git a/library/std/src/sys/unsupported/stdio.rs b/library/std/src/sys/unsupported/stdio.rs index 7e60e0712dd9d..b5e3f5be9885b 100644 --- a/library/std/src/sys/unsupported/stdio.rs +++ b/library/std/src/sys/unsupported/stdio.rs @@ -5,7 +5,7 @@ pub struct Stdout; pub struct Stderr; impl Stdin { - pub fn new() -> Stdin { + pub const fn new() -> Stdin { Stdin } } @@ -17,7 +17,7 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> Stdout { + pub const fn new() -> Stdout { Stdout } } @@ -33,7 +33,7 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> Stderr { + pub const fn new() -> Stderr { Stderr } } diff --git a/library/std/src/sys/vxworks/stdio.rs b/library/std/src/sys/vxworks/stdio.rs index e99d2d583467e..92e9f205b4e6e 100644 --- a/library/std/src/sys/vxworks/stdio.rs +++ b/library/std/src/sys/vxworks/stdio.rs @@ -6,7 +6,7 @@ pub struct Stdout(()); pub struct Stderr(()); impl Stdin { - pub fn new() -> Stdin { + pub const fn new() -> Stdin { Stdin(()) } } @@ -21,7 +21,7 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> Stdout { + pub const fn new() -> Stdout { Stdout(()) } } @@ -40,7 +40,7 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> Stderr { + pub const fn new() -> Stderr { Stderr(()) } } diff --git a/library/std/src/sys/wasi/stdio.rs b/library/std/src/sys/wasi/stdio.rs index cc27e2ee5833c..23baafabf7933 100644 --- a/library/std/src/sys/wasi/stdio.rs +++ b/library/std/src/sys/wasi/stdio.rs @@ -7,7 +7,7 @@ pub struct Stdout; pub struct Stderr; impl Stdin { - pub fn new() -> Stdin { + pub const fn new() -> Stdin { Stdin } @@ -33,7 +33,7 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> Stdout { + pub const fn new() -> Stdout { Stdout } @@ -62,7 +62,7 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> Stderr { + pub const fn new() -> Stderr { Stderr } diff --git a/library/std/src/sys/windows/stdio.rs b/library/std/src/sys/windows/stdio.rs index b2e5458c0d222..ff214497166be 100644 --- a/library/std/src/sys/windows/stdio.rs +++ b/library/std/src/sys/windows/stdio.rs @@ -131,7 +131,7 @@ fn write_u16s(handle: c::HANDLE, data: &[u16]) -> io::Result { } impl Stdin { - pub fn new() -> Stdin { + pub const fn new() -> Stdin { Stdin { surrogate: 0 } } } @@ -255,7 +255,7 @@ fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result { } impl Stdout { - pub fn new() -> Stdout { + pub const fn new() -> Stdout { Stdout } } @@ -271,7 +271,7 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> Stderr { + pub const fn new() -> Stderr { Stderr } } diff --git a/library/std/src/sys/windows/stdio_uwp.rs b/library/std/src/sys/windows/stdio_uwp.rs index 0016f5dcd0112..872511af862a7 100644 --- a/library/std/src/sys/windows/stdio_uwp.rs +++ b/library/std/src/sys/windows/stdio_uwp.rs @@ -30,7 +30,7 @@ fn write(handle_id: c::DWORD, data: &[u8]) -> io::Result { } impl Stdin { - pub fn new() -> Stdin { + pub const fn new() -> Stdin { Stdin {} } } @@ -44,7 +44,7 @@ impl io::Read for Stdin { } impl Stdout { - pub fn new() -> Stdout { + pub const fn new() -> Stdout { Stdout } } @@ -60,7 +60,7 @@ impl io::Write for Stdout { } impl Stderr { - pub fn new() -> Stderr { + pub const fn new() -> Stderr { Stderr } } 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 3/3] 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 } }