Skip to content

Commit 12d4d12

Browse files
committed
implement Error trait for error structs added in allocator API.
1 parent 57ab9e7 commit 12d4d12

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/liballoc/allocator.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
issue = "27700")]
1717

1818
use core::cmp;
19+
use core::fmt;
1920
use core::mem;
2021
use core::usize;
2122
use core::ptr::{self, Unique};
@@ -335,6 +336,19 @@ impl AllocErr {
335336
pub fn is_request_unsupported(&self) -> bool {
336337
if let AllocErr::Unsupported { .. } = *self { true } else { false }
337338
}
339+
pub fn description(&self) -> &str {
340+
match *self {
341+
AllocErr::Exhausted { .. } => "allocator memory exhausted",
342+
AllocErr::Unsupported { .. } => "unsupported allocator request",
343+
}
344+
}
345+
}
346+
347+
// (we need this for downstream impl of trait Error)
348+
impl fmt::Display for AllocErr {
349+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
350+
write!(f, "{}", self.description())
351+
}
338352
}
339353

340354
/// The `CannotReallocInPlace` error is used when `grow_in_place` or
@@ -343,6 +357,20 @@ impl AllocErr {
343357
#[derive(Clone, PartialEq, Eq, Debug)]
344358
pub struct CannotReallocInPlace;
345359

360+
impl CannotReallocInPlace {
361+
pub fn description(&self) -> &str {
362+
"cannot reallocate allocator's memory in place"
363+
}
364+
}
365+
366+
// (we need this for downstream impl of trait Error)
367+
impl fmt::Display for CannotReallocInPlace {
368+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
369+
write!(f, "{}", self.description())
370+
}
371+
}
372+
373+
/// An implementation of `Allocator` can allocate, reallocate, and
346374
/// An implementation of `Alloc` can allocate, reallocate, and
347375
/// deallocate arbitrary blocks of data described via `Layout`.
348376
///

src/libstd/error.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
// coherence challenge (e.g., specialization, neg impls, etc) we can
5252
// reconsider what crate these items belong in.
5353

54+
use alloc::allocator;
5455
use any::TypeId;
5556
use cell;
5657
use char;
@@ -221,6 +222,24 @@ impl Error for ! {
221222
fn description(&self) -> &str { *self }
222223
}
223224

225+
#[unstable(feature = "allocator_api",
226+
reason = "the precise API and guarantees it provides may be tweaked.",
227+
issue = "27700")]
228+
impl Error for allocator::AllocErr {
229+
fn description(&self) -> &str {
230+
allocator::AllocErr::description(self)
231+
}
232+
}
233+
234+
#[unstable(feature = "allocator_api",
235+
reason = "the precise API and guarantees it provides may be tweaked.",
236+
issue = "27700")]
237+
impl Error for allocator::CannotReallocInPlace {
238+
fn description(&self) -> &str {
239+
allocator::CannotReallocInPlace::description(self)
240+
}
241+
}
242+
224243
#[stable(feature = "rust1", since = "1.0.0")]
225244
impl Error for str::ParseBoolError {
226245
fn description(&self) -> &str { "failed to parse bool" }

src/libstd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
// std is implemented with unstable features, many of which are internal
246246
// compiler details that will never be stable
247247
#![feature(alloc)]
248+
#![feature(allocator_api)]
248249
#![feature(allow_internal_unstable)]
249250
#![feature(asm)]
250251
#![feature(associated_consts)]

0 commit comments

Comments
 (0)