Skip to content

Commit f283b50

Browse files
authored
Merge pull request #1319 from olehmisar/master
Remove deprecated `std::error::Error` functions and other minor changes
2 parents c49ae9e + 4c897e3 commit f283b50

File tree

6 files changed

+12
-50
lines changed

6 files changed

+12
-50
lines changed

src/error/multiple_error_types/boxing_errors.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,7 @@ impl fmt::Display for EmptyVec {
2424
}
2525
}
2626
27-
impl error::Error for EmptyVec {
28-
fn description(&self) -> &str {
29-
"invalid first item to double"
30-
}
31-
32-
fn cause(&self) -> Option<&(dyn error::Error)> {
33-
// Generic error, underlying cause isn't tracked.
34-
None
35-
}
36-
}
27+
impl error::Error for EmptyVec {}
3728
3829
fn double_first(vec: Vec<&str>) -> Result<i32> {
3930
vec.first()

src/error/multiple_error_types/define_error_type.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Rust allows us to define our own error types. In general, a "good" error type:
1616
* Composes well with other errors
1717

1818
```rust,editable
19-
use std::error;
2019
use std::fmt;
2120
2221
type Result<T> = std::result::Result<T, DoubleError>;
@@ -38,14 +37,6 @@ impl fmt::Display for DoubleError {
3837
}
3938
}
4039
41-
// This is important for other errors to wrap this one.
42-
impl error::Error for DoubleError {
43-
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
44-
// Generic error, underlying cause isn't tracked.
45-
None
46-
}
47-
}
48-
4940
fn double_first(vec: Vec<&str>) -> Result<i32> {
5041
vec.first()
5142
// Change the error to our new type.

src/error/multiple_error_types/reenter_question_mark.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,7 @@ impl fmt::Display for EmptyVec {
3838
}
3939
}
4040
41-
impl error::Error for EmptyVec {
42-
fn description(&self) -> &str {
43-
"invalid first item to double"
44-
}
45-
46-
fn cause(&self) -> Option<&error::Error> {
47-
// Generic error, underlying cause isn't tracked.
48-
None
49-
}
50-
}
41+
impl error::Error for EmptyVec {}
5142
5243
// The same structure as before but rather than chain all `Results`
5344
// and `Options` along, we `?` to get the inner value out immediately.

src/std_misc/file/create.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,23 @@ cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
1414
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
1515
";
1616
17-
use std::error::Error;
1817
use std::fs::File;
1918
use std::io::prelude::*;
2019
use std::path::Path;
2120
2221
fn main() {
23-
let path = Path::new("out/lorem_ipsum.txt");
22+
let path = Path::new("lorem_ipsum.txt");
2423
let display = path.display();
2524
2625
// Open a file in write-only mode, returns `io::Result<File>`
2726
let mut file = match File::create(&path) {
28-
Err(why) => panic!("couldn't create {}: {}", display, why.description()),
27+
Err(why) => panic!("couldn't create {}: {}", display, why),
2928
Ok(file) => file,
3029
};
3130
3231
// Write the `LOREM_IPSUM` string to `file`, returns `io::Result<()>`
3332
match file.write_all(LOREM_IPSUM.as_bytes()) {
34-
Err(why) => panic!("couldn't write to {}: {}", display, why.description()),
33+
Err(why) => panic!("couldn't write to {}: {}", display, why),
3534
Ok(_) => println!("successfully wrote to {}", display),
3635
}
3736
}
@@ -40,10 +39,9 @@ fn main() {
4039
Here's the expected successful output:
4140

4241
```shell
43-
$ mkdir out
4442
$ rustc create.rs && ./create
45-
successfully wrote to out/lorem_ipsum.txt
46-
$ cat out/lorem_ipsum.txt
43+
successfully wrote to lorem_ipsum.txt
44+
$ cat lorem_ipsum.txt
4745
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
4846
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
4947
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo

src/std_misc/file/open.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ A `File` owns a resource, the file descriptor and takes care of closing the
66
file when it is `drop`ed.
77

88
```rust,editable,ignore
9-
use std::error::Error;
109
use std::fs::File;
1110
use std::io::prelude::*;
1211
use std::path::Path;
@@ -18,24 +17,19 @@ fn main() {
1817
1918
// Open the path in read-only mode, returns `io::Result<File>`
2019
let mut file = match File::open(&path) {
21-
// The `description` method of `io::Error` returns a string that
22-
// describes the error
23-
Err(why) => panic!("couldn't open {}: {}", display,
24-
why.description()),
20+
Err(why) => panic!("couldn't open {}: {}", display, why),
2521
Ok(file) => file,
2622
};
2723
2824
// Read the file contents into a string, returns `io::Result<usize>`
2925
let mut s = String::new();
3026
match file.read_to_string(&mut s) {
31-
Err(why) => panic!("couldn't read {}: {}", display,
32-
why.description()),
27+
Err(why) => panic!("couldn't read {}: {}", display, why),
3328
Ok(_) => print!("{} contains:\n{}", display, s),
3429
}
3530
3631
// `file` goes out of scope, and the "hello.txt" file gets closed
3732
}
38-
3933
```
4034

4135
Here's the expected successful output:

src/std_misc/process/pipe.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ The `std::Child` struct represents a running child process, and exposes the
55
process via pipes.
66

77
```rust,ignore
8-
use std::error::Error;
98
use std::io::prelude::*;
109
use std::process::{Command, Stdio};
1110
@@ -18,7 +17,7 @@ fn main() {
1817
.stdin(Stdio::piped())
1918
.stdout(Stdio::piped())
2019
.spawn() {
21-
Err(why) => panic!("couldn't spawn wc: {}", why.description()),
20+
Err(why) => panic!("couldn't spawn wc: {}", why),
2221
Ok(process) => process,
2322
};
2423
@@ -27,8 +26,7 @@ fn main() {
2726
// `stdin` has type `Option<ChildStdin>`, but since we know this instance
2827
// must have one, we can directly `unwrap` it.
2928
match process.stdin.unwrap().write_all(PANGRAM.as_bytes()) {
30-
Err(why) => panic!("couldn't write to wc stdin: {}",
31-
why.description()),
29+
Err(why) => panic!("couldn't write to wc stdin: {}", why),
3230
Ok(_) => println!("sent pangram to wc"),
3331
}
3432
@@ -41,8 +39,7 @@ fn main() {
4139
// The `stdout` field also has type `Option<ChildStdout>` so must be unwrapped.
4240
let mut s = String::new();
4341
match process.stdout.unwrap().read_to_string(&mut s) {
44-
Err(why) => panic!("couldn't read wc stdout: {}",
45-
why.description()),
42+
Err(why) => panic!("couldn't read wc stdout: {}", why),
4643
Ok(_) => print!("wc responded with:\n{}", s),
4744
}
4845
}

0 commit comments

Comments
 (0)