Skip to content

Commit f65ba38

Browse files
committed
Add a test for downcasting
Ergonomics are a bit crappy right now because method resolution isn't smart enough to drop bounds, unfortunately.
1 parent b529a78 commit f65ba38

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/libstd/io/error.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl Error {
188188
/// If this `Error` was constructed via `new` then this function will
189189
/// return `Some`, otherwise it will return `None`.
190190
#[unstable(feature = "io_error_inner", reason = "recently added")]
191-
pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync)> {
191+
pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> {
192192
match self.repr {
193193
Repr::Os(..) => None,
194194
Repr::Custom(ref c) => Some(&*c.error),
@@ -201,7 +201,7 @@ impl Error {
201201
/// If this `Error` was constructed via `new` then this function will
202202
/// return `Some`, otherwise it will return `None`.
203203
#[unstable(feature = "io_error_inner", reason = "recently added")]
204-
pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync)> {
204+
pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> {
205205
match self.repr {
206206
Repr::Os(..) => None,
207207
Repr::Custom(ref mut c) => Some(&mut *c.error),
@@ -264,3 +264,39 @@ fn _assert_error_is_sync_send() {
264264
fn _is_sync_send<T: Sync+Send>() {}
265265
_is_sync_send::<Error>();
266266
}
267+
268+
#[cfg(test)]
269+
mod test {
270+
use prelude::v1::*;
271+
use super::{Error, ErrorKind};
272+
use error;
273+
use error::Error as error_Error;
274+
use fmt;
275+
276+
#[test]
277+
fn test_downcasting() {
278+
#[derive(Debug)]
279+
struct TestError;
280+
281+
impl fmt::Display for TestError {
282+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
283+
Ok(())
284+
}
285+
}
286+
287+
impl error::Error for TestError {
288+
fn description(&self) -> &str {
289+
"asdf"
290+
}
291+
}
292+
293+
// we have to call all of these UFCS style right now since method
294+
// resolution won't implicitly drop the Send+Sync bounds
295+
let mut err = Error::new(ErrorKind::Other, TestError);
296+
assert!(error::Error::is::<TestError>(err.get_ref().unwrap()));
297+
assert_eq!("asdf", err.get_ref().unwrap().description());
298+
assert!(error::Error::is::<TestError>(err.get_mut().unwrap()));
299+
let extracted = err.into_inner().unwrap();
300+
error::Error::downcast::<TestError>(extracted).unwrap();
301+
}
302+
}

0 commit comments

Comments
 (0)