1
Fork 0

Remove result type from raw standard streams constructors

Raw standard streams constructors are infallible. Remove unnecessary
result type.
This commit is contained in:
Tomasz Miąsko 2020-08-20 00:00:00 +00:00
parent 32cb8d40eb
commit 479c23bb49
10 changed files with 71 additions and 79 deletions

View file

@ -50,8 +50,8 @@ struct StderrRaw(stdio::Stderr);
/// handles is **not** available to raw handles returned from this function. /// handles is **not** available to raw handles returned from this function.
/// ///
/// The returned handle has no external synchronization or buffering. /// The returned handle has no external synchronization or buffering.
fn stdin_raw() -> io::Result<StdinRaw> { fn stdin_raw() -> StdinRaw {
stdio::Stdin::new().map(StdinRaw) StdinRaw(stdio::Stdin::new())
} }
/// Constructs a new raw handle to the standard output stream of this process. /// Constructs a new raw handle to the standard output stream of this process.
@ -63,8 +63,8 @@ fn stdin_raw() -> io::Result<StdinRaw> {
/// ///
/// The returned handle has no external synchronization or buffering layered on /// The returned handle has no external synchronization or buffering layered on
/// top. /// top.
fn stdout_raw() -> io::Result<StdoutRaw> { fn stdout_raw() -> StdoutRaw {
stdio::Stdout::new().map(StdoutRaw) StdoutRaw(stdio::Stdout::new())
} }
/// Constructs a new raw handle to the standard error stream of this process. /// Constructs a new raw handle to the standard error stream of this process.
@ -74,8 +74,8 @@ fn stdout_raw() -> io::Result<StdoutRaw> {
/// ///
/// The returned handle has no external synchronization or buffering layered on /// The returned handle has no external synchronization or buffering layered on
/// top. /// top.
fn stderr_raw() -> io::Result<StderrRaw> { fn stderr_raw() -> StderrRaw {
stdio::Stderr::new().map(StderrRaw) StderrRaw(stdio::Stderr::new())
} }
impl Read for StdinRaw { impl Read for StdinRaw {
@ -356,11 +356,7 @@ pub fn stdin() -> Stdin {
fn stdin_init() -> Arc<Mutex<BufReader<Maybe<StdinRaw>>>> { fn stdin_init() -> Arc<Mutex<BufReader<Maybe<StdinRaw>>>> {
// This must not reentrantly access `INSTANCE` // This must not reentrantly access `INSTANCE`
let stdin = match stdin_raw() { let stdin = Maybe::Real(stdin_raw());
Ok(stdin) => Maybe::Real(stdin),
_ => Maybe::Fake,
};
Arc::new(Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin))) Arc::new(Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin)))
} }
} }
@ -602,10 +598,7 @@ pub fn stdout() -> Stdout {
fn stdout_init() -> Arc<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>> { fn stdout_init() -> Arc<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>> {
// This must not reentrantly access `INSTANCE` // This must not reentrantly access `INSTANCE`
let stdout = match stdout_raw() { let stdout = Maybe::Real(stdout_raw());
Ok(stdout) => Maybe::Real(stdout),
_ => Maybe::Fake,
};
unsafe { unsafe {
let ret = Arc::new(ReentrantMutex::new(RefCell::new(LineWriter::new(stdout)))); let ret = Arc::new(ReentrantMutex::new(RefCell::new(LineWriter::new(stdout))));
ret.init(); ret.init();
@ -788,9 +781,8 @@ pub fn stderr() -> Stderr {
static INIT: Once = Once::new(); static INIT: Once = Once::new();
INIT.call_once(|| unsafe { INIT.call_once(|| unsafe {
INSTANCE.init(); INSTANCE.init();
if let Ok(stderr) = stderr_raw() { let stderr = stderr_raw();
*INSTANCE.lock().borrow_mut() = Maybe::Real(stderr); *INSTANCE.lock().borrow_mut() = Maybe::Real(stderr);
}
}); });
Stderr { inner: &INSTANCE } Stderr { inner: &INSTANCE }
} }

View file

@ -6,8 +6,8 @@ pub struct Stdout(());
pub struct Stderr(()); pub struct Stderr(());
impl Stdin { impl Stdin {
pub fn new() -> io::Result<Stdin> { pub fn new() -> Stdin {
Ok(Stdin(())) Stdin(())
} }
} }
@ -18,8 +18,8 @@ impl io::Read for Stdin {
} }
impl Stdout { impl Stdout {
pub fn new() -> io::Result<Stdout> { pub fn new() -> Stdout {
Ok(Stdout(())) Stdout(())
} }
} }
@ -37,8 +37,8 @@ impl io::Write for Stdout {
} }
impl Stderr { impl Stderr {
pub fn new() -> io::Result<Stderr> { pub fn new() -> Stderr {
Ok(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 const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
pub fn panic_output() -> Option<impl io::Write> { pub fn panic_output() -> Option<impl io::Write> {
Stderr::new().ok() Some(Stderr::new())
} }

View file

@ -7,8 +7,8 @@ pub struct Stdout;
pub struct Stderr; pub struct Stderr;
impl Stdin { impl Stdin {
pub fn new() -> io::Result<Stdin> { pub fn new() -> Stdin {
Ok(Stdin) Stdin
} }
} }
@ -28,8 +28,8 @@ impl io::Read for Stdin {
} }
impl Stdout { impl Stdout {
pub fn new() -> io::Result<Stdout> { pub fn new() -> Stdout {
Ok(Stdout) Stdout
} }
} }
@ -69,8 +69,8 @@ impl io::Write for Stdout {
} }
impl Stderr { impl Stderr {
pub fn new() -> io::Result<Stderr> { pub fn new() -> Stderr {
Ok(Stderr) Stderr
} }
} }
@ -116,5 +116,5 @@ pub fn is_ebadf(_err: &io::Error) -> bool {
} }
pub fn panic_output() -> Option<impl io::Write> { pub fn panic_output() -> Option<impl io::Write> {
Stderr::new().ok() Some(Stderr::new())
} }

View file

@ -19,8 +19,8 @@ fn with_std_fd<F: FnOnce(&FileDesc) -> R, R>(fd: abi::Fd, f: F) -> R {
} }
impl Stdin { impl Stdin {
pub fn new() -> io::Result<Stdin> { pub fn new() -> Stdin {
Ok(Stdin(())) Stdin(())
} }
} }
@ -31,8 +31,8 @@ impl io::Read for Stdin {
} }
impl Stdout { impl Stdout {
pub fn new() -> io::Result<Stdout> { pub fn new() -> Stdout {
Ok(Stdout(())) Stdout(())
} }
} }
@ -47,8 +47,8 @@ impl io::Write for Stdout {
} }
impl Stderr { impl Stderr {
pub fn new() -> io::Result<Stderr> { pub fn new() -> Stderr {
Ok(Stderr(())) Stderr(())
} }
} }

View file

@ -7,8 +7,8 @@ pub struct Stdout(());
pub struct Stderr(()); pub struct Stderr(());
impl Stdin { impl Stdin {
pub fn new() -> io::Result<Stdin> { pub fn new() -> Stdin {
Ok(Stdin(())) Stdin(())
} }
} }
@ -28,8 +28,8 @@ impl io::Read for Stdin {
} }
impl Stdout { impl Stdout {
pub fn new() -> io::Result<Stdout> { pub fn new() -> Stdout {
Ok(Stdout(())) Stdout(())
} }
} }
@ -53,8 +53,8 @@ impl io::Write for Stdout {
} }
impl Stderr { impl Stderr {
pub fn new() -> io::Result<Stderr> { pub fn new() -> Stderr {
Ok(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 const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
pub fn panic_output() -> Option<impl io::Write> { pub fn panic_output() -> Option<impl io::Write> {
Stderr::new().ok() Some(Stderr::new())
} }

View file

@ -5,8 +5,8 @@ pub struct Stdout;
pub struct Stderr; pub struct Stderr;
impl Stdin { impl Stdin {
pub fn new() -> io::Result<Stdin> { pub fn new() -> Stdin {
Ok(Stdin) Stdin
} }
} }
@ -17,8 +17,8 @@ impl io::Read for Stdin {
} }
impl Stdout { impl Stdout {
pub fn new() -> io::Result<Stdout> { pub fn new() -> Stdout {
Ok(Stdout) Stdout
} }
} }
@ -33,8 +33,8 @@ impl io::Write for Stdout {
} }
impl Stderr { impl Stderr {
pub fn new() -> io::Result<Stderr> { pub fn new() -> Stderr {
Ok(Stderr) Stderr
} }
} }

View file

@ -6,8 +6,8 @@ pub struct Stdout(());
pub struct Stderr(()); pub struct Stderr(());
impl Stdin { impl Stdin {
pub fn new() -> io::Result<Stdin> { pub fn new() -> Stdin {
Ok(Stdin(())) Stdin(())
} }
} }
@ -21,8 +21,8 @@ impl io::Read for Stdin {
} }
impl Stdout { impl Stdout {
pub fn new() -> io::Result<Stdout> { pub fn new() -> Stdout {
Ok(Stdout(())) Stdout(())
} }
} }
@ -40,8 +40,8 @@ impl io::Write for Stdout {
} }
impl Stderr { impl Stderr {
pub fn new() -> io::Result<Stderr> { pub fn new() -> Stderr {
Ok(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 const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
pub fn panic_output() -> Option<impl io::Write> { pub fn panic_output() -> Option<impl io::Write> {
Stderr::new().ok() Some(Stderr::new())
} }

View file

@ -7,8 +7,8 @@ pub struct Stdout;
pub struct Stderr; pub struct Stderr;
impl Stdin { impl Stdin {
pub fn new() -> io::Result<Stdin> { pub fn new() -> Stdin {
Ok(Stdin) Stdin
} }
#[inline] #[inline]
@ -33,8 +33,8 @@ impl io::Read for Stdin {
} }
impl Stdout { impl Stdout {
pub fn new() -> io::Result<Stdout> { pub fn new() -> Stdout {
Ok(Stdout) Stdout
} }
#[inline] #[inline]
@ -62,8 +62,8 @@ impl io::Write for Stdout {
} }
impl Stderr { impl Stderr {
pub fn new() -> io::Result<Stderr> { pub fn new() -> Stderr {
Ok(Stderr) Stderr
} }
#[inline] #[inline]
@ -98,5 +98,5 @@ pub fn is_ebadf(err: &io::Error) -> bool {
} }
pub fn panic_output() -> Option<impl io::Write> { pub fn panic_output() -> Option<impl io::Write> {
Stderr::new().ok() Some(Stderr::new())
} }

View file

@ -131,8 +131,8 @@ fn write_u16s(handle: c::HANDLE, data: &[u16]) -> io::Result<usize> {
} }
impl Stdin { impl Stdin {
pub fn new() -> io::Result<Stdin> { pub fn new() -> Stdin {
Ok(Stdin { surrogate: 0 }) Stdin { surrogate: 0 }
} }
} }
@ -255,8 +255,8 @@ fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
} }
impl Stdout { impl Stdout {
pub fn new() -> io::Result<Stdout> { pub fn new() -> Stdout {
Ok(Stdout) Stdout
} }
} }
@ -271,8 +271,8 @@ impl io::Write for Stdout {
} }
impl Stderr { impl Stderr {
pub fn new() -> io::Result<Stderr> { pub fn new() -> Stderr {
Ok(Stderr) Stderr
} }
} }
@ -291,5 +291,5 @@ pub fn is_ebadf(err: &io::Error) -> bool {
} }
pub fn panic_output() -> Option<impl io::Write> { pub fn panic_output() -> Option<impl io::Write> {
Stderr::new().ok() Some(Stderr::new())
} }

View file

@ -30,8 +30,8 @@ fn write(handle_id: c::DWORD, data: &[u8]) -> io::Result<usize> {
} }
impl Stdin { impl Stdin {
pub fn new() -> io::Result<Stdin> { pub fn new() -> Stdin {
Ok(Stdin {}) Stdin {}
} }
} }
@ -44,8 +44,8 @@ impl io::Read for Stdin {
} }
impl Stdout { impl Stdout {
pub fn new() -> io::Result<Stdout> { pub fn new() -> Stdout {
Ok(Stdout) Stdout
} }
} }
@ -60,8 +60,8 @@ impl io::Write for Stdout {
} }
impl Stderr { impl Stderr {
pub fn new() -> io::Result<Stderr> { pub fn new() -> Stderr {
Ok(Stderr) Stderr
} }
} }
@ -80,5 +80,5 @@ pub fn is_ebadf(err: &io::Error) -> bool {
} }
pub fn panic_output() -> Option<impl io::Write> { pub fn panic_output() -> Option<impl io::Write> {
Stderr::new().ok() Some(Stderr::new())
} }