about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorFelix S. Klock II <pnkfelix@pnkfx.org>2017-06-13 21:57:49 +0200
committerFelix S. Klock II <pnkfelix@pnkfx.org>2017-06-15 23:48:31 +0200
commit12d4d12fef4f9024ffcc8eb5fef11f1a21074cd7 (patch)
treecbea264acfb3a0d9eba389f1476ec19eebad15bb /src/liballoc
parent57ab9e7e7c481427a8d59e16b932fbd1e5fe4c02 (diff)
downloadrust-12d4d12fef4f9024ffcc8eb5fef11f1a21074cd7.tar.gz
rust-12d4d12fef4f9024ffcc8eb5fef11f1a21074cd7.zip
implement Error trait for error structs added in allocator API.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/allocator.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/liballoc/allocator.rs b/src/liballoc/allocator.rs
index c308d99a72c..752acbd0b45 100644
--- a/src/liballoc/allocator.rs
+++ b/src/liballoc/allocator.rs
@@ -16,6 +16,7 @@
             issue = "27700")]
 
 use core::cmp;
+use core::fmt;
 use core::mem;
 use core::usize;
 use core::ptr::{self, Unique};
@@ -335,6 +336,19 @@ impl AllocErr {
     pub fn is_request_unsupported(&self) -> bool {
         if let AllocErr::Unsupported { .. } = *self { true } else { false }
     }
+    pub fn description(&self) -> &str {
+        match *self {
+            AllocErr::Exhausted { .. } => "allocator memory exhausted",
+            AllocErr::Unsupported { .. } => "unsupported allocator request",
+        }
+    }
+}
+
+// (we need this for downstream impl of trait Error)
+impl fmt::Display for AllocErr {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "{}", self.description())
+    }
 }
 
 /// The `CannotReallocInPlace` error is used when `grow_in_place` or
@@ -343,6 +357,20 @@ impl AllocErr {
 #[derive(Clone, PartialEq, Eq, Debug)]
 pub struct CannotReallocInPlace;
 
+impl CannotReallocInPlace {
+    pub fn description(&self) -> &str {
+        "cannot reallocate allocator's memory in place"
+    }
+}
+
+// (we need this for downstream impl of trait Error)
+impl fmt::Display for CannotReallocInPlace {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "{}", self.description())
+    }
+}
+
+/// An implementation of `Allocator` can allocate, reallocate, and
 /// An implementation of `Alloc` can allocate, reallocate, and
 /// deallocate arbitrary blocks of data described via `Layout`.
 ///