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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
use crate::alloc::{AllocRef, BuildAllocRef, CapacityOverflow, LayoutErr, NonZeroLayout};
use core::fmt;

/// Augments `AllocErr` with a `CapacityOverflow` variant.
pub enum CollectionAllocErr<B: BuildAllocRef>
where
    B::Ref: AllocRef,
{
    /// Error due to the computed capacity exceeding the collection's maximum
    /// (usually `isize::MAX` bytes).
    CapacityOverflow,

    /// The memory allocator returned an error
    AllocError {
        /// The layout of allocation request that failed
        layout: NonZeroLayout,

        /// Error returned by the allocator
        inner: <B::Ref as AllocRef>::Error,
    },
}

impl<B: BuildAllocRef> fmt::Debug for CollectionAllocErr<B>
where
    B::Ref: AllocRef,
    <B::Ref as AllocRef>::Error: fmt::Debug,
{
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::CapacityOverflow => fmt.write_str("CapacityOverflow"),
            Self::AllocError { layout, inner } => fmt
                .debug_struct("AllocError")
                .field("layout", &layout)
                .field("inner", &inner)
                .finish(),
        }
    }
}

impl<B: BuildAllocRef> Clone for CollectionAllocErr<B>
where
    B::Ref: AllocRef,
    <B::Ref as AllocRef>::Error: Clone,
{
    fn clone(&self) -> Self {
        match self {
            Self::CapacityOverflow => Self::CapacityOverflow,
            Self::AllocError { layout, inner } => Self::AllocError {
                layout: *layout,
                inner: inner.clone(),
            },
        }
    }
}

impl<B: BuildAllocRef> PartialEq for CollectionAllocErr<B>
where
    B::Ref: AllocRef,
    <B::Ref as AllocRef>::Error: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::CapacityOverflow, Self::CapacityOverflow) => true,
            (
                Self::AllocError { layout, inner },
                Self::AllocError {
                    layout: other_layout,
                    inner: other_inner,
                },
            ) => layout == other_layout && inner == other_inner,
            _ => false,
        }
    }
}

impl<B: BuildAllocRef> Eq for CollectionAllocErr<B>
where
    B::Ref: AllocRef,
    <B::Ref as AllocRef>::Error: Eq,
{
}

impl<B: BuildAllocRef> From<CapacityOverflow> for CollectionAllocErr<B>
where
    B::Ref: AllocRef,
{
    #[inline]
    #[must_use]
    fn from(_: CapacityOverflow) -> Self {
        Self::CapacityOverflow
    }
}

impl<B: BuildAllocRef> From<LayoutErr> for CollectionAllocErr<B>
where
    B::Ref: AllocRef,
{
    #[inline]
    #[must_use]
    fn from(_: LayoutErr) -> Self {
        Self::CapacityOverflow
    }
}