rune_alloc/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! Error types used by rune alloc.

use core::alloc::LayoutError;
use core::convert::Infallible;
use core::fmt;

use crate::alloc::AllocError;

/// An error type returned when a custom error is available alongside an allocation error.
#[derive(Debug)]
pub enum CustomError<E> {
    /// Custom error being returned.
    Custom(E),
    /// Try reserve error being returned.
    Error(Error),
}

impl<E> From<Error> for CustomError<E> {
    fn from(error: Error) -> Self {
        CustomError::Error(error)
    }
}

impl<E> From<AllocError> for CustomError<E> {
    fn from(error: AllocError) -> Self {
        CustomError::Error(Error::from(error))
    }
}

/// The error type for methods which allocate or reserve.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
    /// Error due to the computed capacity exceeding the collection's maximum
    /// (usually `isize::MAX` bytes).
    #[doc(hidden)]
    CapacityOverflow,

    /// Error when computing layout.
    #[doc(hidden)]
    LayoutError,

    /// Error during internal formatting.
    #[doc(hidden)]
    FormatError,

    /// The memory allocator returned an error
    #[doc(hidden)]
    AllocError {
        /// The layout of the allocation request that failed.
        error: AllocError,
    },
}

impl From<AllocError> for Error {
    #[inline]
    fn from(error: AllocError) -> Self {
        Error::AllocError { error }
    }
}

impl From<Infallible> for Error {
    #[inline(always)]
    fn from(value: Infallible) -> Self {
        match value {}
    }
}

impl From<LayoutError> for Error {
    #[inline]
    fn from(_: LayoutError) -> Self {
        Error::LayoutError
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::CapacityOverflow => write!(f, "Capacity overflow"),
            Error::LayoutError => write!(f, "Layout error"),
            Error::FormatError => write!(f, "Format error"),
            Error::AllocError { error } => error.fmt(f),
        }
    }
}

impl core::error::Error for Error {}