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`. new(element: T) -> CapacityError<T>15 pub fn new(element: T) -> CapacityError<T> { 16 CapacityError { 17 element: element, 18 } 19 } 20 21 /// Extract the overflowing element element(self) -> T22 pub fn element(self) -> T { 23 self.element 24 } 25 26 /// Convert into a `CapacityError` that does not carry an element. simplify(self) -> CapacityError27 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> { description(&self) -> &str37 fn description(&self) -> &str { 38 CAPERROR 39 } 40 } 41 42 impl<T> fmt::Display for CapacityError<T> { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result43 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 44 write!(f, "{}", CAPERROR) 45 } 46 } 47 48 impl<T> fmt::Debug for CapacityError<T> { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result49 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 50 write!(f, "{}: {}", "CapacityError", CAPERROR) 51 } 52 } 53 54