1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 
11 pub mod alloc;
12 pub mod hash_map;
13 pub mod hash_set;
14 mod shim;
15 mod table;
16 
17 pub mod fake;
18 
19 use std::{error, fmt};
20 
21 trait Recover<Q: ?Sized> {
22     type Key;
23 
get(&self, key: &Q) -> Option<&Self::Key>24     fn get(&self, key: &Q) -> Option<&Self::Key>;
take(&mut self, key: &Q) -> Option<Self::Key>25     fn take(&mut self, key: &Q) -> Option<Self::Key>;
replace(&mut self, key: Self::Key) -> Option<Self::Key>26     fn replace(&mut self, key: Self::Key) -> Option<Self::Key>;
27 }
28 
29 #[derive(Debug)]
30 pub struct AllocationInfo {
31     /// The size we are requesting.
32     size: usize,
33     /// The alignment we are requesting.
34     alignment: usize,
35 }
36 
37 #[derive(Debug)]
38 pub struct FailedAllocationError {
39     reason: &'static str,
40     /// The allocation info we are requesting, if needed.
41     allocation_info: Option<AllocationInfo>,
42 }
43 
44 impl FailedAllocationError {
45     #[inline]
new(reason: &'static str) -> Self46     pub fn new(reason: &'static str) -> Self {
47         Self {
48             reason,
49             allocation_info: None,
50         }
51     }
52 }
53 
54 impl error::Error for FailedAllocationError {
description(&self) -> &str55     fn description(&self) -> &str {
56         self.reason
57     }
58 }
59 
60 impl fmt::Display for FailedAllocationError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result61     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62         match self.allocation_info {
63             Some(ref info) => write!(
64                 f,
65                 "{}, allocation: (size: {}, alignment: {})",
66                 self.reason, info.size, info.alignment
67             ),
68             None => self.reason.fmt(f),
69         }
70     }
71 }
72