1use std::fmt;
2#[cfg(feature="std")]
3use std::any::Any;
4#[cfg(feature="std")]
5use std::error::Error;
6
7/// Error value indicating insufficient capacity
8#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
9pub struct CapacityError<T = ()> {
10 element: T,
11}
12
13impl<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
32const CAPERROR: &'static str = "insufficient capacity";
33
34#[cfg(feature="std")]
35/// Requires `features="std"`.
36impl<T: Any> Error for CapacityError<T> {}
37
38impl<T> fmt::Display for CapacityError<T> {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 write!(f, "{}", CAPERROR)
41 }
42}
43
44impl<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