about summary refs log tree commit diff
path: root/src/liballoc/boxed.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-03-31 16:18:55 -0700
committerAlex Crichton <alex@alexcrichton.com>2015-03-31 16:18:55 -0700
commit50b3ecf3bcc2e39a7a42e7f4b49f19398d5cc681 (patch)
tree0546f83f5625d11fc4d5d9b2ad215a4ac3ce4778 /src/liballoc/boxed.rs
parent85e997adfffa5e8511ff31710649038216028832 (diff)
parentac77392f8ab1c201b0c927f6a2d30b632b95acda (diff)
downloadrust-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/boxed.rs')
-rw-r--r--src/liballoc/boxed.rs46
1 files changed, 44 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(),
+            })))
+        }
+    }
+}