diff options
| author | Steven Fackler <sfackler@gmail.com> | 2015-05-27 23:03:04 -0700 |
|---|---|---|
| committer | Steven Fackler <sfackler@gmail.com> | 2015-05-27 23:03:04 -0700 |
| commit | f65ba38cc4d69089575580807b262a029b53c0e2 (patch) | |
| tree | 8c84f1c8e8e0c4fe16fb88ca86fc1c164b5547b4 /src/libstd/io | |
| parent | b529a7837bcbaee4a5e9f61ee659c94af7e41f60 (diff) | |
| download | rust-f65ba38cc4d69089575580807b262a029b53c0e2.tar.gz rust-f65ba38cc4d69089575580807b262a029b53c0e2.zip | |
Add a test for downcasting
Ergonomics are a bit crappy right now because method resolution isn't smart enough to drop bounds, unfortunately.
Diffstat (limited to 'src/libstd/io')
| -rw-r--r-- | src/libstd/io/error.rs | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/libstd/io/error.rs b/src/libstd/io/error.rs index c3a773617e0..449e49c08e5 100644 --- a/src/libstd/io/error.rs +++ b/src/libstd/io/error.rs @@ -188,7 +188,7 @@ impl Error { /// If this `Error` was constructed via `new` then this function will /// return `Some`, otherwise it will return `None`. #[unstable(feature = "io_error_inner", reason = "recently added")] - pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync)> { + pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { match self.repr { Repr::Os(..) => None, Repr::Custom(ref c) => Some(&*c.error), @@ -201,7 +201,7 @@ impl Error { /// If this `Error` was constructed via `new` then this function will /// return `Some`, otherwise it will return `None`. #[unstable(feature = "io_error_inner", reason = "recently added")] - pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync)> { + pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { match self.repr { Repr::Os(..) => None, Repr::Custom(ref mut c) => Some(&mut *c.error), @@ -264,3 +264,39 @@ fn _assert_error_is_sync_send() { fn _is_sync_send<T: Sync+Send>() {} _is_sync_send::<Error>(); } + +#[cfg(test)] +mod test { + use prelude::v1::*; + use super::{Error, ErrorKind}; + use error; + use error::Error as error_Error; + use fmt; + + #[test] + fn test_downcasting() { + #[derive(Debug)] + struct TestError; + + impl fmt::Display for TestError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + Ok(()) + } + } + + impl error::Error for TestError { + fn description(&self) -> &str { + "asdf" + } + } + + // we have to call all of these UFCS style right now since method + // resolution won't implicitly drop the Send+Sync bounds + let mut err = Error::new(ErrorKind::Other, TestError); + assert!(error::Error::is::<TestError>(err.get_ref().unwrap())); + assert_eq!("asdf", err.get_ref().unwrap().description()); + assert!(error::Error::is::<TestError>(err.get_mut().unwrap())); + let extracted = err.into_inner().unwrap(); + error::Error::downcast::<TestError>(extracted).unwrap(); + } +} |
