1use core::any;
6use core::fmt;
7
8#[derive(Copy, Clone, Debug, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum ZeroVecError {
12 InvalidLength { ty: &'static str, len: usize },
15 ParseError { ty: &'static str },
17 VarZeroVecFormatError,
19}
20
21impl fmt::Display for ZeroVecError {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
23 match *self {
24 ZeroVecError::InvalidLength { ty, len } => {
25 write!(f, "Invalid length {len} for slice of type {ty}")
26 }
27 ZeroVecError::ParseError { ty } => {
28 write!(f, "Could not parse bytes to slice of type {ty}")
29 }
30 ZeroVecError::VarZeroVecFormatError => {
31 write!(f, "Invalid format for VarZeroVec buffer")
32 }
33 }
34 }
35}
36
37impl ZeroVecError {
38 pub fn parse<T: ?Sized + 'static>() -> ZeroVecError {
40 ZeroVecError::ParseError {
41 ty: any::type_name::<T>(),
42 }
43 }
44
45 pub fn length<T: ?Sized + 'static>(len: usize) -> ZeroVecError {
47 ZeroVecError::InvalidLength {
48 ty: any::type_name::<T>(),
49 len,
50 }
51 }
52}
53
54#[cfg(feature = "std")]
55impl ::std::error::Error for ZeroVecError {}