Skip to content

Commit

Permalink
Rollup merge of rust-lang#34129 - jviide:from-string-box-error, r=ste…
Browse files Browse the repository at this point in the history
…veklabnik

Remove a gotcha from book/error-handling.md

The book's "Error handling with `Box<Error>`" section talks about `Box<Error>`. In the actual example `Box<Error + Send + Sync>` is used instead so that the corresponding From impls could be used to convert a plain string to an error type. Rust 1.7 added support for conversion from `&str`/`String` to
`Box<Error>`, so this gotcha and later references to it can now be removed.

r? @steveklabnik
  • Loading branch information
sanxiyn committed Jun 10, 2016
2 parents e1e193a + 75fc40c commit e0d4bb3
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/doc/book/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,7 @@ use std::error::Error;
fn search<P: AsRef<Path>>
(file_path: P, city: &str)
-> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> {
-> Result<Vec<PopulationCount>, Box<Error>> {
let mut found = vec![];
let file = try!(File::open(file_path));
let mut rdr = csv::Reader::from_reader(file);
Expand Down Expand Up @@ -1858,20 +1858,17 @@ Instead of `x.unwrap()`, we now have `try!(x)`. Since our function returns a
`Result<T, E>`, the `try!` macro will return early from the function if an
error occurs.

There is one big gotcha in this code: we used `Box<Error + Send + Sync>`
instead of `Box<Error>`. We did this so we could convert a plain string to an
error type. We need these extra bounds so that we can use the
[corresponding `From`
impls](../std/convert/trait.From.html):
At the end of `search` we also convert a plain string to an error type
by using the [corresponding `From` impls](../std/convert/trait.From.html):

```rust,ignore
// We are making use of this impl in the code above, since we call `From::from`
// on a `&'static str`.
impl<'a, 'b> From<&'b str> for Box<Error + Send + Sync + 'a>
impl<'a> From<&'a str> for Box<Error>
// But this is also useful when you need to allocate a new string for an
// error message, usually with `format!`.
impl From<String> for Box<Error + Send + Sync>
impl From<String> for Box<Error>
```

Since `search` now returns a `Result<T, E>`, `main` should use case analysis
Expand Down Expand Up @@ -1964,7 +1961,7 @@ use std::io;
fn search<P: AsRef<Path>>
(file_path: &Option<P>, city: &str)
-> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> {
-> Result<Vec<PopulationCount>, Box<Error>> {
let mut found = vec![];
let input: Box<io::Read> = match *file_path {
None => Box::new(io::stdin()),
Expand Down Expand Up @@ -2175,9 +2172,8 @@ heuristics!
`unwrap`. Be warned: if it winds up in someone else's hands, don't be
surprised if they are agitated by poor error messages!
* If you're writing a quick 'n' dirty program and feel ashamed about panicking
anyway, then use either a `String` or a `Box<Error + Send + Sync>` for your
error type (the `Box<Error + Send + Sync>` type is because of the
[available `From` impls](../std/convert/trait.From.html)).
anyway, then use either a `String` or a `Box<Error>` for your
error type.
* Otherwise, in a program, define your own error types with appropriate
[`From`](../std/convert/trait.From.html)
and
Expand Down

0 comments on commit e0d4bb3

Please sign in to comment.