diff options
| author | bors <bors@rust-lang.org> | 2015-04-01 10:02:37 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-04-01 10:02:37 +0000 |
| commit | 89436536246250ee3cbc47a61c31037ce7558c06 (patch) | |
| tree | 451f704eef5a5c33ef3aa36a04c498f855cb4fb8 /src/liballoc | |
| parent | d754722a04b99fdcae0fd97fa2a4395521145ef2 (diff) | |
| parent | 2b71aed003997cc09b85c01306083187cb924a29 (diff) | |
| download | rust-89436536246250ee3cbc47a61c31037ce7558c06.tar.gz rust-89436536246250ee3cbc47a61c31037ce7558c06.zip | |
Auto merge of #23936 - pnkfelix:rollup, r=pnkfelix
This is an attempt to fix #23922
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/arc.rs | 2 | ||||
| -rw-r--r-- | src/liballoc/boxed.rs | 78 | ||||
| -rw-r--r-- | src/liballoc/boxed_test.rs | 1 |
3 files changed, 54 insertions, 27 deletions
diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index 9b37ddc7ab5..855c86f08e7 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -110,7 +110,7 @@ use heap::deallocate; /// let child_numbers = shared_numbers.clone(); /// /// thread::spawn(move || { -/// let local_numbers = child_numbers.as_slice(); +/// let local_numbers = &child_numbers[..]; /// /// // Work with the local numbers /// }); diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index f9bd0ab2f1e..550b25ac3a7 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -51,13 +51,15 @@ use core::prelude::*; use core::any::Any; use core::cmp::Ordering; use core::default::Default; -use core::error::{Error, FromError}; +use core::error::Error; use core::fmt; use core::hash::{self, Hash}; use core::mem; use core::ops::{Deref, DerefMut}; -use core::ptr::Unique; -use core::raw::TraitObject; +use core::ptr::{self, Unique}; +use core::raw::{TraitObject, Slice}; + +use heap; /// A value that represents the heap. This is the default place that the `box` /// keyword allocates into when no place is supplied. @@ -233,24 +235,10 @@ impl<T: ?Sized + Hash> Hash for Box<T> { } } -/// Extension methods for an owning `Any` trait object. -#[unstable(feature = "alloc", - reason = "this trait will likely disappear once compiler bugs blocking \ - a direct impl on `Box<Any>` have been fixed ")] -// FIXME(#18737): this should be a direct impl on `Box<Any>`. If you're -// removing this please make sure that you can downcase on -// `Box<Any + Send>` as well as `Box<Any>` -pub trait BoxAny { - /// Returns the boxed value if it is of type `T`, or - /// `Err(Self)` if it isn't. - #[stable(feature = "rust1", since = "1.0.0")] - fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>>; -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl BoxAny for Box<Any> { +impl Box<Any> { #[inline] - fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> { + #[stable(feature = "rust1", since = "1.0.0")] + pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> { if self.is::<T>() { unsafe { // Get the raw representation of the trait object @@ -267,10 +255,10 @@ impl BoxAny for Box<Any> { } } -#[stable(feature = "rust1", since = "1.0.0")] -impl BoxAny for Box<Any+Send> { +impl Box<Any+Send> { #[inline] - fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> { + #[stable(feature = "rust1", since = "1.0.0")] + pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> { <Box<Any>>::downcast(self) } } @@ -322,8 +310,48 @@ impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> { impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {} #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, E: Error + 'a> FromError<E> for Box<Error + 'a> { - fn from_error(err: E) -> Box<Error + 'a> { +impl<'a, E: Error + 'a> From<E> for Box<Error + 'a> { + fn from(err: E) -> Box<Error + 'a> { Box::new(err) } } + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a, E: Error + Send + 'a> From<E> for Box<Error + Send + 'a> { + fn from(err: E) -> Box<Error + Send + 'a> { + Box::new(err) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a, 'b> From<&'b str> for Box<Error + Send + 'a> { + fn from(err: &'b str) -> Box<Error + Send + 'a> { + #[derive(Debug)] + struct StringError(Box<str>); + impl Error for StringError { + fn description(&self) -> &str { &self.0 } + } + impl fmt::Display for StringError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.0.fmt(f) + } + } + + // Unfortunately `String` is located in libcollections, so we construct + // a `Box<str>` manually here. + unsafe { + let alloc = if err.len() == 0 { + 0 as *mut u8 + } else { + let ptr = heap::allocate(err.len(), 1); + if ptr.is_null() { ::oom(); } + ptr as *mut u8 + }; + ptr::copy(err.as_bytes().as_ptr(), alloc, err.len()); + Box::new(StringError(mem::transmute(Slice { + data: alloc, + len: err.len(), + }))) + } + } +} diff --git a/src/liballoc/boxed_test.rs b/src/liballoc/boxed_test.rs index bb1ff9428a7..682d5f407c4 100644 --- a/src/liballoc/boxed_test.rs +++ b/src/liballoc/boxed_test.rs @@ -17,7 +17,6 @@ use core::clone::Clone; use std::boxed; use std::boxed::Box; -use std::boxed::BoxAny; #[test] fn test_owned_clone() { |
