mayheap/
error.rs

1/// The `Error` type for the `mayheap` crate.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum Error {
4    /// Attempted to grow a collection beyond its capacity.
5    ///
6    /// This error can only occur when `heapless` feature is enabled.
7    BufferOverflow,
8    /// Invalid UTF-8 sequence.
9    Utf8Error(core::str::Utf8Error),
10}
11
12/// The Result type for the zlink crate.
13pub type Result<T> = core::result::Result<T, Error>;
14
15impl core::error::Error for Error {
16    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
17        match self {
18            Error::BufferOverflow => None,
19            Error::Utf8Error(err) => Some(err),
20        }
21    }
22}
23
24impl core::fmt::Display for Error {
25    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26        match self {
27            Error::BufferOverflow => {
28                write!(f, "Attempted to grow a collection beyond its capacity")
29            }
30            Error::Utf8Error(err) => {
31                write!(f, "Invalid UTF-8 sequence: {}", err)
32            }
33        }
34    }
35}
36
37impl From<core::str::Utf8Error> for Error {
38    fn from(err: core::str::Utf8Error) -> Self {
39        Error::Utf8Error(err)
40    }
41}