Skip to content

Commit 7736985

Browse files
committed
Return a buffered stdin by default.
One of the most common ways to use the stdin stream is to read it line by line for a small program. In order to facilitate this common usage pattern, this commit changes the stdin() function to return a BufferedReader by default. A new `stdin_raw()` method was added to get access to the raw unbuffered stream. I have not changed the stdout or stderr methods because they are currently unable to flush in their destructor, but #12403 should have just fixed that.
1 parent 879e8aa commit 7736985

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/libstd/io/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ Some examples of obvious things you might want to do
2626
* Read lines from stdin
2727
2828
```rust
29-
use std::io::BufferedReader;
30-
use std::io::stdin;
29+
use std::io;
3130
32-
let mut stdin = BufferedReader::new(stdin());
33-
for line in stdin.lines() {
31+
for line in io::stdin().lines() {
3432
print!("{}", line);
3533
}
3634
```
@@ -1097,10 +1095,9 @@ pub trait Buffer: Reader {
10971095
/// # Example
10981096
///
10991097
/// ```rust
1100-
/// use std::io::{BufferedReader, stdin};
1101-
///
1102-
/// let mut reader = BufferedReader::new(stdin());
1098+
/// use std::io;
11031099
///
1100+
/// let mut reader = io::stdin();
11041101
/// let input = reader.read_line().ok().unwrap_or(~"nothing");
11051102
/// ```
11061103
///

src/libstd/io/stdio.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ out.write(bytes!("Hello, world!"));
3030
use container::Container;
3131
use fmt;
3232
use io::{Reader, Writer, IoResult, IoError, OtherIoError,
33-
standard_error, EndOfFile, LineBufferedWriter};
33+
standard_error, EndOfFile, LineBufferedWriter, BufferedReader};
3434
use libc;
3535
use mem::replace;
3636
use option::{Option, Some, None};
@@ -86,8 +86,21 @@ fn src<T>(fd: libc::c_int, readable: bool, f: |StdSource| -> T) -> T {
8686

8787
/// Creates a new non-blocking handle to the stdin of the current process.
8888
///
89-
/// See `stdout()` for notes about this function.
90-
pub fn stdin() -> StdReader {
89+
/// The returned handled is buffered by default with a `BufferedReader`. If
90+
/// buffered access is not desired, the `stdin_raw` function is provided to
91+
/// provided unbuffered access to stdin.
92+
///
93+
/// See `stdout()` for more notes about this function.
94+
pub fn stdin() -> BufferedReader<StdReader> {
95+
BufferedReader::new(stdin_raw())
96+
}
97+
98+
/// Creates a new non-blocking handle to the stdin of the current process.
99+
///
100+
/// Unlike `stdin()`, the returned reader is *not* a buffered reader.
101+
///
102+
/// See `stdout()` for more notes about this function.
103+
pub fn stdin_raw() -> StdReader {
91104
src(libc::STDIN_FILENO, true, |src| StdReader { inner: src })
92105
}
93106

src/test/bench/sudoku.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ fn main() {
278278
let mut sudoku = if use_default {
279279
Sudoku::from_vec(&DEFAULT_SUDOKU)
280280
} else {
281-
Sudoku::read(BufferedReader::new(io::stdin()))
281+
Sudoku::read(io::stdin())
282282
};
283283
sudoku.solve();
284284
sudoku.write(&mut io::stdout());

0 commit comments

Comments
 (0)