diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 74f0599e486e1da696f2619ceaa6a0248db948e1..415416199459f56b1b1bf12795a8a50b27582d52 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -10,6 +10,8 @@ //! A unique pointer type. +#![stable] + use core::any::{Any, AnyRefExt}; use core::clone::Clone; use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering}; @@ -44,7 +46,7 @@ /// A type that represents a uniquely-owned value. #[lang = "owned_box"] -#[unstable = "custom allocators will add an additional type parameter (with default)"] +#[stable] pub struct Box(Unique); #[stable] @@ -111,18 +113,37 @@ fn hash(&self, state: &mut S) { } } +#[cfg(not(stage0))] +impl Box { + pub fn downcast(self) -> Result, Box> { + if self.is::() { + unsafe { + // Get the raw representation of the trait object + let to: TraitObject = + mem::transmute::, TraitObject>(self); + + // Extract the data pointer + Ok(mem::transmute(to.data)) + } + } else { + Err(self) + } + } +} /// Extension methods for an owning `Any` trait object. #[unstable = "post-DST and coherence changes, this will not be a trait but \ rather a direct `impl` on `Box`"] +#[cfg(stage0)] pub trait BoxAny { /// Returns the boxed value if it is of type `T`, or /// `Err(Self)` if it isn't. - #[unstable = "naming conventions around accessing innards may change"] + #[stable] fn downcast(self) -> Result, Self>; } #[stable] +#[cfg(stage0)] impl BoxAny for Box { #[inline] fn downcast(self) -> Result, Box> { @@ -147,7 +168,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { } } -impl fmt::Show for Box { +impl fmt::Show for Box { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.pad("Box") } diff --git a/src/librustc_typeck/coherence/orphan.rs b/src/librustc_typeck/coherence/orphan.rs index 79443200ddf0e10eef6f2ec00174b4155c904c19..49f150cf027d5d17f015892a53b7bdab2b3554c3 100644 --- a/src/librustc_typeck/coherence/orphan.rs +++ b/src/librustc_typeck/coherence/orphan.rs @@ -57,6 +57,11 @@ fn visit_item(&mut self, item: &'v ast::Item) { ty::ty_trait(ref data) => { self.check_def_id(item.span, data.principal_def_id()); } + ty::ty_uniq(..) => { + self.check_def_id(item.span, + self.tcx.lang_items.owned_box() + .unwrap()); + } _ => { span_err!(self.tcx.sess, item.span, E0118, "no base type found for inherent implementation; \