Skip to content

Commit e912b2a

Browse files
committed
Add downcasting to std::error::Error
This commit brings the `Error` trait in line with the [Error interoperation RFC](rust-lang/rfcs#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 ```rust impl<T: Reflect> Error for MyErrorType<T> { ... } ``` This commit furthermore marks `Reflect` as stable, since we are already essentially committed to it via `Any`. Note that in the future, if we determine that the parametricity aspects of `Reflect` are not needed, we can deprecate the trait and provide a blanket implementation for it for *all* types (rather than by using OIBIT), which would allow all mentions of `Reflect` to be dropped over time. So there is not a strong commitment here. [breaking-change]
1 parent 2214860 commit e912b2a

File tree

6 files changed

+100
-16
lines changed

6 files changed

+100
-16
lines changed

src/libcore/any.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ pub trait Any: Reflect + 'static {
9797
fn get_type_id(&self) -> TypeId;
9898
}
9999

100-
impl<T> Any for T
101-
where T: Reflect + 'static
102-
{
100+
impl<T: Reflect + 'static> Any for T {
103101
fn get_type_id(&self) -> TypeId { TypeId::of::<T>() }
104102
}
105103

src/libcore/marker.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ mod impls {
459459
///
460460
/// [1]: http://en.wikipedia.org/wiki/Parametricity
461461
#[rustc_reflect_like]
462-
#[unstable(feature = "core", reason = "requires RFC and more experience")]
462+
#[stable(feature = "rust1", since = "1.0.0")]
463463
#[allow(deprecated)]
464464
#[cfg(not(stage0))]
465465
pub trait Reflect {}
@@ -471,4 +471,3 @@ pub trait Reflect {}
471471
pub trait Reflect: MarkerTrait {}
472472

473473
impl Reflect for .. { }
474-

src/libstd/error.rs

+89-4
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,21 @@
4747
// coherence challenge (e.g., specialization, neg impls, etc) we can
4848
// reconsider what crate these items belong in.
4949

50+
use any::TypeId;
5051
use boxed::Box;
5152
use convert::From;
5253
use fmt::{self, Debug, Display};
53-
use marker::{Send, Sync};
54+
use marker::{Send, Sync, Reflect};
55+
use mem::transmute;
5456
use num;
55-
use option::Option;
56-
use option::Option::None;
57+
use option::Option::{self, Some, None};
58+
use raw::TraitObject;
5759
use str;
5860
use string::{self, String};
5961

6062
/// Base functionality for all errors in Rust.
6163
#[stable(feature = "rust1", since = "1.0.0")]
62-
pub trait Error: Debug + Display {
64+
pub trait Error: Debug + Display + Reflect {
6365
/// A short description of the error.
6466
///
6567
/// The description should not contain newlines or sentence-ending
@@ -71,6 +73,14 @@ pub trait Error: Debug + Display {
7173
/// The lower-level cause of this error, if any.
7274
#[stable(feature = "rust1", since = "1.0.0")]
7375
fn cause(&self) -> Option<&Error> { None }
76+
77+
/// Get the `TypeId` of `self`
78+
#[doc(hidden)]
79+
#[unstable(feature = "core",
80+
reason = "unclear whether to commit to this public implementation detail")]
81+
fn type_id(&self) -> TypeId where Self: 'static {
82+
TypeId::of::<&'static Self>()
83+
}
7484
}
7585

7686
#[stable(feature = "rust1", since = "1.0.0")]
@@ -154,3 +164,78 @@ impl Error for string::FromUtf16Error {
154164
}
155165
}
156166

167+
// copied from any.rs
168+
impl Error + 'static {
169+
/// Returns true if the boxed type is the same as `T`
170+
#[unstable(feature = "error_downcast", reason = "recently added")]
171+
#[inline]
172+
pub fn is<T: Error + 'static>(&self) -> bool {
173+
// Get TypeId of the type this function is instantiated with
174+
let t = TypeId::of::<&'static T>();
175+
176+
// Get TypeId of the type in the trait object
177+
let boxed = self.type_id();
178+
179+
// Compare both TypeIds on equality
180+
t == boxed
181+
}
182+
183+
/// Returns some reference to the boxed value if it is of type `T`, or
184+
/// `None` if it isn't.
185+
#[unstable(feature = "error_downcast", reason = "recently added")]
186+
#[inline]
187+
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
188+
if self.is::<T>() {
189+
unsafe {
190+
// Get the raw representation of the trait object
191+
let to: TraitObject = transmute(self);
192+
193+
// Extract the data pointer
194+
Some(transmute(to.data))
195+
}
196+
} else {
197+
None
198+
}
199+
}
200+
201+
/// Returns some mutable reference to the boxed value if it is of type `T`, or
202+
/// `None` if it isn't.
203+
#[unstable(feature = "error_downcast", reason = "recently added")]
204+
#[inline]
205+
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
206+
if self.is::<T>() {
207+
unsafe {
208+
// Get the raw representation of the trait object
209+
let to: TraitObject = transmute(self);
210+
211+
// Extract the data pointer
212+
Some(transmute(to.data))
213+
}
214+
} else {
215+
None
216+
}
217+
}
218+
}
219+
220+
impl Error + 'static + Send {
221+
/// Forwards to the method defined on the type `Any`.
222+
#[unstable(feature = "error_downcast", reason = "recently added")]
223+
#[inline]
224+
pub fn is<T: Error + 'static>(&self) -> bool {
225+
<Error + 'static>::is::<T>(self)
226+
}
227+
228+
/// Forwards to the method defined on the type `Any`.
229+
#[unstable(feature = "error_downcast", reason = "recently added")]
230+
#[inline]
231+
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
232+
<Error + 'static>::downcast_ref::<T>(self)
233+
}
234+
235+
/// Forwards to the method defined on the type `Any`.
236+
#[unstable(feature = "error_downcast", reason = "recently added")]
237+
#[inline]
238+
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
239+
<Error + 'static>::downcast_mut::<T>(self)
240+
}
241+
}

src/libstd/io/buffered.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use prelude::v1::*;
1414
use io::prelude::*;
1515

16+
use marker::Reflect;
1617
use cmp;
1718
use error;
1819
use fmt;
@@ -323,7 +324,7 @@ impl<W> From<IntoInnerError<W>> for Error {
323324
}
324325

325326
#[stable(feature = "rust1", since = "1.0.0")]
326-
impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
327+
impl<W: Reflect + Send + fmt::Debug> error::Error for IntoInnerError<W> {
327328
fn description(&self) -> &str {
328329
error::Error::description(self.error())
329330
}

src/libstd/sync/mpsc/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ use error;
272272
use fmt;
273273
use mem;
274274
use cell::UnsafeCell;
275+
use marker::Reflect;
275276

276277
pub use self::select::{Select, Handle};
277278
use self::select::StartResult;
@@ -958,8 +959,7 @@ impl<T> fmt::Display for SendError<T> {
958959
}
959960

960961
#[stable(feature = "rust1", since = "1.0.0")]
961-
impl<T: Send> error::Error for SendError<T> {
962-
962+
impl<T: Send + Reflect> error::Error for SendError<T> {
963963
fn description(&self) -> &str {
964964
"sending on a closed channel"
965965
}
@@ -994,7 +994,7 @@ impl<T> fmt::Display for TrySendError<T> {
994994
}
995995

996996
#[stable(feature = "rust1", since = "1.0.0")]
997-
impl<T: Send> error::Error for TrySendError<T> {
997+
impl<T: Send + Reflect> error::Error for TrySendError<T> {
998998

999999
fn description(&self) -> &str {
10001000
match *self {

src/libstd/sys/common/poison.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use prelude::v1::*;
1212

13+
use marker::Reflect;
1314
use cell::UnsafeCell;
1415
use error::{Error};
1516
use fmt;
@@ -109,7 +110,7 @@ impl<T> fmt::Display for PoisonError<T> {
109110
}
110111
}
111112

112-
impl<T: Send> Error for PoisonError<T> {
113+
impl<T: Send + Reflect> Error for PoisonError<T> {
113114
fn description(&self) -> &str {
114115
"poisoned lock: another task failed inside"
115116
}
@@ -155,13 +156,13 @@ impl<T> fmt::Debug for TryLockError<T> {
155156
}
156157

157158
#[stable(feature = "rust1", since = "1.0.0")]
158-
impl<T: Send> fmt::Display for TryLockError<T> {
159+
impl<T: Send + Reflect> fmt::Display for TryLockError<T> {
159160
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
160161
self.description().fmt(f)
161162
}
162163
}
163164

164-
impl<T: Send> Error for TryLockError<T> {
165+
impl<T: Send + Reflect> Error for TryLockError<T> {
165166
fn description(&self) -> &str {
166167
match *self {
167168
TryLockError::Poisoned(ref p) => p.description(),

0 commit comments

Comments
 (0)