diff options
| author | Aaron Turon <aturon@mozilla.com> | 2015-04-24 14:34:57 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-05-05 10:57:01 -0700 |
| commit | 279eaabfda25072f3ba20d4c8ea5fdd4f29382ea (patch) | |
| tree | 93f4e39f0e6a52956ed08a0dae1086f806972262 /src/liballoc | |
| parent | 850151a75709f146addd30bbbf1f23d384f0b381 (diff) | |
| download | rust-279eaabfda25072f3ba20d4c8ea5fdd4f29382ea.tar.gz rust-279eaabfda25072f3ba20d4c8ea5fdd4f29382ea.zip | |
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<T> Error for MyErrorType<T> { ... }
```
must change to something like
```rust
impl<T: Reflect> Error for MyErrorType<T> { ... }
```
except that `Reflect` is currently unstable (and should remain so for
the time being). For now, code can instead bound by `Any`:
```rust
impl<T: Any> Error for MyErrorType<T> { ... }
```
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]
Conflicts:
src/libcore/marker.rs
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/boxed.rs | 11 |
1 files changed, 8 insertions, 3 deletions
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<T: ?Sized + Hash> Hash for Box<T> { impl Box<Any> { #[inline] #[stable(feature = "rust1", since = "1.0.0")] + /// Attempt to downcast the box to a concrete type. pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> { if self.is::<T>() { unsafe { @@ -257,11 +258,15 @@ impl Box<Any> { } } -impl Box<Any+Send> { +impl Box<Any + Send> { #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> { - <Box<Any>>::downcast(self) + /// Attempt to downcast the box to a concrete type. + pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any + Send>> { + <Box<Any>>::downcast(self).map_err(|s| unsafe { + // reapply the Send marker + mem::transmute::<Box<Any>, Box<Any + Send>>(s) + }) } } |
