Skip to content

Commit

Permalink
Ensure that Error cannot be exhaustively matched on
Browse files Browse the repository at this point in the history
This is going to be the "generic thing which can fail in a myriad of
ways" type. I want to be able to have specific useful cases which you
can recover from (e.g. no `Box<std::error::Error>`), but I don't want
people to exhaustively match on it.

Techincally adding a variant to this is still a violation of
[RFC #1105](https://github.com/rust-lang/rfcs/blob/master/text/1105-api-evolution.md),
but if you're actually matching against `__Nonexhaustive` and expected
it not to break eventually, you knew what was coming.

If some form of [RFC #757](rust-lang/rfcs#757)
ever lands, we'll switch to using that instead.
  • Loading branch information
sgrif committed Nov 29, 2015
1 parent f081727 commit 5435e30
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Change Log
All user visible changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/), as described
for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/text/1105-api-evolution.md)


## Unreleased

### Changed

* Added a hidden `__Nonexhaustive` variant to `result::Error`. This is not
intended to be something you can exhaustively match on, but I do want people
to be able to check for specific cases, so `Box<std::error::Error>` is
not an option.

## [0.1.0] - 2015-11-29

* Initial release
4 changes: 4 additions & 0 deletions diesel/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::ffi::NulError;
pub enum Error {
InvalidCString(NulError),
DatabaseError(String),
#[doc(hidden)]
__Nonexhaustive,
}

#[derive(Debug)]
Expand Down Expand Up @@ -57,6 +59,7 @@ impl Display for Error {
match self {
&Error::InvalidCString(ref nul_err) => nul_err.fmt(f),
&Error::DatabaseError(ref s) => write!(f, "{}", &s),
&Error::__Nonexhaustive => unreachable!(),
}
}
}
Expand All @@ -66,6 +69,7 @@ impl StdError for Error {
match self {
&Error::InvalidCString(ref nul_err) => nul_err.description(),
&Error::DatabaseError(ref s) => &s,
&Error::__Nonexhaustive => unreachable!(),
}
}
}
Expand Down

0 comments on commit 5435e30

Please sign in to comment.