about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/boxed.rs46
-rw-r--r--src/liballoc/lib.rs1
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))]