Skip to main content

osom_lib_boxed/
errors.rs

1//! Holds the definition of [`CBoxError`].
2use core::fmt::Display;
3
4use osom_lib_reprc::macros::reprc;
5
6/// Represents possible errors when working with [`CBox`][super::cbox::CBox].
7#[reprc]
8#[repr(u8)]
9#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
10#[must_use]
11pub enum CBoxError {
12    /// The underlying allocator returned an error.
13    AllocationError = 0,
14}
15
16osom_lib_macros::unreachable_from_infallible!(CBoxError);
17
18impl Display for CBoxError {
19    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
20        match self {
21            CBoxError::AllocationError => write!(f, "CBoxError::AllocationError"),
22        }
23    }
24}
25
26/// Represents possible errors when working with [`CBox`][super::cbox::CBox].
27#[reprc]
28#[repr(u8)]
29#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
30#[must_use]
31pub enum CBoxTryCloneError {
32    /// The underlying allocator returned an error.
33    BoxError(CBoxError) = 0,
34
35    /// The allocator cloning failed.
36    AllocatorCloningError = 1,
37
38    /// The item cloning failed.
39    ItemCloningError = 2,
40}
41
42osom_lib_macros::unreachable_from_infallible!(CBoxTryCloneError);
43
44impl Display for CBoxTryCloneError {
45    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
46        match self {
47            CBoxTryCloneError::BoxError(e) => write!(f, "CBoxTryCloneError::BoxError({e})"),
48            CBoxTryCloneError::ItemCloningError => write!(f, "CBoxTryCloneError::ItemCloningError"),
49            CBoxTryCloneError::AllocatorCloningError => todo!(),
50        }
51    }
52}
53
54impl From<CBoxError> for CBoxTryCloneError {
55    fn from(e: CBoxError) -> Self {
56        Self::BoxError(e)
57    }
58}