Skip to content

Commit 78e0946

Browse files
committed
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.
1 parent 4a00421 commit 78e0946

File tree

1 file changed

+33
-100
lines changed

1 file changed

+33
-100
lines changed

library/std/src/io/stdio.rs

+33-100
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ const fn stderr_raw() -> StderrRaw {
8383

8484
impl Read for StdinRaw {
8585
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
86-
self.0.read(buf)
86+
handle_ebadf(self.0.read(buf), 0)
8787
}
8888

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

9393
#[inline]
@@ -101,25 +101,22 @@ impl Read for StdinRaw {
101101
}
102102

103103
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
104-
self.0.read_to_end(buf)
104+
handle_ebadf(self.0.read_to_end(buf), 0)
105105
}
106106

107107
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
108-
self.0.read_to_string(buf)
109-
}
110-
111-
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
112-
self.0.read_exact(buf)
108+
handle_ebadf(self.0.read_to_string(buf), 0)
113109
}
114110
}
115111

116112
impl Write for StdoutRaw {
117113
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
118-
self.0.write(buf)
114+
handle_ebadf(self.0.write(buf), buf.len())
119115
}
120116

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

125122
#[inline]
@@ -128,29 +125,30 @@ impl Write for StdoutRaw {
128125
}
129126

130127
fn flush(&mut self) -> io::Result<()> {
131-
self.0.flush()
128+
handle_ebadf(self.0.flush(), ())
132129
}
133130

134131
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
135-
self.0.write_all(buf)
132+
handle_ebadf(self.0.write_all(buf), ())
136133
}
137134

138135
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
139-
self.0.write_all_vectored(bufs)
136+
handle_ebadf(self.0.write_all_vectored(bufs), ())
140137
}
141138

142139
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
143-
self.0.write_fmt(fmt)
140+
handle_ebadf(self.0.write_fmt(fmt), ())
144141
}
145142
}
146143

147144
impl Write for StderrRaw {
148145
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
149-
self.0.write(buf)
146+
handle_ebadf(self.0.write(buf), buf.len())
150147
}
151148

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

156154
#[inline]
@@ -159,80 +157,19 @@ impl Write for StderrRaw {
159157
}
160158

161159
fn flush(&mut self) -> io::Result<()> {
162-
self.0.flush()
160+
handle_ebadf(self.0.flush(), ())
163161
}
164162

165163
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
166-
self.0.write_all(buf)
164+
handle_ebadf(self.0.write_all(buf), ())
167165
}
168166

169167
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
170-
self.0.write_all_vectored(bufs)
168+
handle_ebadf(self.0.write_all_vectored(bufs), ())
171169
}
172170

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

@@ -277,7 +214,7 @@ fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
277214
/// ```
278215
#[stable(feature = "rust1", since = "1.0.0")]
279216
pub struct Stdin {
280-
inner: Arc<Mutex<BufReader<Maybe<StdinRaw>>>>,
217+
inner: Arc<Mutex<BufReader<StdinRaw>>>,
281218
}
282219

283220
/// A locked reference to the `Stdin` handle.
@@ -308,7 +245,7 @@ pub struct Stdin {
308245
/// ```
309246
#[stable(feature = "rust1", since = "1.0.0")]
310247
pub struct StdinLock<'a> {
311-
inner: MutexGuard<'a, BufReader<Maybe<StdinRaw>>>,
248+
inner: MutexGuard<'a, BufReader<StdinRaw>>,
312249
}
313250

314251
/// Constructs a new handle to the standard input of the current process.
@@ -352,14 +289,14 @@ pub struct StdinLock<'a> {
352289
/// ```
353290
#[stable(feature = "rust1", since = "1.0.0")]
354291
pub fn stdin() -> Stdin {
355-
static INSTANCE: Lazy<Mutex<BufReader<Maybe<StdinRaw>>>> = Lazy::new();
292+
static INSTANCE: Lazy<Mutex<BufReader<StdinRaw>>> = Lazy::new();
356293
return Stdin {
357294
inner: unsafe { INSTANCE.get(stdin_init).expect("cannot access stdin during shutdown") },
358295
};
359296

360-
fn stdin_init() -> Arc<Mutex<BufReader<Maybe<StdinRaw>>>> {
297+
fn stdin_init() -> Arc<Mutex<BufReader<StdinRaw>>> {
361298
// This must not reentrantly access `INSTANCE`
362-
let stdin = Maybe::Real(stdin_raw());
299+
let stdin = stdin_raw();
363300
Arc::new(Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin)))
364301
}
365302
}
@@ -536,7 +473,7 @@ pub struct Stdout {
536473
// FIXME: this should be LineWriter or BufWriter depending on the state of
537474
// stdout (tty or not). Note that if this is not line buffered it
538475
// should also flush-on-panic or some form of flush-on-abort.
539-
inner: Arc<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>>,
476+
inner: Arc<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>>,
540477
}
541478

542479
/// A locked reference to the `Stdout` handle.
@@ -550,7 +487,7 @@ pub struct Stdout {
550487
/// an error.
551488
#[stable(feature = "rust1", since = "1.0.0")]
552489
pub struct StdoutLock<'a> {
553-
inner: ReentrantMutexGuard<'a, RefCell<LineWriter<Maybe<StdoutRaw>>>>,
490+
inner: ReentrantMutexGuard<'a, RefCell<LineWriter<StdoutRaw>>>,
554491
}
555492

556493
/// Constructs a new handle to the standard output of the current process.
@@ -594,14 +531,14 @@ pub struct StdoutLock<'a> {
594531
/// ```
595532
#[stable(feature = "rust1", since = "1.0.0")]
596533
pub fn stdout() -> Stdout {
597-
static INSTANCE: Lazy<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>> = Lazy::new();
534+
static INSTANCE: Lazy<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = Lazy::new();
598535
return Stdout {
599536
inner: unsafe { INSTANCE.get(stdout_init).expect("cannot access stdout during shutdown") },
600537
};
601538

602-
fn stdout_init() -> Arc<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>> {
539+
fn stdout_init() -> Arc<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> {
603540
// This must not reentrantly access `INSTANCE`
604-
let stdout = Maybe::Real(stdout_raw());
541+
let stdout = stdout_raw();
605542
unsafe {
606543
let ret = Arc::new(ReentrantMutex::new(RefCell::new(LineWriter::new(stdout))));
607544
ret.init();
@@ -711,7 +648,7 @@ impl fmt::Debug for StdoutLock<'_> {
711648
/// an error.
712649
#[stable(feature = "rust1", since = "1.0.0")]
713650
pub struct Stderr {
714-
inner: &'static ReentrantMutex<RefCell<Maybe<StderrRaw>>>,
651+
inner: &'static ReentrantMutex<RefCell<StderrRaw>>,
715652
}
716653

717654
/// A locked reference to the `Stderr` handle.
@@ -725,7 +662,7 @@ pub struct Stderr {
725662
/// an error.
726663
#[stable(feature = "rust1", since = "1.0.0")]
727664
pub struct StderrLock<'a> {
728-
inner: ReentrantMutexGuard<'a, RefCell<Maybe<StderrRaw>>>,
665+
inner: ReentrantMutexGuard<'a, RefCell<StderrRaw>>,
729666
}
730667

731668
/// Constructs a new handle to the standard error of the current process.
@@ -774,18 +711,14 @@ pub fn stderr() -> Stderr {
774711
//
775712
// This has the added benefit of allowing `stderr` to be usable during
776713
// process shutdown as well!
777-
static INSTANCE: ReentrantMutex<RefCell<Maybe<StderrRaw>>> =
778-
unsafe { ReentrantMutex::new(RefCell::new(Maybe::Fake)) };
714+
static INSTANCE: ReentrantMutex<RefCell<StderrRaw>> =
715+
unsafe { ReentrantMutex::new(RefCell::new(stderr_raw())) };
779716

780717
// When accessing stderr we need one-time initialization of the reentrant
781-
// mutex, followed by one-time detection of whether we actually have a
782-
// stderr handle or not. Afterwards we can just always use the now-filled-in
783-
// `INSTANCE` value.
718+
// mutex. Afterwards we can just always use the now-filled-in `INSTANCE` value.
784719
static INIT: Once = Once::new();
785720
INIT.call_once(|| unsafe {
786721
INSTANCE.init();
787-
let stderr = stderr_raw();
788-
*INSTANCE.lock().borrow_mut() = Maybe::Real(stderr);
789722
});
790723
Stderr { inner: &INSTANCE }
791724
}

0 commit comments

Comments
 (0)