Skip to content

Commit ebc03f7

Browse files
committed
Auto merge of #75772 - tmiasko:io-maybe-no, r=dtolnay
Remove unused `Maybe` wrapper around raw standard streams * Remove result type from raw standard streams constructors * Make raw standard stream constructors const * Remove wrapper type handling absent raw standard streams cargo checked with: ```shell env CC=true ./x.py check library/std/ \ --target i686-unknown-linux-gnu \ --target wasm32-unknown-emscripten \ --target wasm32-wasi \ --target x86_64-fortanix-unknown-sgx \ --target x86_64-pc-windows-gnu \ --target x86_64-unknown-cloudabi \ --target x86_64-unknown-hermit \ --target x86_64-unknown-linux-gnu \ --target x86_64-uwp-windows-gnu \ --target x86_64-wrs-vxworks ``` Note: Last target doesn't compile currently.
2 parents e3a4b16 + 78e0946 commit ebc03f7

File tree

10 files changed

+103
-175
lines changed

10 files changed

+103
-175
lines changed

library/std/src/io/stdio.rs

+42-114
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ struct StderrRaw(stdio::Stderr);
5050
/// handles is **not** available to raw handles returned from this function.
5151
///
5252
/// The returned handle has no external synchronization or buffering.
53-
fn stdin_raw() -> io::Result<StdinRaw> {
54-
stdio::Stdin::new().map(StdinRaw)
53+
#[unstable(feature = "libstd_sys_internals", issue = "none")]
54+
const fn stdin_raw() -> StdinRaw {
55+
StdinRaw(stdio::Stdin::new())
5556
}
5657

5758
/// Constructs a new raw handle to the standard output stream of this process.
@@ -63,8 +64,9 @@ fn stdin_raw() -> io::Result<StdinRaw> {
6364
///
6465
/// The returned handle has no external synchronization or buffering layered on
6566
/// top.
66-
fn stdout_raw() -> io::Result<StdoutRaw> {
67-
stdio::Stdout::new().map(StdoutRaw)
67+
#[unstable(feature = "libstd_sys_internals", issue = "none")]
68+
const fn stdout_raw() -> StdoutRaw {
69+
StdoutRaw(stdio::Stdout::new())
6870
}
6971

7072
/// Constructs a new raw handle to the standard error stream of this process.
@@ -74,17 +76,18 @@ fn stdout_raw() -> io::Result<StdoutRaw> {
7476
///
7577
/// The returned handle has no external synchronization or buffering layered on
7678
/// top.
77-
fn stderr_raw() -> io::Result<StderrRaw> {
78-
stdio::Stderr::new().map(StderrRaw)
79+
#[unstable(feature = "libstd_sys_internals", issue = "none")]
80+
const fn stderr_raw() -> StderrRaw {
81+
StderrRaw(stdio::Stderr::new())
7982
}
8083

8184
impl Read for StdinRaw {
8285
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
83-
self.0.read(buf)
86+
handle_ebadf(self.0.read(buf), 0)
8487
}
8588

8689
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
87-
self.0.read_vectored(bufs)
90+
handle_ebadf(self.0.read_vectored(bufs), 0)
8891
}
8992

9093
#[inline]
@@ -98,25 +101,22 @@ impl Read for StdinRaw {
98101
}
99102

100103
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
101-
self.0.read_to_end(buf)
104+
handle_ebadf(self.0.read_to_end(buf), 0)
102105
}
103106

104107
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
105-
self.0.read_to_string(buf)
106-
}
107-
108-
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
109-
self.0.read_exact(buf)
108+
handle_ebadf(self.0.read_to_string(buf), 0)
110109
}
111110
}
112111

113112
impl Write for StdoutRaw {
114113
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
115-
self.0.write(buf)
114+
handle_ebadf(self.0.write(buf), buf.len())
116115
}
117116

118117
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
119-
self.0.write_vectored(bufs)
118+
let total = bufs.iter().map(|b| b.len()).sum();
119+
handle_ebadf(self.0.write_vectored(bufs), total)
120120
}
121121

122122
#[inline]
@@ -125,29 +125,30 @@ impl Write for StdoutRaw {
125125
}
126126

127127
fn flush(&mut self) -> io::Result<()> {
128-
self.0.flush()
128+
handle_ebadf(self.0.flush(), ())
129129
}
130130

131131
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
132-
self.0.write_all(buf)
132+
handle_ebadf(self.0.write_all(buf), ())
133133
}
134134

135135
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
136-
self.0.write_all_vectored(bufs)
136+
handle_ebadf(self.0.write_all_vectored(bufs), ())
137137
}
138138

139139
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
140-
self.0.write_fmt(fmt)
140+
handle_ebadf(self.0.write_fmt(fmt), ())
141141
}
142142
}
143143

144144
impl Write for StderrRaw {
145145
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
146-
self.0.write(buf)
146+
handle_ebadf(self.0.write(buf), buf.len())
147147
}
148148

149149
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
150-
self.0.write_vectored(bufs)
150+
let total = bufs.iter().map(|b| b.len()).sum();
151+
handle_ebadf(self.0.write_vectored(bufs), total)
151152
}
152153

153154
#[inline]
@@ -156,80 +157,19 @@ impl Write for StderrRaw {
156157
}
157158

158159
fn flush(&mut self) -> io::Result<()> {
159-
self.0.flush()
160+
handle_ebadf(self.0.flush(), ())
160161
}
161162

162163
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
163-
self.0.write_all(buf)
164+
handle_ebadf(self.0.write_all(buf), ())
164165
}
165166

166167
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
167-
self.0.write_all_vectored(bufs)
168+
handle_ebadf(self.0.write_all_vectored(bufs), ())
168169
}
169170

170171
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
171-
self.0.write_fmt(fmt)
172-
}
173-
}
174-
175-
enum Maybe<T> {
176-
Real(T),
177-
Fake,
178-
}
179-
180-
impl<W: io::Write> io::Write for Maybe<W> {
181-
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
182-
match *self {
183-
Maybe::Real(ref mut w) => handle_ebadf(w.write(buf), buf.len()),
184-
Maybe::Fake => Ok(buf.len()),
185-
}
186-
}
187-
188-
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
189-
let total = bufs.iter().map(|b| b.len()).sum();
190-
match self {
191-
Maybe::Real(w) => handle_ebadf(w.write_vectored(bufs), total),
192-
Maybe::Fake => Ok(total),
193-
}
194-
}
195-
196-
#[inline]
197-
fn is_write_vectored(&self) -> bool {
198-
match self {
199-
Maybe::Real(w) => w.is_write_vectored(),
200-
Maybe::Fake => true,
201-
}
202-
}
203-
204-
fn flush(&mut self) -> io::Result<()> {
205-
match *self {
206-
Maybe::Real(ref mut w) => handle_ebadf(w.flush(), ()),
207-
Maybe::Fake => Ok(()),
208-
}
209-
}
210-
}
211-
212-
impl<R: io::Read> io::Read for Maybe<R> {
213-
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
214-
match *self {
215-
Maybe::Real(ref mut r) => handle_ebadf(r.read(buf), 0),
216-
Maybe::Fake => Ok(0),
217-
}
218-
}
219-
220-
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
221-
match self {
222-
Maybe::Real(r) => handle_ebadf(r.read_vectored(bufs), 0),
223-
Maybe::Fake => Ok(0),
224-
}
225-
}
226-
227-
#[inline]
228-
fn is_read_vectored(&self) -> bool {
229-
match self {
230-
Maybe::Real(w) => w.is_read_vectored(),
231-
Maybe::Fake => true,
232-
}
172+
handle_ebadf(self.0.write_fmt(fmt), ())
233173
}
234174
}
235175

@@ -274,7 +214,7 @@ fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
274214
/// ```
275215
#[stable(feature = "rust1", since = "1.0.0")]
276216
pub struct Stdin {
277-
inner: Arc<Mutex<BufReader<Maybe<StdinRaw>>>>,
217+
inner: Arc<Mutex<BufReader<StdinRaw>>>,
278218
}
279219

280220
/// A locked reference to the `Stdin` handle.
@@ -305,7 +245,7 @@ pub struct Stdin {
305245
/// ```
306246
#[stable(feature = "rust1", since = "1.0.0")]
307247
pub struct StdinLock<'a> {
308-
inner: MutexGuard<'a, BufReader<Maybe<StdinRaw>>>,
248+
inner: MutexGuard<'a, BufReader<StdinRaw>>,
309249
}
310250

311251
/// Constructs a new handle to the standard input of the current process.
@@ -349,18 +289,14 @@ pub struct StdinLock<'a> {
349289
/// ```
350290
#[stable(feature = "rust1", since = "1.0.0")]
351291
pub fn stdin() -> Stdin {
352-
static INSTANCE: Lazy<Mutex<BufReader<Maybe<StdinRaw>>>> = Lazy::new();
292+
static INSTANCE: Lazy<Mutex<BufReader<StdinRaw>>> = Lazy::new();
353293
return Stdin {
354294
inner: unsafe { INSTANCE.get(stdin_init).expect("cannot access stdin during shutdown") },
355295
};
356296

357-
fn stdin_init() -> Arc<Mutex<BufReader<Maybe<StdinRaw>>>> {
297+
fn stdin_init() -> Arc<Mutex<BufReader<StdinRaw>>> {
358298
// This must not reentrantly access `INSTANCE`
359-
let stdin = match stdin_raw() {
360-
Ok(stdin) => Maybe::Real(stdin),
361-
_ => Maybe::Fake,
362-
};
363-
299+
let stdin = stdin_raw();
364300
Arc::new(Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin)))
365301
}
366302
}
@@ -537,7 +473,7 @@ pub struct Stdout {
537473
// FIXME: this should be LineWriter or BufWriter depending on the state of
538474
// stdout (tty or not). Note that if this is not line buffered it
539475
// should also flush-on-panic or some form of flush-on-abort.
540-
inner: Arc<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>>,
476+
inner: Arc<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>>,
541477
}
542478

543479
/// A locked reference to the `Stdout` handle.
@@ -551,7 +487,7 @@ pub struct Stdout {
551487
/// an error.
552488
#[stable(feature = "rust1", since = "1.0.0")]
553489
pub struct StdoutLock<'a> {
554-
inner: ReentrantMutexGuard<'a, RefCell<LineWriter<Maybe<StdoutRaw>>>>,
490+
inner: ReentrantMutexGuard<'a, RefCell<LineWriter<StdoutRaw>>>,
555491
}
556492

557493
/// Constructs a new handle to the standard output of the current process.
@@ -595,17 +531,14 @@ pub struct StdoutLock<'a> {
595531
/// ```
596532
#[stable(feature = "rust1", since = "1.0.0")]
597533
pub fn stdout() -> Stdout {
598-
static INSTANCE: Lazy<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>> = Lazy::new();
534+
static INSTANCE: Lazy<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = Lazy::new();
599535
return Stdout {
600536
inner: unsafe { INSTANCE.get(stdout_init).expect("cannot access stdout during shutdown") },
601537
};
602538

603-
fn stdout_init() -> Arc<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>> {
539+
fn stdout_init() -> Arc<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> {
604540
// This must not reentrantly access `INSTANCE`
605-
let stdout = match stdout_raw() {
606-
Ok(stdout) => Maybe::Real(stdout),
607-
_ => Maybe::Fake,
608-
};
541+
let stdout = stdout_raw();
609542
unsafe {
610543
let ret = Arc::new(ReentrantMutex::new(RefCell::new(LineWriter::new(stdout))));
611544
ret.init();
@@ -715,7 +648,7 @@ impl fmt::Debug for StdoutLock<'_> {
715648
/// an error.
716649
#[stable(feature = "rust1", since = "1.0.0")]
717650
pub struct Stderr {
718-
inner: &'static ReentrantMutex<RefCell<Maybe<StderrRaw>>>,
651+
inner: &'static ReentrantMutex<RefCell<StderrRaw>>,
719652
}
720653

721654
/// A locked reference to the `Stderr` handle.
@@ -729,7 +662,7 @@ pub struct Stderr {
729662
/// an error.
730663
#[stable(feature = "rust1", since = "1.0.0")]
731664
pub struct StderrLock<'a> {
732-
inner: ReentrantMutexGuard<'a, RefCell<Maybe<StderrRaw>>>,
665+
inner: ReentrantMutexGuard<'a, RefCell<StderrRaw>>,
733666
}
734667

735668
/// Constructs a new handle to the standard error of the current process.
@@ -778,19 +711,14 @@ pub fn stderr() -> Stderr {
778711
//
779712
// This has the added benefit of allowing `stderr` to be usable during
780713
// process shutdown as well!
781-
static INSTANCE: ReentrantMutex<RefCell<Maybe<StderrRaw>>> =
782-
unsafe { ReentrantMutex::new(RefCell::new(Maybe::Fake)) };
714+
static INSTANCE: ReentrantMutex<RefCell<StderrRaw>> =
715+
unsafe { ReentrantMutex::new(RefCell::new(stderr_raw())) };
783716

784717
// When accessing stderr we need one-time initialization of the reentrant
785-
// mutex, followed by one-time detection of whether we actually have a
786-
// stderr handle or not. Afterwards we can just always use the now-filled-in
787-
// `INSTANCE` value.
718+
// mutex. Afterwards we can just always use the now-filled-in `INSTANCE` value.
788719
static INIT: Once = Once::new();
789720
INIT.call_once(|| unsafe {
790721
INSTANCE.init();
791-
if let Ok(stderr) = stderr_raw() {
792-
*INSTANCE.lock().borrow_mut() = Maybe::Real(stderr);
793-
}
794722
});
795723
Stderr { inner: &INSTANCE }
796724
}

library/std/src/sys/cloudabi/stdio.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ pub struct Stdout(());
66
pub struct Stderr(());
77

88
impl Stdin {
9-
pub fn new() -> io::Result<Stdin> {
10-
Ok(Stdin(()))
9+
pub const fn new() -> Stdin {
10+
Stdin(())
1111
}
1212
}
1313

@@ -18,8 +18,8 @@ impl io::Read for Stdin {
1818
}
1919

2020
impl Stdout {
21-
pub fn new() -> io::Result<Stdout> {
22-
Ok(Stdout(()))
21+
pub const fn new() -> Stdout {
22+
Stdout(())
2323
}
2424
}
2525

@@ -37,8 +37,8 @@ impl io::Write for Stdout {
3737
}
3838

3939
impl Stderr {
40-
pub fn new() -> io::Result<Stderr> {
41-
Ok(Stderr(()))
40+
pub const fn new() -> Stderr {
41+
Stderr(())
4242
}
4343
}
4444

@@ -62,5 +62,5 @@ pub fn is_ebadf(err: &io::Error) -> bool {
6262
pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
6363

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

library/std/src/sys/hermit/stdio.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pub struct Stdout;
77
pub struct Stderr;
88

99
impl Stdin {
10-
pub fn new() -> io::Result<Stdin> {
11-
Ok(Stdin)
10+
pub const fn new() -> Stdin {
11+
Stdin
1212
}
1313
}
1414

@@ -28,8 +28,8 @@ impl io::Read for Stdin {
2828
}
2929

3030
impl Stdout {
31-
pub fn new() -> io::Result<Stdout> {
32-
Ok(Stdout)
31+
pub const fn new() -> Stdout {
32+
Stdout
3333
}
3434
}
3535

@@ -69,8 +69,8 @@ impl io::Write for Stdout {
6969
}
7070

7171
impl Stderr {
72-
pub fn new() -> io::Result<Stderr> {
73-
Ok(Stderr)
72+
pub const fn new() -> Stderr {
73+
Stderr
7474
}
7575
}
7676

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

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

0 commit comments

Comments
 (0)