diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-01-01 22:24:06 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-01-01 22:31:53 -0800 |
| commit | f2ccdfd8914b7b8f8f8f2053f9ecd98d54ef95a6 (patch) | |
| tree | c4b42e27633b47edf08b898f565eab5b38493541 /src/liballoc | |
| parent | cd614164e692cca3a1460737f581fcb6d4630baf (diff) | |
| download | rust-f2ccdfd8914b7b8f8f8f2053f9ecd98d54ef95a6.tar.gz rust-f2ccdfd8914b7b8f8f8f2053f9ecd98d54ef95a6.zip | |
std: Second pass stabilization for `boxed`
This commit performs a second pass over the `std::boxed` module, taking the following actions: * `boxed` is now stable * `Box` is now stable * `BoxAny` is removed in favor of a direct `impl Box<Any>` * `Box::downcast` remains unstable while the name of the `downcast` family of methods is determined. This is a breaking change due to the removal of the `BoxAny` trait (note that the `downcast` method still exists), and existing consumers of `BoxAny` simply need to remove the import in their modules. [breaking-change]
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/boxed.rs | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 74f0599e486..41541619945 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 @@ pub static HEAP: () = (); /// 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<T>(Unique<T>); #[stable] @@ -111,18 +113,37 @@ impl<S: hash::Writer, Sized? T: Hash<S>> Hash<S> for Box<T> { } } +#[cfg(not(stage0))] +impl Box<Any> { + pub fn downcast<T: 'static>(self) -> Result<Box<T>, Box<Any>> { + if self.is::<T>() { + unsafe { + // Get the raw representation of the trait object + let to: TraitObject = + mem::transmute::<Box<Any>, 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<Any>`"] +#[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<T: 'static>(self) -> Result<Box<T>, Self>; } #[stable] +#[cfg(stage0)] impl BoxAny for Box<Any> { #[inline] fn downcast<T: 'static>(self) -> Result<Box<T>, Box<Any>> { @@ -147,7 +168,7 @@ impl<Sized? T: fmt::Show> fmt::Show for Box<T> { } } -impl fmt::Show for Box<Any+'static> { +impl fmt::Show for Box<Any> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.pad("Box<Any>") } |
