Skip to content

Commit d54bd9f

Browse files
committed
std: Stabilize the io module
The new `std::io` module has had some time to bake now, and this commit stabilizes its functionality. There are still portions of the module which remain unstable, and below contains a summart of the actions taken. This commit also deprecates the entire contents of the `old_io` module in a blanket fashion. All APIs should now have a reasonable replacement in the new I/O modules. Stable APIs: * `std::io` (the name) * `std::io::prelude` (the name) * `Read` * `Read::read` * `Read::{read_to_end, read_to_string}` after being modified to return a `usize` for the number of bytes read. * `Write` * `Write::write` * `Write::{write_all, write_fmt}` * `BufRead` * `BufRead::{fill_buf, consume}` * `BufRead::{read_line, read_until}` after being modified to return a `usize` for the number of bytes read. * `BufReader` * `BufReader::{new, with_capacity}` * `BufReader::{get_ref, get_mut, into_inner}` * `{Read,BufRead} for BufReader` * `BufWriter` * `BufWriter::{new, with_capacity}` * `BufWriter::{get_ref, get_mut, into_inner}` * `Write for BufWriter` * `IntoInnerError` * `IntoInnerError::{error, into_inner}` * `{Error,Display} for IntoInnerError` * `LineWriter` * `LineWriter::{new, with_capacity}` - `with_capacity` was added * `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added) * `Write for LineWriter` * `BufStream` * `BufStream::{new, with_capacities}` * `BufStream::{get_ref, get_mut, into_inner}` * `{BufRead,Read,Write} for BufStream` * `stdin` * `Stdin` * `Stdin::lock` * `Stdin::read_line` - added method * `StdinLock` * `Read for Stdin` * `{Read,BufRead} for StdinLock` * `stdout` * `Stdout` * `Stdout::lock` * `StdoutLock` * `Write for Stdout` * `Write for StdoutLock` * `stderr` * `Stderr` * `Stderr::lock` * `StderrLock` * `Write for Stderr` * `Write for StderrLock` * `io::Result` * `io::Error` * `io::Error::last_os_error` * `{Display, Error} for Error` Unstable APIs: (reasons can be found in the commit itself) * `Write::flush` * `Seek` * `ErrorKind` * `Error::new` * `Error::from_os_error` * `Error::kind` Deprecated APIs * `Error::description` - available via the `Error` trait * `Error::detail` - available via the `Display` implementation * `thread::Builder::{stdout, stderr}` Changes in functionality: * `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing backtraces has migrated to `std::io`. * The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed by folding functionality into the corresponding trait. [breaking-change]
1 parent 79dd393 commit d54bd9f

File tree

8 files changed

+212
-105
lines changed

8 files changed

+212
-105
lines changed

src/libstd/io/buffered.rs

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use prelude::v1::*;
1616
use io::prelude::*;
1717

1818
use cmp;
19-
use error::Error as StdError;
20-
use error::FromError;
19+
use error::{self, FromError};
2120
use fmt;
2221
use io::{self, Cursor, DEFAULT_BUF_SIZE, Error, ErrorKind};
2322
use ptr;
@@ -28,18 +27,21 @@ use ptr;
2827
/// For example, every call to `read` on `TcpStream` results in a system call.
2928
/// A `BufReader` performs large, infrequent reads on the underlying `Read`
3029
/// and maintains an in-memory buffer of the results.
30+
#[stable(feature = "rust1", since = "1.0.0")]
3131
pub struct BufReader<R> {
3232
inner: R,
3333
buf: Cursor<Vec<u8>>,
3434
}
3535

3636
impl<R: Read> BufReader<R> {
3737
/// Creates a new `BufReader` with a default buffer capacity
38+
#[stable(feature = "rust1", since = "1.0.0")]
3839
pub fn new(inner: R) -> BufReader<R> {
3940
BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
4041
}
4142

4243
/// Creates a new `BufReader` with the specified buffer capacity
44+
#[stable(feature = "rust1", since = "1.0.0")]
4345
pub fn with_capacity(cap: usize, inner: R) -> BufReader<R> {
4446
BufReader {
4547
inner: inner,
@@ -48,21 +50,25 @@ impl<R: Read> BufReader<R> {
4850
}
4951

5052
/// Gets a reference to the underlying reader.
53+
#[stable(feature = "rust1", since = "1.0.0")]
5154
pub fn get_ref(&self) -> &R { &self.inner }
5255

5356
/// Gets a mutable reference to the underlying reader.
5457
///
5558
/// # Warning
5659
///
5760
/// It is inadvisable to directly read from the underlying reader.
61+
#[stable(feature = "rust1", since = "1.0.0")]
5862
pub fn get_mut(&mut self) -> &mut R { &mut self.inner }
5963

6064
/// Unwraps this `BufReader`, returning the underlying reader.
6165
///
6266
/// Note that any leftover data in the internal buffer is lost.
67+
#[stable(feature = "rust1", since = "1.0.0")]
6368
pub fn into_inner(self) -> R { self.inner }
6469
}
6570

71+
#[stable(feature = "rust1", since = "1.0.0")]
6672
impl<R: Read> Read for BufReader<R> {
6773
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
6874
// If we don't have any buffered data and we're doing a massive read
@@ -77,6 +83,7 @@ impl<R: Read> Read for BufReader<R> {
7783
}
7884
}
7985

86+
#[stable(feature = "rust1", since = "1.0.0")]
8087
impl<R: Read> BufRead for BufReader<R> {
8188
fn fill_buf(&mut self) -> io::Result<&[u8]> {
8289
// If we've reached the end of our internal buffer then we need to fetch
@@ -112,6 +119,7 @@ impl<R> fmt::Debug for BufReader<R> where R: fmt::Debug {
112119
/// underlying `Write` in large, infrequent batches.
113120
///
114121
/// This writer will be flushed when it is dropped.
122+
#[stable(feature = "rust1", since = "1.0.0")]
115123
pub struct BufWriter<W> {
116124
inner: Option<W>,
117125
buf: Vec<u8>,
@@ -120,15 +128,18 @@ pub struct BufWriter<W> {
120128
/// An error returned by `into_inner` which indicates whether a flush error
121129
/// happened or not.
122130
#[derive(Debug)]
131+
#[stable(feature = "rust1", since = "1.0.0")]
123132
pub struct IntoInnerError<W>(W, Error);
124133

125134
impl<W: Write> BufWriter<W> {
126135
/// Creates a new `BufWriter` with a default buffer capacity
136+
#[stable(feature = "rust1", since = "1.0.0")]
127137
pub fn new(inner: W) -> BufWriter<W> {
128138
BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
129139
}
130140

131141
/// Creates a new `BufWriter` with the specified buffer capacity
142+
#[stable(feature = "rust1", since = "1.0.0")]
132143
pub fn with_capacity(cap: usize, inner: W) -> BufWriter<W> {
133144
BufWriter {
134145
inner: Some(inner),
@@ -165,18 +176,21 @@ impl<W: Write> BufWriter<W> {
165176
}
166177

167178
/// Gets a reference to the underlying writer.
179+
#[stable(feature = "rust1", since = "1.0.0")]
168180
pub fn get_ref(&self) -> &W { self.inner.as_ref().unwrap() }
169181

170182
/// Gets a mutable reference to the underlying write.
171183
///
172184
/// # Warning
173185
///
174186
/// It is inadvisable to directly read from the underlying writer.
187+
#[stable(feature = "rust1", since = "1.0.0")]
175188
pub fn get_mut(&mut self) -> &mut W { self.inner.as_mut().unwrap() }
176189

177190
/// Unwraps this `BufWriter`, returning the underlying writer.
178191
///
179192
/// The buffer is flushed before returning the writer.
193+
#[stable(feature = "rust1", since = "1.0.0")]
180194
pub fn into_inner(mut self) -> Result<W, IntoInnerError<BufWriter<W>>> {
181195
match self.flush_buf() {
182196
Err(e) => Err(IntoInnerError(self, e)),
@@ -185,6 +199,7 @@ impl<W: Write> BufWriter<W> {
185199
}
186200
}
187201

202+
#[stable(feature = "rust1", since = "1.0.0")]
188203
impl<W: Write> Write for BufWriter<W> {
189204
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
190205
if self.buf.len() + buf.len() > self.buf.capacity() {
@@ -224,23 +239,30 @@ impl<W> IntoInnerError<W> {
224239
/// Returns the error which caused the call to `into_inner` to fail.
225240
///
226241
/// This error was returned when attempting to flush the internal buffer.
242+
#[stable(feature = "rust1", since = "1.0.0")]
227243
pub fn error(&self) -> &Error { &self.1 }
228244

229245
/// Returns the underlying `BufWriter` instance which generated the error.
230246
///
231247
/// The returned object can be used to retry a flush or re-inspect the
232248
/// buffer.
249+
#[stable(feature = "rust1", since = "1.0.0")]
233250
pub fn into_inner(self) -> W { self.0 }
234251
}
235252

253+
#[stable(feature = "rust1", since = "1.0.0")]
236254
impl<W> FromError<IntoInnerError<W>> for Error {
237255
fn from_error(iie: IntoInnerError<W>) -> Error { iie.1 }
238256
}
239257

240-
impl<W> StdError for IntoInnerError<W> {
241-
fn description(&self) -> &str { self.error().description() }
258+
#[stable(feature = "rust1", since = "1.0.0")]
259+
impl<W> error::Error for IntoInnerError<W> {
260+
fn description(&self) -> &str {
261+
error::Error::description(self.error())
262+
}
242263
}
243264

265+
#[stable(feature = "rust1", since = "1.0.0")]
244266
impl<W> fmt::Display for IntoInnerError<W> {
245267
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
246268
self.error().fmt(f)
@@ -251,33 +273,49 @@ impl<W> fmt::Display for IntoInnerError<W> {
251273
/// (`0x0a`, `'\n'`) is detected.
252274
///
253275
/// This writer will be flushed when it is dropped.
276+
#[stable(feature = "rust1", since = "1.0.0")]
254277
pub struct LineWriter<W> {
255278
inner: BufWriter<W>,
256279
}
257280

258281
impl<W: Write> LineWriter<W> {
259282
/// Creates a new `LineWriter`
283+
#[stable(feature = "rust1", since = "1.0.0")]
260284
pub fn new(inner: W) -> LineWriter<W> {
261285
// Lines typically aren't that long, don't use a giant buffer
262-
LineWriter { inner: BufWriter::with_capacity(1024, inner) }
286+
LineWriter::with_capacity(1024, inner)
287+
}
288+
289+
/// Creates a new `LineWriter` with a specified capacity for the internal
290+
/// buffer.
291+
#[stable(feature = "rust1", since = "1.0.0")]
292+
pub fn with_capacity(cap: usize, inner: W) -> LineWriter<W> {
293+
LineWriter { inner: BufWriter::with_capacity(cap, inner) }
263294
}
264295

265296
/// Gets a reference to the underlying writer.
297+
#[stable(feature = "rust1", since = "1.0.0")]
298+
pub fn get_ref(&self) -> &W { self.inner.get_ref() }
299+
300+
/// Gets a mutable reference to the underlying writer.
266301
///
267-
/// This type does not expose the ability to get a mutable reference to the
268-
/// underlying reader because that could possibly corrupt the buffer.
269-
pub fn get_ref<'a>(&'a self) -> &'a W { self.inner.get_ref() }
302+
/// Caution must be taken when calling methods on the mutable reference
303+
/// returned as extra writes could corrupt the output stream.
304+
#[stable(feature = "rust1", since = "1.0.0")]
305+
pub fn get_mut(&mut self) -> &mut W { self.inner.get_mut() }
270306

271307
/// Unwraps this `LineWriter`, returning the underlying writer.
272308
///
273309
/// The internal buffer is flushed before returning the writer.
310+
#[stable(feature = "rust1", since = "1.0.0")]
274311
pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
275312
self.inner.into_inner().map_err(|IntoInnerError(buf, e)| {
276313
IntoInnerError(LineWriter { inner: buf }, e)
277314
})
278315
}
279316
}
280317

318+
#[stable(feature = "rust1", since = "1.0.0")]
281319
impl<W: Write> Write for LineWriter<W> {
282320
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
283321
match buf.rposition_elem(&b'\n') {
@@ -320,19 +358,21 @@ impl<W: Read> Read for InternalBufWriter<W> {
320358

321359
/// Wraps a Stream and buffers input and output to and from it.
322360
///
323-
/// It can be excessively inefficient to work directly with a `Stream`. For
361+
/// It can be excessively inefficient to work directly with a `Read+Write`. For
324362
/// example, every call to `read` or `write` on `TcpStream` results in a system
325363
/// call. A `BufStream` keeps in memory buffers of data, making large,
326-
/// infrequent calls to `read` and `write` on the underlying `Stream`.
364+
/// infrequent calls to `read` and `write` on the underlying `Read+Write`.
327365
///
328366
/// The output half will be flushed when this stream is dropped.
367+
#[stable(feature = "rust1", since = "1.0.0")]
329368
pub struct BufStream<S> {
330369
inner: BufReader<InternalBufWriter<S>>
331370
}
332371

333372
impl<S: Read + Write> BufStream<S> {
334373
/// Creates a new buffered stream with explicitly listed capacities for the
335374
/// reader/writer buffer.
375+
#[stable(feature = "rust1", since = "1.0.0")]
336376
pub fn with_capacities(reader_cap: usize, writer_cap: usize, inner: S)
337377
-> BufStream<S> {
338378
let writer = BufWriter::with_capacity(writer_cap, inner);
@@ -343,11 +383,13 @@ impl<S: Read + Write> BufStream<S> {
343383

344384
/// Creates a new buffered stream with the default reader/writer buffer
345385
/// capacities.
386+
#[stable(feature = "rust1", since = "1.0.0")]
346387
pub fn new(inner: S) -> BufStream<S> {
347388
BufStream::with_capacities(DEFAULT_BUF_SIZE, DEFAULT_BUF_SIZE, inner)
348389
}
349390

350391
/// Gets a reference to the underlying stream.
392+
#[stable(feature = "rust1", since = "1.0.0")]
351393
pub fn get_ref(&self) -> &S {
352394
let InternalBufWriter(ref w) = self.inner.inner;
353395
w.get_ref()
@@ -359,6 +401,7 @@ impl<S: Read + Write> BufStream<S> {
359401
///
360402
/// It is inadvisable to read directly from or write directly to the
361403
/// underlying stream.
404+
#[stable(feature = "rust1", since = "1.0.0")]
362405
pub fn get_mut(&mut self) -> &mut S {
363406
let InternalBufWriter(ref mut w) = self.inner.inner;
364407
w.get_mut()
@@ -368,6 +411,7 @@ impl<S: Read + Write> BufStream<S> {
368411
///
369412
/// The internal buffer is flushed before returning the stream. Any leftover
370413
/// data in the read buffer is lost.
414+
#[stable(feature = "rust1", since = "1.0.0")]
371415
pub fn into_inner(self) -> Result<S, IntoInnerError<BufStream<S>>> {
372416
let BufReader { inner: InternalBufWriter(w), buf } = self.inner;
373417
w.into_inner().map_err(|IntoInnerError(w, e)| {
@@ -378,17 +422,20 @@ impl<S: Read + Write> BufStream<S> {
378422
}
379423
}
380424

425+
#[stable(feature = "rust1", since = "1.0.0")]
381426
impl<S: Read + Write> BufRead for BufStream<S> {
382427
fn fill_buf(&mut self) -> io::Result<&[u8]> { self.inner.fill_buf() }
383428
fn consume(&mut self, amt: uint) { self.inner.consume(amt) }
384429
}
385430

431+
#[stable(feature = "rust1", since = "1.0.0")]
386432
impl<S: Read + Write> Read for BufStream<S> {
387433
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
388434
self.inner.read(buf)
389435
}
390436
}
391437

438+
#[stable(feature = "rust1", since = "1.0.0")]
392439
impl<S: Read + Write> Write for BufStream<S> {
393440
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
394441
self.inner.inner.get_mut().write(buf)

src/libstd/io/error.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use boxed::Box;
1212
use clone::Clone;
13-
use error::Error as StdError;
13+
use error;
1414
use fmt;
1515
use option::Option::{self, Some, None};
1616
use result;
@@ -22,6 +22,7 @@ use sys;
2222
///
2323
/// This typedef is generally used to avoid writing out `io::Error` directly and
2424
/// is otherwise a direct mapping to `std::result::Result`.
25+
#[stable(feature = "rust1", since = "1.0.0")]
2526
pub type Result<T> = result::Result<T, Error>;
2627

2728
/// The error type for I/O operations of the `Read`, `Write`, `Seek`, and
@@ -31,6 +32,7 @@ pub type Result<T> = result::Result<T, Error>;
3132
/// `Error` can be created with crafted error messages and a particular value of
3233
/// `ErrorKind`.
3334
#[derive(PartialEq, Eq, Clone, Debug)]
35+
#[stable(feature = "rust1", since = "1.0.0")]
3436
pub struct Error {
3537
repr: Repr,
3638
}
@@ -50,6 +52,10 @@ struct Custom {
5052

5153
/// A list specifying general categories of I/O error.
5254
#[derive(Copy, PartialEq, Eq, Clone, Debug)]
55+
#[unstable(feature = "io",
56+
reason = "the interaction between OS error codes and how they map to \
57+
these names (as well as the names themselves) has not \
58+
been thoroughly thought out")]
5359
pub enum ErrorKind {
5460
/// The file was not found.
5561
FileNotFound,
@@ -96,6 +102,9 @@ pub enum ErrorKind {
96102

97103
impl Error {
98104
/// Creates a new custom error from a specified kind/description/detail.
105+
#[unstable(feature = "io", reason = "the exact makeup of an Error may
106+
change to include `Box<Error>` for \
107+
example")]
99108
pub fn new(kind: ErrorKind,
100109
description: &'static str,
101110
detail: Option<String>) -> Error {
@@ -113,16 +122,20 @@ impl Error {
113122
/// This function reads the value of `errno` for the target platform (e.g.
114123
/// `GetLastError` on Windows) and will return a corresponding instance of
115124
/// `Error` for the error code.
125+
#[stable(feature = "rust1", since = "1.0.0")]
116126
pub fn last_os_error() -> Error {
117127
Error::from_os_error(sys::os::errno() as i32)
118128
}
119129

120130
/// Creates a new instance of an `Error` from a particular OS error code.
131+
#[unstable(feature = "io",
132+
reason = "unclear whether this function is necessary")]
121133
pub fn from_os_error(code: i32) -> Error {
122134
Error { repr: Repr::Os(code) }
123135
}
124136

125137
/// Return the corresponding `ErrorKind` for this error.
138+
#[stable(feature = "rust1", since = "1.0.0")]
126139
pub fn kind(&self) -> ErrorKind {
127140
match self.repr {
128141
Repr::Os(code) => sys::decode_error_kind(code),
@@ -131,6 +144,9 @@ impl Error {
131144
}
132145

133146
/// Returns a short description for this error message
147+
#[unstable(feature = "io")]
148+
#[deprecated(since = "1.0.0", reason = "use the Error trait's description \
149+
method instead")]
134150
pub fn description(&self) -> &str {
135151
match self.repr {
136152
Repr::Os(..) => "os error",
@@ -139,6 +155,8 @@ impl Error {
139155
}
140156

141157
/// Returns a detailed error message for this error (if one is available)
158+
#[unstable(feature = "io")]
159+
#[deprecated(since = "1.0.0", reason = "use the to_string() method instead")]
142160
pub fn detail(&self) -> Option<String> {
143161
match self.repr {
144162
Repr::Os(code) => Some(sys::os::error_string(code)),
@@ -147,6 +165,7 @@ impl Error {
147165
}
148166
}
149167

168+
#[stable(feature = "rust1", since = "1.0.0")]
150169
impl fmt::Display for Error {
151170
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
152171
match self.repr {
@@ -173,7 +192,8 @@ impl fmt::Display for Error {
173192
}
174193
}
175194

176-
impl StdError for Error {
195+
#[stable(feature = "rust1", since = "1.0.0")]
196+
impl error::Error for Error {
177197
fn description(&self) -> &str {
178198
match self.repr {
179199
Repr::Os(..) => "os error",

0 commit comments

Comments
 (0)