From afa9d6c46c96058fe644ba84b01d15426057d2c8 Mon Sep 17 00:00:00 2001 From: olehmisar Date: Sun, 22 Mar 2020 19:30:24 +0200 Subject: [PATCH 1/2] Remove deprecated `std::error::Error::description` --- src/error/multiple_error_types/boxing_errors.md | 11 +---------- .../multiple_error_types/reenter_question_mark.md | 11 +---------- src/std_misc/file/create.md | 12 +++++------- src/std_misc/file/open.md | 10 ++-------- src/std_misc/process/pipe.md | 9 +++------ 5 files changed, 12 insertions(+), 41 deletions(-) diff --git a/src/error/multiple_error_types/boxing_errors.md b/src/error/multiple_error_types/boxing_errors.md index b0fc2aa3ec..84b0c41e55 100644 --- a/src/error/multiple_error_types/boxing_errors.md +++ b/src/error/multiple_error_types/boxing_errors.md @@ -24,16 +24,7 @@ impl fmt::Display for EmptyVec { } } -impl error::Error for EmptyVec { - fn description(&self) -> &str { - "invalid first item to double" - } - - fn cause(&self) -> Option<&(dyn error::Error)> { - // Generic error, underlying cause isn't tracked. - None - } -} +impl error::Error for EmptyVec {} fn double_first(vec: Vec<&str>) -> Result { vec.first() diff --git a/src/error/multiple_error_types/reenter_question_mark.md b/src/error/multiple_error_types/reenter_question_mark.md index a94c0f8e5e..61f80fc3e3 100644 --- a/src/error/multiple_error_types/reenter_question_mark.md +++ b/src/error/multiple_error_types/reenter_question_mark.md @@ -38,16 +38,7 @@ impl fmt::Display for EmptyVec { } } -impl error::Error for EmptyVec { - fn description(&self) -> &str { - "invalid first item to double" - } - - fn cause(&self) -> Option<&error::Error> { - // Generic error, underlying cause isn't tracked. - None - } -} +impl error::Error for EmptyVec {} // The same structure as before but rather than chain all `Results` // and `Options` along, we `?` to get the inner value out immediately. diff --git a/src/std_misc/file/create.md b/src/std_misc/file/create.md index 5fa63d58db..16eba89747 100644 --- a/src/std_misc/file/create.md +++ b/src/std_misc/file/create.md @@ -14,24 +14,23 @@ cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. "; -use std::error::Error; use std::fs::File; use std::io::prelude::*; use std::path::Path; fn main() { - let path = Path::new("out/lorem_ipsum.txt"); + let path = Path::new("lorem_ipsum.txt"); let display = path.display(); // Open a file in write-only mode, returns `io::Result` let mut file = match File::create(&path) { - Err(why) => panic!("couldn't create {}: {}", display, why.description()), + Err(why) => panic!("couldn't create {}: {}", display, why), Ok(file) => file, }; // Write the `LOREM_IPSUM` string to `file`, returns `io::Result<()>` match file.write_all(LOREM_IPSUM.as_bytes()) { - Err(why) => panic!("couldn't write to {}: {}", display, why.description()), + Err(why) => panic!("couldn't write to {}: {}", display, why), Ok(_) => println!("successfully wrote to {}", display), } } @@ -40,10 +39,9 @@ fn main() { Here's the expected successful output: ```shell -$ mkdir out $ rustc create.rs && ./create -successfully wrote to out/lorem_ipsum.txt -$ cat out/lorem_ipsum.txt +successfully wrote to lorem_ipsum.txt +$ cat lorem_ipsum.txt Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo diff --git a/src/std_misc/file/open.md b/src/std_misc/file/open.md index 5056197f46..16fdb2c91c 100644 --- a/src/std_misc/file/open.md +++ b/src/std_misc/file/open.md @@ -6,7 +6,6 @@ A `File` owns a resource, the file descriptor and takes care of closing the file when it is `drop`ed. ```rust,editable,ignore -use std::error::Error; use std::fs::File; use std::io::prelude::*; use std::path::Path; @@ -18,24 +17,19 @@ fn main() { // Open the path in read-only mode, returns `io::Result` let mut file = match File::open(&path) { - // The `description` method of `io::Error` returns a string that - // describes the error - Err(why) => panic!("couldn't open {}: {}", display, - why.description()), + Err(why) => panic!("couldn't open {}: {}", display, why), Ok(file) => file, }; // Read the file contents into a string, returns `io::Result` let mut s = String::new(); match file.read_to_string(&mut s) { - Err(why) => panic!("couldn't read {}: {}", display, - why.description()), + Err(why) => panic!("couldn't read {}: {}", display, why), Ok(_) => print!("{} contains:\n{}", display, s), } // `file` goes out of scope, and the "hello.txt" file gets closed } - ``` Here's the expected successful output: diff --git a/src/std_misc/process/pipe.md b/src/std_misc/process/pipe.md index aeba29d2f5..fb0be0ea11 100644 --- a/src/std_misc/process/pipe.md +++ b/src/std_misc/process/pipe.md @@ -5,7 +5,6 @@ The `std::Child` struct represents a running child process, and exposes the process via pipes. ```rust,ignore -use std::error::Error; use std::io::prelude::*; use std::process::{Command, Stdio}; @@ -18,7 +17,7 @@ fn main() { .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn() { - Err(why) => panic!("couldn't spawn wc: {}", why.description()), + Err(why) => panic!("couldn't spawn wc: {}", why), Ok(process) => process, }; @@ -27,8 +26,7 @@ fn main() { // `stdin` has type `Option`, but since we know this instance // must have one, we can directly `unwrap` it. match process.stdin.unwrap().write_all(PANGRAM.as_bytes()) { - Err(why) => panic!("couldn't write to wc stdin: {}", - why.description()), + Err(why) => panic!("couldn't write to wc stdin: {}", why), Ok(_) => println!("sent pangram to wc"), } @@ -41,8 +39,7 @@ fn main() { // The `stdout` field also has type `Option` so must be unwrapped. let mut s = String::new(); match process.stdout.unwrap().read_to_string(&mut s) { - Err(why) => panic!("couldn't read wc stdout: {}", - why.description()), + Err(why) => panic!("couldn't read wc stdout: {}", why), Ok(_) => print!("wc responded with:\n{}", s), } } From 4c897e3feb67101b16d225562c36cfa6157986f7 Mon Sep 17 00:00:00 2001 From: olehmisar Date: Sun, 22 Mar 2020 19:43:18 +0200 Subject: [PATCH 2/2] Remove unnecessary `impl std::error::Error` --- src/error/multiple_error_types/define_error_type.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/error/multiple_error_types/define_error_type.md b/src/error/multiple_error_types/define_error_type.md index 45e5766690..4c374276dc 100644 --- a/src/error/multiple_error_types/define_error_type.md +++ b/src/error/multiple_error_types/define_error_type.md @@ -16,7 +16,6 @@ Rust allows us to define our own error types. In general, a "good" error type: * Composes well with other errors ```rust,editable -use std::error; use std::fmt; type Result = std::result::Result; @@ -38,14 +37,6 @@ impl fmt::Display for DoubleError { } } -// This is important for other errors to wrap this one. -impl error::Error for DoubleError { - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - // Generic error, underlying cause isn't tracked. - None - } -} - fn double_first(vec: Vec<&str>) -> Result { vec.first() // Change the error to our new type.