@@ -43,12 +43,13 @@ $ ./example numbers.txt
43
43
44
44
An example program that does this task reads like this:
45
45
46
- ~~~~ {.xfail-test}
46
+ ~~~~
47
47
# #[allow(unused_imports)];
48
+ # extern mod extra;
48
49
use std::io::buffered::BufferedReader;
49
- use std::io::fs:: File;
50
+ use std::io::File;
50
51
# mod BufferedReader {
51
- # use std::io::fs:: File;
52
+ # use std::io::File;
52
53
# use std::io::mem::MemReader;
53
54
# use std::io::buffered::BufferedReader;
54
55
# static s : &'static [u8] = bytes!("1 2\n\
@@ -71,10 +72,9 @@ fn main() {
71
72
fn read_int_pairs() -> ~[(int,int)] {
72
73
let mut pairs = ~[];
73
74
74
- let args = std::os::args();
75
-
76
75
// 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());
76
+ # let _g = std::io::ignore_io_error();
77
+ let path = Path::new(&"foo.txt");
78
78
let mut reader = BufferedReader::new(File::open(&path));
79
79
80
80
// 1. Iterate over the lines of our file.
@@ -242,13 +242,14 @@ If the example is rewritten to use failure, these error cases can be trapped.
242
242
In this rewriting, failures are trapped by placing the I/O logic in a sub-task,
243
243
and trapping its exit status using ` task::try ` :
244
244
245
- ~~~~ {.xfail-test}
245
+ ~~~~
246
246
# #[allow(unused_imports)];
247
+ # extern mod extra;
247
248
use std::io::buffered::BufferedReader;
248
- use std::io::fs:: File;
249
+ use std::io::File;
249
250
use std::task;
250
251
# mod BufferedReader {
251
- # use std::io::fs:: File;
252
+ # use std::io::File;
252
253
# use std::io::mem::MemReader;
253
254
# use std::io::buffered::BufferedReader;
254
255
# static s : &'static [u8] = bytes!("1 2\n\
@@ -280,8 +281,8 @@ fn main() {
280
281
281
282
fn read_int_pairs() -> ~[(int,int)] {
282
283
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() );
284
+ # let _g = std::io::ignore_io_error ();
285
+ let path = Path::new(&"foo.txt" );
285
286
286
287
let mut reader = BufferedReader::new(File::open(&path));
287
288
for line in reader.lines() {
@@ -346,12 +347,13 @@ If no handler is found, `Condition::raise` will fail the task with an appropriat
346
347
Rewriting the example to use a condition in place of ignoring malformed lines makes it slightly longer,
347
348
but similarly clear as the version that used ` fail! ` in the logic where the error occurs:
348
349
349
- ~~~~ {.xfail-test}
350
+ ~~~~
350
351
# #[allow(unused_imports)];
352
+ # extern mod extra;
351
353
use std::io::buffered::BufferedReader;
352
- use std::io::fs:: File;
354
+ use std::io::File;
353
355
# mod BufferedReader {
354
- # use std::io::fs:: File;
356
+ # use std::io::File;
355
357
# use std::io::mem::MemReader;
356
358
# use std::io::buffered::BufferedReader;
357
359
# static s : &'static [u8] = bytes!("1 2\n\
@@ -378,8 +380,8 @@ fn main() {
378
380
379
381
fn read_int_pairs() -> ~[(int,int)] {
380
382
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() );
383
+ # let _g = std::io::ignore_io_error ();
384
+ let path = Path::new(&"foo.txt" );
383
385
384
386
let mut reader = BufferedReader::new(File::open(&path));
385
387
for line in reader.lines() {
@@ -415,12 +417,13 @@ To trap a condition, use `Condition::trap` in some caller of the site that calls
415
417
For example, this version of the program traps the ` malformed_line ` condition
416
418
and replaces bad input lines with the pair ` (-1,-1) ` :
417
419
418
- ~~~~ {.xfail-test}
420
+ ~~~~
419
421
# #[allow(unused_imports)];
422
+ # extern mod extra;
420
423
use std::io::buffered::BufferedReader;
421
- use std::io::fs:: File;
424
+ use std::io::File;
422
425
# mod BufferedReader {
423
- # use std::io::fs:: File;
426
+ # use std::io::File;
424
427
# use std::io::mem::MemReader;
425
428
# use std::io::buffered::BufferedReader;
426
429
# static s : &'static [u8] = bytes!("1 2\n\
@@ -452,8 +455,8 @@ fn main() {
452
455
453
456
fn read_int_pairs() -> ~[(int,int)] {
454
457
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() );
458
+ # let _g = std::io::ignore_io_error ();
459
+ let path = Path::new(&"foo.txt" );
457
460
458
461
let mut reader = BufferedReader::new(File::open(&path));
459
462
for line in reader.lines() {
@@ -490,12 +493,13 @@ In the example program, the first form of the `malformed_line` API implicitly as
490
493
This assumption may not be correct; some callers may wish to skip malformed lines, for example.
491
494
Changing the condition's return type from ` (int,int) ` to ` Option<(int,int)> ` will suffice to support this type of recovery:
492
495
493
- ~~~~ {.xfail-test}
496
+ ~~~~
494
497
# #[allow(unused_imports)];
498
+ # extern mod extra;
495
499
use std::io::buffered::BufferedReader;
496
- use std::io::fs:: File;
500
+ use std::io::File;
497
501
# mod BufferedReader {
498
- # use std::io::fs:: File;
502
+ # use std::io::File;
499
503
# use std::io::mem::MemReader;
500
504
# use std::io::buffered::BufferedReader;
501
505
# static s : &'static [u8] = bytes!("1 2\n\
@@ -528,8 +532,8 @@ fn main() {
528
532
529
533
fn read_int_pairs() -> ~[(int,int)] {
530
534
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() );
535
+ # let _g = std::io::ignore_io_error ();
536
+ let path = Path::new(&"foo.txt" );
533
537
534
538
let mut reader = BufferedReader::new(File::open(&path));
535
539
for line in reader.lines() {
@@ -575,12 +579,13 @@ until all relevant combinations encountered in practice are encoded.
575
579
In the example, suppose a third possible recovery form arose: reusing the previous value read.
576
580
This can be encoded in the handler API by introducing a helper type: ` enum MalformedLineFix ` .
577
581
578
- ~~~~ {.xfail-test}
582
+ ~~~~
579
583
# #[allow(unused_imports)];
584
+ # extern mod extra;
580
585
use std::io::buffered::BufferedReader;
581
- use std::io::fs:: File;
586
+ use std::io::File;
582
587
# mod BufferedReader {
583
- # use std::io::fs:: File;
588
+ # use std::io::File;
584
589
# use std::io::mem::MemReader;
585
590
# use std::io::buffered::BufferedReader;
586
591
# static s : &'static [u8] = bytes!("1 2\n\
@@ -622,8 +627,8 @@ fn main() {
622
627
623
628
fn read_int_pairs() -> ~[(int,int)] {
624
629
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() );
630
+ # let _g = std::io::ignore_io_error ();
631
+ let path = Path::new(&"foo.txt" );
627
632
628
633
let mut reader = BufferedReader::new(File::open(&path));
629
634
for line in reader.lines() {
@@ -699,12 +704,13 @@ task <unnamed> failed at 'called `Option::unwrap()` on a `None` value', .../libs
699
704
To make the program robust &mdash ; or at least flexible &mdash ; in the face of this potential failure,
700
705
a second condition and a helper function will suffice:
701
706
702
- ~~~~ {.xfail-test}
707
+ ~~~~
703
708
# #[allow(unused_imports)];
709
+ # extern mod extra;
704
710
use std::io::buffered::BufferedReader;
705
- use std::io::fs:: File;
711
+ use std::io::File;
706
712
# mod BufferedReader {
707
- # use std::io::fs:: File;
713
+ # use std::io::File;
708
714
# use std::io::mem::MemReader;
709
715
# use std::io::buffered::BufferedReader;
710
716
# static s : &'static [u8] = bytes!("1 2\n\
@@ -760,8 +766,8 @@ fn parse_int(x: &str) -> int {
760
766
761
767
fn read_int_pairs() -> ~[(int,int)] {
762
768
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() );
769
+ # let _g = std::io::ignore_io_error ();
770
+ let path = Path::new(&"foo.txt" );
765
771
766
772
let mut reader = BufferedReader::new(File::open(&path));
767
773
for line in reader.lines() {
0 commit comments