Skip to content

Commit

Permalink
implement Error trait for error structs added in allocator API.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnkfelix committed Jun 15, 2017
1 parent 57ab9e7 commit 12d4d12
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/liballoc/allocator.rs
Expand Up @@ -16,6 +16,7 @@
issue = "27700")]

use core::cmp;
use core::fmt;
use core::mem;
use core::usize;
use core::ptr::{self, Unique};
Expand Down Expand Up @@ -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
Expand All @@ -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`.
///
Expand Down
19 changes: 19 additions & 0 deletions src/libstd/error.rs
Expand Up @@ -51,6 +51,7 @@
// coherence challenge (e.g., specialization, neg impls, etc) we can
// reconsider what crate these items belong in.

use alloc::allocator;
use any::TypeId;
use cell;
use char;
Expand Down Expand Up @@ -221,6 +222,24 @@ impl Error for ! {
fn description(&self) -> &str { *self }
}

#[unstable(feature = "allocator_api",
reason = "the precise API and guarantees it provides may be tweaked.",
issue = "27700")]
impl Error for allocator::AllocErr {
fn description(&self) -> &str {
allocator::AllocErr::description(self)
}
}

#[unstable(feature = "allocator_api",
reason = "the precise API and guarantees it provides may be tweaked.",
issue = "27700")]
impl Error for allocator::CannotReallocInPlace {
fn description(&self) -> &str {
allocator::CannotReallocInPlace::description(self)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Error for str::ParseBoolError {
fn description(&self) -> &str { "failed to parse bool" }
Expand Down
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Expand Up @@ -245,6 +245,7 @@
// std is implemented with unstable features, many of which are internal
// compiler details that will never be stable
#![feature(alloc)]
#![feature(allocator_api)]
#![feature(allow_internal_unstable)]
#![feature(asm)]
#![feature(associated_consts)]
Expand Down

0 comments on commit 12d4d12

Please sign in to comment.