From a5762625a168f195afbc5b74d674a93f8c692a8e Mon Sep 17 00:00:00 2001 From: Aaron Turon Date: Fri, 24 Apr 2015 14:34:57 -0700 Subject: Add downcasting to std::error::Error This commit brings the `Error` trait in line with the [Error interoperation RFC](https://github.com/rust-lang/rfcs/pull/201) by adding downcasting, which has long been intended. This change means that for any `Error` trait objects that are `'static`, you can downcast to concrete error types. To make this work, it is necessary for `Error` to inherit from `Reflect` (which is currently used to mark concrete types as "permitted for reflection, aka downcasting"). This is a breaking change: it means that impls like ```rust impl Error for MyErrorType { ... } ``` must change to something like ```rust impl Error for MyErrorType { ... } ``` except that `Reflect` is currently unstable (and should remain so for the time being). For now, code can instead bound by `Any`: ```rust impl Error for MyErrorType { ... } ``` which *is* stable and has `Reflect` as a super trait. The downside is that this imposes a `'static` constraint, but that only constrains *when* `Error` is implemented -- it does not actually constrain the types that can implement `Error`. [breaking-change] --- src/liballoc/boxed.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 7696abd659f..7a089d733cf 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -240,6 +240,7 @@ impl Hash for Box { impl Box { #[inline] #[stable(feature = "rust1", since = "1.0.0")] + /// Attempt to downcast the box to a concrete type. pub fn downcast(self) -> Result, Box> { if self.is::() { unsafe { @@ -257,11 +258,15 @@ impl Box { } } -impl Box { +impl Box { #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn downcast(self) -> Result, Box> { - >::downcast(self) + /// Attempt to downcast the box to a concrete type. + pub fn downcast(self) -> Result, Box> { + >::downcast(self).map_err(|s| unsafe { + // reapply the Send marker + mem::transmute::, Box>(s) + }) } } -- cgit 1.4.1-3-g733a5