Skip to content

Commit

Permalink
Remove result type from raw standard streams constructors
Browse files Browse the repository at this point in the history
Raw standard streams constructors are infallible. Remove unnecessary
result type.
  • Loading branch information
tmiasko committed Aug 21, 2020
1 parent 32cb8d4 commit 479c23b
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 79 deletions.
28 changes: 10 additions & 18 deletions library/std/src/io/stdio.rs
Expand Up @@ -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<StdinRaw> {
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.
Expand All @@ -63,8 +63,8 @@ fn stdin_raw() -> io::Result<StdinRaw> {
///
/// The returned handle has no external synchronization or buffering layered on
/// top.
fn stdout_raw() -> io::Result<StdoutRaw> {
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.
Expand All @@ -74,8 +74,8 @@ fn stdout_raw() -> io::Result<StdoutRaw> {
///
/// The returned handle has no external synchronization or buffering layered on
/// top.
fn stderr_raw() -> io::Result<StderrRaw> {
stdio::Stderr::new().map(StderrRaw)
fn stderr_raw() -> StderrRaw {
StderrRaw(stdio::Stderr::new())
}

impl Read for StdinRaw {
Expand Down Expand Up @@ -356,11 +356,7 @@ pub fn stdin() -> Stdin {

fn stdin_init() -> Arc<Mutex<BufReader<Maybe<StdinRaw>>>> {
// 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)))
}
}
Expand Down Expand Up @@ -602,10 +598,7 @@ pub fn stdout() -> Stdout {

fn stdout_init() -> Arc<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>> {
// 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();
Expand Down Expand Up @@ -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 }
}
Expand Down
14 changes: 7 additions & 7 deletions library/std/src/sys/cloudabi/stdio.rs
Expand Up @@ -6,8 +6,8 @@ pub struct Stdout(());
pub struct Stderr(());

impl Stdin {
pub fn new() -> io::Result<Stdin> {
Ok(Stdin(()))
pub fn new() -> Stdin {
Stdin(())
}
}

Expand All @@ -18,8 +18,8 @@ impl io::Read for Stdin {
}

impl Stdout {
pub fn new() -> io::Result<Stdout> {
Ok(Stdout(()))
pub fn new() -> Stdout {
Stdout(())
}
}

Expand All @@ -37,8 +37,8 @@ impl io::Write for Stdout {
}

impl Stderr {
pub fn new() -> io::Result<Stderr> {
Ok(Stderr(()))
pub fn new() -> Stderr {
Stderr(())
}
}

Expand All @@ -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<impl io::Write> {
Stderr::new().ok()
Some(Stderr::new())
}
14 changes: 7 additions & 7 deletions library/std/src/sys/hermit/stdio.rs
Expand Up @@ -7,8 +7,8 @@ pub struct Stdout;
pub struct Stderr;

impl Stdin {
pub fn new() -> io::Result<Stdin> {
Ok(Stdin)
pub fn new() -> Stdin {
Stdin
}
}

Expand All @@ -28,8 +28,8 @@ impl io::Read for Stdin {
}

impl Stdout {
pub fn new() -> io::Result<Stdout> {
Ok(Stdout)
pub fn new() -> Stdout {
Stdout
}
}

Expand Down Expand Up @@ -69,8 +69,8 @@ impl io::Write for Stdout {
}

impl Stderr {
pub fn new() -> io::Result<Stderr> {
Ok(Stderr)
pub fn new() -> Stderr {
Stderr
}
}

Expand Down Expand Up @@ -116,5 +116,5 @@ pub fn is_ebadf(_err: &io::Error) -> bool {
}

pub fn panic_output() -> Option<impl io::Write> {
Stderr::new().ok()
Some(Stderr::new())
}
12 changes: 6 additions & 6 deletions library/std/src/sys/sgx/stdio.rs
Expand Up @@ -19,8 +19,8 @@ fn with_std_fd<F: FnOnce(&FileDesc) -> R, R>(fd: abi::Fd, f: F) -> R {
}

impl Stdin {
pub fn new() -> io::Result<Stdin> {
Ok(Stdin(()))
pub fn new() -> Stdin {
Stdin(())
}
}

Expand All @@ -31,8 +31,8 @@ impl io::Read for Stdin {
}

impl Stdout {
pub fn new() -> io::Result<Stdout> {
Ok(Stdout(()))
pub fn new() -> Stdout {
Stdout(())
}
}

Expand All @@ -47,8 +47,8 @@ impl io::Write for Stdout {
}

impl Stderr {
pub fn new() -> io::Result<Stderr> {
Ok(Stderr(()))
pub fn new() -> Stderr {
Stderr(())
}
}

Expand Down
14 changes: 7 additions & 7 deletions library/std/src/sys/unix/stdio.rs
Expand Up @@ -7,8 +7,8 @@ pub struct Stdout(());
pub struct Stderr(());

impl Stdin {
pub fn new() -> io::Result<Stdin> {
Ok(Stdin(()))
pub fn new() -> Stdin {
Stdin(())
}
}

Expand All @@ -28,8 +28,8 @@ impl io::Read for Stdin {
}

impl Stdout {
pub fn new() -> io::Result<Stdout> {
Ok(Stdout(()))
pub fn new() -> Stdout {
Stdout(())
}
}

Expand All @@ -53,8 +53,8 @@ impl io::Write for Stdout {
}

impl Stderr {
pub fn new() -> io::Result<Stderr> {
Ok(Stderr(()))
pub fn new() -> Stderr {
Stderr(())
}
}

Expand Down Expand Up @@ -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<impl io::Write> {
Stderr::new().ok()
Some(Stderr::new())
}
12 changes: 6 additions & 6 deletions library/std/src/sys/unsupported/stdio.rs
Expand Up @@ -5,8 +5,8 @@ pub struct Stdout;
pub struct Stderr;

impl Stdin {
pub fn new() -> io::Result<Stdin> {
Ok(Stdin)
pub fn new() -> Stdin {
Stdin
}
}

Expand All @@ -17,8 +17,8 @@ impl io::Read for Stdin {
}

impl Stdout {
pub fn new() -> io::Result<Stdout> {
Ok(Stdout)
pub fn new() -> Stdout {
Stdout
}
}

Expand All @@ -33,8 +33,8 @@ impl io::Write for Stdout {
}

impl Stderr {
pub fn new() -> io::Result<Stderr> {
Ok(Stderr)
pub fn new() -> Stderr {
Stderr
}
}

Expand Down
14 changes: 7 additions & 7 deletions library/std/src/sys/vxworks/stdio.rs
Expand Up @@ -6,8 +6,8 @@ pub struct Stdout(());
pub struct Stderr(());

impl Stdin {
pub fn new() -> io::Result<Stdin> {
Ok(Stdin(()))
pub fn new() -> Stdin {
Stdin(())
}
}

Expand All @@ -21,8 +21,8 @@ impl io::Read for Stdin {
}

impl Stdout {
pub fn new() -> io::Result<Stdout> {
Ok(Stdout(()))
pub fn new() -> Stdout {
Stdout(())
}
}

Expand All @@ -40,8 +40,8 @@ impl io::Write for Stdout {
}

impl Stderr {
pub fn new() -> io::Result<Stderr> {
Ok(Stderr(()))
pub fn new() -> Stderr {
Stderr(())
}
}

Expand All @@ -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<impl io::Write> {
Stderr::new().ok()
Some(Stderr::new())
}
14 changes: 7 additions & 7 deletions library/std/src/sys/wasi/stdio.rs
Expand Up @@ -7,8 +7,8 @@ pub struct Stdout;
pub struct Stderr;

impl Stdin {
pub fn new() -> io::Result<Stdin> {
Ok(Stdin)
pub fn new() -> Stdin {
Stdin
}

#[inline]
Expand All @@ -33,8 +33,8 @@ impl io::Read for Stdin {
}

impl Stdout {
pub fn new() -> io::Result<Stdout> {
Ok(Stdout)
pub fn new() -> Stdout {
Stdout
}

#[inline]
Expand Down Expand Up @@ -62,8 +62,8 @@ impl io::Write for Stdout {
}

impl Stderr {
pub fn new() -> io::Result<Stderr> {
Ok(Stderr)
pub fn new() -> Stderr {
Stderr
}

#[inline]
Expand Down Expand Up @@ -98,5 +98,5 @@ pub fn is_ebadf(err: &io::Error) -> bool {
}

pub fn panic_output() -> Option<impl io::Write> {
Stderr::new().ok()
Some(Stderr::new())
}
14 changes: 7 additions & 7 deletions library/std/src/sys/windows/stdio.rs
Expand Up @@ -131,8 +131,8 @@ fn write_u16s(handle: c::HANDLE, data: &[u16]) -> io::Result<usize> {
}

impl Stdin {
pub fn new() -> io::Result<Stdin> {
Ok(Stdin { surrogate: 0 })
pub fn new() -> Stdin {
Stdin { surrogate: 0 }
}
}

Expand Down Expand Up @@ -255,8 +255,8 @@ fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
}

impl Stdout {
pub fn new() -> io::Result<Stdout> {
Ok(Stdout)
pub fn new() -> Stdout {
Stdout
}
}

Expand All @@ -271,8 +271,8 @@ impl io::Write for Stdout {
}

impl Stderr {
pub fn new() -> io::Result<Stderr> {
Ok(Stderr)
pub fn new() -> Stderr {
Stderr
}
}

Expand All @@ -291,5 +291,5 @@ pub fn is_ebadf(err: &io::Error) -> bool {
}

pub fn panic_output() -> Option<impl io::Write> {
Stderr::new().ok()
Some(Stderr::new())
}

0 comments on commit 479c23b

Please sign in to comment.