Skip to content

Commit befa249

Browse files
committed
---
yaml --- r: 216463 b: refs/heads/stable c: a576262 h: refs/heads/master i: 216461: 87dd9ea 216459: 427e8e2 216455: 8f1d3e9 216447: 95c7a9e v: v3
1 parent aaaf9db commit befa249

File tree

11 files changed

+154
-89
lines changed

11 files changed

+154
-89
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ refs/heads/tmp: 378a370ff2057afeb1eae86eb6e78c476866a4a6
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f
3030
refs/tags/homu-tmp: a5286998df566e736b32f6795bfc3803bdaf453d
3131
refs/tags/1.0.0-beta: 8cbb92b53468ee2b0c2d3eeb8567005953d40828
32-
refs/heads/stable: c634ec2e88a85d7f553ec0d6746e24a886bc6fd4
32+
refs/heads/stable: a5762625a168f195afbc5b74d674a93f8c692a8e
3333
refs/tags/1.0.0: 55bd4f8ff2b323f317ae89e254ce87162d52a375

branches/stable/src/liballoc/boxed.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ impl<T: ?Sized + Hash> Hash for Box<T> {
240240
impl Box<Any> {
241241
#[inline]
242242
#[stable(feature = "rust1", since = "1.0.0")]
243+
/// Attempt to downcast the box to a concrete type.
243244
pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> {
244245
if self.is::<T>() {
245246
unsafe {
@@ -257,11 +258,15 @@ impl Box<Any> {
257258
}
258259
}
259260

260-
impl Box<Any+Send> {
261+
impl Box<Any + Send> {
261262
#[inline]
262263
#[stable(feature = "rust1", since = "1.0.0")]
263-
pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> {
264-
<Box<Any>>::downcast(self)
264+
/// Attempt to downcast the box to a concrete type.
265+
pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any + Send>> {
266+
<Box<Any>>::downcast(self).map_err(|s| unsafe {
267+
// reapply the Send marker
268+
mem::transmute::<Box<Any>, Box<Any + Send>>(s)
269+
})
265270
}
266271
}
267272

branches/stable/src/libcore/any.rs

Lines changed: 2 additions & 4 deletions
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

@@ -222,7 +220,7 @@ impl TypeId {
222220
/// Returns the `TypeId` of the type this generic function has been
223221
/// instantiated with
224222
#[stable(feature = "rust1", since = "1.0.0")]
225-
pub fn of<T: ?Sized + Any>() -> TypeId {
223+
pub fn of<T: ?Sized + Reflect + 'static>() -> TypeId {
226224
TypeId {
227225
t: unsafe { intrinsics::type_id::<T>() },
228226
}

branches/stable/src/libcore/marker.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ mod impls {
416416
#[rustc_reflect_like]
417417
#[unstable(feature = "core", reason = "requires RFC and more experience")]
418418
#[allow(deprecated)]
419+
#[rustc_on_unimplemented = "`{Self}` does not implement `Any`; \
420+
ensure all type parameters are bounded by `Any`"]
419421
pub trait Reflect {}
420422

421423
impl Reflect for .. { }

branches/stable/src/librustdoc/html/render.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,9 +1787,6 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
17871787
let types = t.items.iter().filter(|m| {
17881788
match m.inner { clean::AssociatedTypeItem(..) => true, _ => false }
17891789
}).collect::<Vec<_>>();
1790-
let consts = t.items.iter().filter(|m| {
1791-
match m.inner { clean::AssociatedConstItem(..) => true, _ => false }
1792-
}).collect::<Vec<_>>();
17931790
let required = t.items.iter().filter(|m| {
17941791
match m.inner { clean::TyMethodItem(_) => true, _ => false }
17951792
}).collect::<Vec<_>>();
@@ -1806,15 +1803,7 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
18061803
try!(render_assoc_item(w, t, AssocItemLink::Anchor));
18071804
try!(write!(w, ";\n"));
18081805
}
1809-
if !types.is_empty() && !consts.is_empty() {
1810-
try!(w.write_str("\n"));
1811-
}
1812-
for t in &consts {
1813-
try!(write!(w, " "));
1814-
try!(render_assoc_item(w, t, AssocItemLink::Anchor));
1815-
try!(write!(w, ";\n"));
1816-
}
1817-
if !consts.is_empty() && !required.is_empty() {
1806+
if !types.is_empty() && !required.is_empty() {
18181807
try!(w.write_str("\n"));
18191808
}
18201809
for m in &required {
@@ -1916,11 +1905,11 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
19161905
}
19171906

19181907
fn assoc_const(w: &mut fmt::Formatter, it: &clean::Item,
1919-
ty: &clean::Type, default: Option<&String>)
1908+
ty: &clean::Type, default: &Option<String>)
19201909
-> fmt::Result {
19211910
try!(write!(w, "const {}", it.name.as_ref().unwrap()));
19221911
try!(write!(w, ": {}", ty));
1923-
if let Some(default) = default {
1912+
if let Some(ref default) = *default {
19241913
try!(write!(w, " = {}", default));
19251914
}
19261915
Ok(())
@@ -1982,7 +1971,7 @@ fn render_assoc_item(w: &mut fmt::Formatter, meth: &clean::Item,
19821971
link)
19831972
}
19841973
clean::AssociatedConstItem(ref ty, ref default) => {
1985-
assoc_const(w, meth, ty, default.as_ref())
1974+
assoc_const(w, meth, ty, default)
19861975
}
19871976
clean::AssociatedTypeItem(ref bounds, ref default) => {
19881977
assoc_type(w, meth, bounds, default)
@@ -2346,15 +2335,9 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl, link: AssocItemLink,
23462335
clean::AssociatedConstItem(ref ty, ref default) => {
23472336
let name = item.name.as_ref().unwrap();
23482337
try!(write!(w, "<h4 id='assoc_const.{}' class='{}'><code>",
2349-
*name, shortty(item)));
2350-
try!(assoc_const(w, item, ty, default.as_ref()));
2351-
try!(write!(w, "</code></h4>\n"));
2352-
}
2353-
clean::ConstantItem(ref c) => {
2354-
let name = item.name.as_ref().unwrap();
2355-
try!(write!(w, "<h4 id='assoc_const.{}' class='{}'><code>",
2356-
*name, shortty(item)));
2357-
try!(assoc_const(w, item, &c.type_, Some(&c.expr)));
2338+
*name,
2339+
shortty(item)));
2340+
try!(assoc_const(w, item, ty, default));
23582341
try!(write!(w, "</code></h4>\n"));
23592342
}
23602343
clean::AssociatedTypeItem(ref bounds, ref default) => {

branches/stable/src/libstd/error.rs

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,22 @@
4747
// coherence challenge (e.g., specialization, neg impls, etc) we can
4848
// reconsider what crate these items belong in.
4949

50-
use boxed::Box;
50+
use any::TypeId;
51+
use boxed::{self, 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 result::Result::{self, Ok, Err};
59+
use raw::TraitObject;
5760
use str;
5861
use string::{self, String};
5962

6063
/// Base functionality for all errors in Rust.
6164
#[stable(feature = "rust1", since = "1.0.0")]
62-
pub trait Error: Debug + Display {
65+
pub trait Error: Debug + Display + Reflect {
6366
/// A short description of the error.
6467
///
6568
/// The description should not contain newlines or sentence-ending
@@ -71,6 +74,14 @@ pub trait Error: Debug + Display {
7174
/// The lower-level cause of this error, if any.
7275
#[stable(feature = "rust1", since = "1.0.0")]
7376
fn cause(&self) -> Option<&Error> { None }
77+
78+
/// Get the `TypeId` of `self`
79+
#[doc(hidden)]
80+
#[unstable(feature = "core",
81+
reason = "unclear whether to commit to this public implementation detail")]
82+
fn type_id(&self) -> TypeId where Self: 'static {
83+
TypeId::of::<Self>()
84+
}
7485
}
7586

7687
#[stable(feature = "rust1", since = "1.0.0")]
@@ -154,3 +165,112 @@ impl Error for string::FromUtf16Error {
154165
}
155166
}
156167

168+
// copied from any.rs
169+
impl Error + 'static {
170+
/// Returns true if the boxed type is the same as `T`
171+
#[unstable(feature = "error_downcast", reason = "recently added")]
172+
#[inline]
173+
pub fn is<T: Error + 'static>(&self) -> bool {
174+
// Get TypeId of the type this function is instantiated with
175+
let t = TypeId::of::<T>();
176+
177+
// Get TypeId of the type in the trait object
178+
let boxed = self.type_id();
179+
180+
// Compare both TypeIds on equality
181+
t == boxed
182+
}
183+
184+
/// Returns some reference to the boxed value if it is of type `T`, or
185+
/// `None` if it isn't.
186+
#[unstable(feature = "error_downcast", reason = "recently added")]
187+
#[inline]
188+
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
189+
if self.is::<T>() {
190+
unsafe {
191+
// Get the raw representation of the trait object
192+
let to: TraitObject = transmute(self);
193+
194+
// Extract the data pointer
195+
Some(transmute(to.data))
196+
}
197+
} else {
198+
None
199+
}
200+
}
201+
202+
/// Returns some mutable reference to the boxed value if it is of type `T`, or
203+
/// `None` if it isn't.
204+
#[unstable(feature = "error_downcast", reason = "recently added")]
205+
#[inline]
206+
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
207+
if self.is::<T>() {
208+
unsafe {
209+
// Get the raw representation of the trait object
210+
let to: TraitObject = transmute(self);
211+
212+
// Extract the data pointer
213+
Some(transmute(to.data))
214+
}
215+
} else {
216+
None
217+
}
218+
}
219+
}
220+
221+
impl Error + 'static + Send {
222+
/// Forwards to the method defined on the type `Any`.
223+
#[unstable(feature = "error_downcast", reason = "recently added")]
224+
#[inline]
225+
pub fn is<T: Error + 'static>(&self) -> bool {
226+
<Error + 'static>::is::<T>(self)
227+
}
228+
229+
/// Forwards to the method defined on the type `Any`.
230+
#[unstable(feature = "error_downcast", reason = "recently added")]
231+
#[inline]
232+
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
233+
<Error + 'static>::downcast_ref::<T>(self)
234+
}
235+
236+
/// Forwards to the method defined on the type `Any`.
237+
#[unstable(feature = "error_downcast", reason = "recently added")]
238+
#[inline]
239+
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
240+
<Error + 'static>::downcast_mut::<T>(self)
241+
}
242+
}
243+
244+
impl Error {
245+
#[inline]
246+
#[unstable(feature = "error_downcast", reason = "recently added")]
247+
/// Attempt to downcast the box to a concrete type.
248+
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Error>> {
249+
if self.is::<T>() {
250+
unsafe {
251+
// Get the raw representation of the trait object
252+
let raw = boxed::into_raw(self);
253+
let to: TraitObject =
254+
transmute::<*mut Error, TraitObject>(raw);
255+
256+
// Extract the data pointer
257+
Ok(Box::from_raw(to.data as *mut T))
258+
}
259+
} else {
260+
Err(self)
261+
}
262+
}
263+
}
264+
265+
impl Error + Send {
266+
#[inline]
267+
#[unstable(feature = "error_downcast", reason = "recently added")]
268+
/// Attempt to downcast the box to a concrete type.
269+
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Error + Send>> {
270+
let err: Box<Error> = self;
271+
<Error>::downcast(err).map_err(|s| unsafe {
272+
// reapply the Send marker
273+
transmute::<Box<Error>, Box<Error + Send>>(s)
274+
})
275+
}
276+
}

branches/stable/src/libstd/io/buffered.rs

Lines changed: 2 additions & 1 deletion
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;
@@ -322,7 +323,7 @@ impl<W> From<IntoInnerError<W>> for Error {
322323
}
323324

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

branches/stable/src/libstd/sync/mpsc/mod.rs

Lines changed: 3 additions & 3 deletions
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;
@@ -955,8 +956,7 @@ impl<T> fmt::Display for SendError<T> {
955956
}
956957

957958
#[stable(feature = "rust1", since = "1.0.0")]
958-
impl<T: Send> error::Error for SendError<T> {
959-
959+
impl<T: Send + Reflect> error::Error for SendError<T> {
960960
fn description(&self) -> &str {
961961
"sending on a closed channel"
962962
}
@@ -991,7 +991,7 @@ impl<T> fmt::Display for TrySendError<T> {
991991
}
992992

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

996996
fn description(&self) -> &str {
997997
match *self {

branches/stable/src/libstd/sys/common/poison.rs

Lines changed: 4 additions & 3 deletions
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(),

branches/stable/src/test/compile-fail/issue-24446.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)