Skip to content

Commit b74b2bd

Browse files
committed
auto merge of #11271 : adridu59/rust/patch-io, r=huonw
2 parents ff578b7 + da3e64d commit b74b2bd

File tree

2 files changed

+58
-41
lines changed

2 files changed

+58
-41
lines changed

doc/tutorial-conditions.md

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ $ ./example numbers.txt
4343

4444
An example program that does this task reads like this:
4545

46-
~~~~{.xfail-test}
46+
~~~~
4747
# #[allow(unused_imports)];
4848
use std::io::buffered::BufferedReader;
49-
use std::io::fs::File;
49+
use std::io::File;
5050
# mod BufferedReader {
51-
# use std::io::fs::File;
51+
# use std::io::File;
5252
# use std::io::mem::MemReader;
5353
# use std::io::buffered::BufferedReader;
5454
# static s : &'static [u8] = bytes!("1 2\n\
@@ -71,10 +71,9 @@ fn main() {
7171
fn read_int_pairs() -> ~[(int,int)] {
7272
let mut pairs = ~[];
7373
74-
let args = std::os::args();
75-
7674
// Path takes a generic by-value, rather than by reference
77-
let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice());
75+
# let _g = std::io::ignore_io_error();
76+
let path = Path::new(&"foo.txt");
7877
let mut reader = BufferedReader::new(File::open(&path));
7978
8079
// 1. Iterate over the lines of our file.
@@ -242,13 +241,13 @@ If the example is rewritten to use failure, these error cases can be trapped.
242241
In this rewriting, failures are trapped by placing the I/O logic in a sub-task,
243242
and trapping its exit status using `task::try`:
244243

245-
~~~~{.xfail-test}
244+
~~~~
246245
# #[allow(unused_imports)];
247246
use std::io::buffered::BufferedReader;
248-
use std::io::fs::File;
247+
use std::io::File;
249248
use std::task;
250249
# mod BufferedReader {
251-
# use std::io::fs::File;
250+
# use std::io::File;
252251
# use std::io::mem::MemReader;
253252
# use std::io::buffered::BufferedReader;
254253
# static s : &'static [u8] = bytes!("1 2\n\
@@ -280,8 +279,8 @@ fn main() {
280279
281280
fn read_int_pairs() -> ~[(int,int)] {
282281
let mut pairs = ~[];
283-
let args = std::os::args();
284-
let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice());
282+
# let _g = std::io::ignore_io_error();
283+
let path = Path::new(&"foo.txt");
285284
286285
let mut reader = BufferedReader::new(File::open(&path));
287286
for line in reader.lines() {
@@ -346,12 +345,12 @@ If no handler is found, `Condition::raise` will fail the task with an appropriat
346345
Rewriting the example to use a condition in place of ignoring malformed lines makes it slightly longer,
347346
but similarly clear as the version that used `fail!` in the logic where the error occurs:
348347

349-
~~~~{.xfail-test}
348+
~~~~
350349
# #[allow(unused_imports)];
351350
use std::io::buffered::BufferedReader;
352-
use std::io::fs::File;
351+
use std::io::File;
353352
# mod BufferedReader {
354-
# use std::io::fs::File;
353+
# use std::io::File;
355354
# use std::io::mem::MemReader;
356355
# use std::io::buffered::BufferedReader;
357356
# static s : &'static [u8] = bytes!("1 2\n\
@@ -378,8 +377,8 @@ fn main() {
378377
379378
fn read_int_pairs() -> ~[(int,int)] {
380379
let mut pairs = ~[];
381-
let args = std::os::args();
382-
let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice());
380+
# let _g = std::io::ignore_io_error();
381+
let path = Path::new(&"foo.txt");
383382
384383
let mut reader = BufferedReader::new(File::open(&path));
385384
for line in reader.lines() {
@@ -415,12 +414,12 @@ To trap a condition, use `Condition::trap` in some caller of the site that calls
415414
For example, this version of the program traps the `malformed_line` condition
416415
and replaces bad input lines with the pair `(-1,-1)`:
417416

418-
~~~~{.xfail-test}
417+
~~~~
419418
# #[allow(unused_imports)];
420419
use std::io::buffered::BufferedReader;
421-
use std::io::fs::File;
420+
use std::io::File;
422421
# mod BufferedReader {
423-
# use std::io::fs::File;
422+
# use std::io::File;
424423
# use std::io::mem::MemReader;
425424
# use std::io::buffered::BufferedReader;
426425
# static s : &'static [u8] = bytes!("1 2\n\
@@ -452,8 +451,8 @@ fn main() {
452451
453452
fn read_int_pairs() -> ~[(int,int)] {
454453
let mut pairs = ~[];
455-
let args = std::os::args();
456-
let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice());
454+
# let _g = std::io::ignore_io_error();
455+
let path = Path::new(&"foo.txt");
457456
458457
let mut reader = BufferedReader::new(File::open(&path));
459458
for line in reader.lines() {
@@ -490,12 +489,12 @@ In the example program, the first form of the `malformed_line` API implicitly as
490489
This assumption may not be correct; some callers may wish to skip malformed lines, for example.
491490
Changing the condition's return type from `(int,int)` to `Option<(int,int)>` will suffice to support this type of recovery:
492491

493-
~~~~{.xfail-test}
492+
~~~~
494493
# #[allow(unused_imports)];
495494
use std::io::buffered::BufferedReader;
496-
use std::io::fs::File;
495+
use std::io::File;
497496
# mod BufferedReader {
498-
# use std::io::fs::File;
497+
# use std::io::File;
499498
# use std::io::mem::MemReader;
500499
# use std::io::buffered::BufferedReader;
501500
# static s : &'static [u8] = bytes!("1 2\n\
@@ -528,8 +527,8 @@ fn main() {
528527
529528
fn read_int_pairs() -> ~[(int,int)] {
530529
let mut pairs = ~[];
531-
let args = std::os::args();
532-
let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice());
530+
# let _g = std::io::ignore_io_error();
531+
let path = Path::new(&"foo.txt");
533532
534533
let mut reader = BufferedReader::new(File::open(&path));
535534
for line in reader.lines() {
@@ -575,12 +574,12 @@ until all relevant combinations encountered in practice are encoded.
575574
In the example, suppose a third possible recovery form arose: reusing the previous value read.
576575
This can be encoded in the handler API by introducing a helper type: `enum MalformedLineFix`.
577576

578-
~~~~{.xfail-test}
577+
~~~~
579578
# #[allow(unused_imports)];
580579
use std::io::buffered::BufferedReader;
581-
use std::io::fs::File;
580+
use std::io::File;
582581
# mod BufferedReader {
583-
# use std::io::fs::File;
582+
# use std::io::File;
584583
# use std::io::mem::MemReader;
585584
# use std::io::buffered::BufferedReader;
586585
# static s : &'static [u8] = bytes!("1 2\n\
@@ -622,8 +621,8 @@ fn main() {
622621
623622
fn read_int_pairs() -> ~[(int,int)] {
624623
let mut pairs = ~[];
625-
let args = std::os::args();
626-
let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice());
624+
# let _g = std::io::ignore_io_error();
625+
let path = Path::new(&"foo.txt");
627626
628627
let mut reader = BufferedReader::new(File::open(&path));
629628
for line in reader.lines() {
@@ -699,12 +698,12 @@ task <unnamed> failed at 'called `Option::unwrap()` on a `None` value', .../libs
699698
To make the program robust &mdash; or at least flexible &mdash; in the face of this potential failure,
700699
a second condition and a helper function will suffice:
701700

702-
~~~~{.xfail-test}
701+
~~~~
703702
# #[allow(unused_imports)];
704703
use std::io::buffered::BufferedReader;
705-
use std::io::fs::File;
704+
use std::io::File;
706705
# mod BufferedReader {
707-
# use std::io::fs::File;
706+
# use std::io::File;
708707
# use std::io::mem::MemReader;
709708
# use std::io::buffered::BufferedReader;
710709
# static s : &'static [u8] = bytes!("1 2\n\
@@ -760,8 +759,8 @@ fn parse_int(x: &str) -> int {
760759
761760
fn read_int_pairs() -> ~[(int,int)] {
762761
let mut pairs = ~[];
763-
let args = std::os::args();
764-
let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice());
762+
# let _g = std::io::ignore_io_error();
763+
let path = Path::new(&"foo.txt");
765764
766765
let mut reader = BufferedReader::new(File::open(&path));
767766
for line in reader.lines() {

src/libstd/io/mod.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ an error the task will fail.
194194
If you wanted to handle the error though you might write
195195
196196
let mut error = None;
197-
do io_error::cond(|e: IoError| {
197+
do io_error::cond.trap(|e: IoError| {
198198
error = Some(e);
199199
}).in {
200200
File::new("diary.txt").write_line("met a girl");
@@ -498,10 +498,16 @@ pub trait Reader {
498498
///
499499
/// # Example
500500
///
501-
/// let mut reader = BufferedReader::new(File::open(&Path::new("foo.txt")));
502-
/// for line in reader.lines() {
503-
/// println(line);
504-
/// }
501+
/// ```rust
502+
/// use std::io::buffered::BufferedReader;
503+
/// use std::io::File;
504+
/// # let _g = ::std::io::ignore_io_error();
505+
///
506+
/// let mut reader = BufferedReader::new(File::open(&Path::new("foo.txt")));
507+
/// println(reader.read_line().unwrap_or(~"Expected at least one line."));
508+
///
509+
/// if reader.eof() { println("The file is only one line long."); }
510+
/// ```
505511
///
506512
/// # Failure
507513
///
@@ -1057,6 +1063,18 @@ pub trait Buffer: Reader {
10571063
/// encoded unicode codepoints. If a newline is encountered, then the
10581064
/// newline is contained in the returned string.
10591065
///
1066+
/// # Example
1067+
///
1068+
/// ```rust
1069+
/// use std::io::buffered::BufferedReader;
1070+
/// use std::io::stdin;
1071+
/// # let _g = ::std::io::ignore_io_error();
1072+
///
1073+
/// let mut reader = BufferedReader::new(std::io::stdin());
1074+
///
1075+
/// let input = reader.read_line().unwrap_or(~"nothing");
1076+
/// ```
1077+
///
10601078
/// # Failure
10611079
///
10621080
/// This function will raise on the `io_error` condition (except for

0 commit comments

Comments
 (0)