diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-03-31 16:18:55 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-03-31 16:18:55 -0700 |
| commit | 50b3ecf3bcc2e39a7a42e7f4b49f19398d5cc681 (patch) | |
| tree | 0546f83f5625d11fc4d5d9b2ad215a4ac3ce4778 /src/liballoc | |
| parent | 85e997adfffa5e8511ff31710649038216028832 (diff) | |
| parent | ac77392f8ab1c201b0c927f6a2d30b632b95acda (diff) | |
| download | rust-50b3ecf3bcc2e39a7a42e7f4b49f19398d5cc681.tar.gz rust-50b3ecf3bcc2e39a7a42e7f4b49f19398d5cc681.zip | |
rollup merge of #23919: alexcrichton/stabilize-io-error
Conflicts: src/libstd/fs/tempdir.rs src/libstd/io/error.rs
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/boxed.rs | 46 | ||||
| -rw-r--r-- | src/liballoc/lib.rs | 1 |
2 files changed, 45 insertions, 2 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index f3c0dd0c3dc..550b25ac3a7 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -56,8 +56,10 @@ 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. @@ -313,3 +315,43 @@ impl<'a, E: Error + 'a> From<E> for 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/lib.rs b/src/liballoc/lib.rs index b92dfa9117e..a880ba5cfe4 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -78,6 +78,7 @@ #![feature(unsafe_no_drop_flag, filling_drop)] #![feature(core)] #![feature(unique)] +#![feature(convert)] #![cfg_attr(test, feature(test, alloc, rustc_private))] #![cfg_attr(all(not(feature = "external_funcs"), not(feature = "external_crate")), feature(libc))] |
