| 1 | use std::fmt; | 
| 2 | #[ cfg(feature= "std")] | 
|---|
| 3 | use std::any::Any; | 
|---|
| 4 | #[ cfg(feature= "std")] | 
|---|
| 5 | use std::error::Error; | 
|---|
| 6 |  | 
|---|
| 7 | /// Error value indicating insufficient capacity | 
|---|
| 8 | #[ derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)] | 
|---|
| 9 | pub struct CapacityError<T = ()> { | 
|---|
| 10 | element: T, | 
|---|
| 11 | } | 
|---|
| 12 |  | 
|---|
| 13 | impl<T> CapacityError<T> { | 
|---|
| 14 | /// Create a new `CapacityError` from `element`. | 
|---|
| 15 | pub const fn new(element: T) -> CapacityError<T> { | 
|---|
| 16 | CapacityError { | 
|---|
| 17 | element: element, | 
|---|
| 18 | } | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | /// Extract the overflowing element | 
|---|
| 22 | pub fn element(self) -> T { | 
|---|
| 23 | self.element | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | /// Convert into a `CapacityError` that does not carry an element. | 
|---|
| 27 | pub fn simplify(self) -> CapacityError { | 
|---|
| 28 | CapacityError { element: () } | 
|---|
| 29 | } | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | const CAPERROR: &'static str = "insufficient capacity"; | 
|---|
| 33 |  | 
|---|
| 34 | #[ cfg(feature= "std")] | 
|---|
| 35 | /// Requires `features="std"`. | 
|---|
| 36 | impl<T: Any> Error for CapacityError<T> {} | 
|---|
| 37 |  | 
|---|
| 38 | impl<T> fmt::Display for CapacityError<T> { | 
|---|
| 39 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 
|---|
| 40 | write!(f, "{} ", CAPERROR) | 
|---|
| 41 | } | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | impl<T> fmt::Debug for CapacityError<T> { | 
|---|
| 45 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 
|---|
| 46 | write!(f, "{} : {} ", "CapacityError", CAPERROR) | 
|---|
| 47 | } | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|