ialloc/
error.rs

1//! [`ExcessiveAlignmentRequestedError`], [`ExcessiveSliceRequestedError`] (and any future error types)
2
3use crate::Alignment;
4
5#[cfg(doc)] use core::alloc::LayoutError;
6use core::fmt::{self, Debug, Display, Formatter};
7
8
9
10/// The allocator explicitly rejected a zero-sized allocation, because they behave inconsistently.
11///
12/// This may be because:
13/// -   A platform specific allocator fails on some but not all platforms.
14/// -   Allocating 0 bytes succeeds, but reallocating to 0 bytes fails.
15#[derive(Clone, Copy, Debug)] pub struct BannedZeroSizedAllocationsError;
16
17/// More alignment was requested than the allocator could support.
18#[derive(Clone, Copy, Debug)] pub struct ExcessiveAlignmentRequestedError {
19    pub requested: Alignment,
20    pub supported: Alignment,
21}
22
23/// A slice large enough to result in a [`LayoutError`] was requested.
24#[derive(Clone, Copy, Debug)] pub struct ExcessiveSliceRequestedError {
25    pub requested: usize,
26}
27
28impl Display for BannedZeroSizedAllocationsError  { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "requested a 0-sized allocation, but those have inconsistent behavior with this allocator") } }
29impl Display for ExcessiveAlignmentRequestedError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "requested {:?} alignment, but a maximum of {:?} is supported", self.requested, self.supported) } }
30impl Display for ExcessiveSliceRequestedError     { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "requested {} elements, but that would result in a LayoutError", self.requested) } }
31impl From<BannedZeroSizedAllocationsError > for () { fn from(_: BannedZeroSizedAllocationsError) -> Self {} }
32impl From<ExcessiveAlignmentRequestedError> for () { fn from(_: ExcessiveAlignmentRequestedError) -> Self {} }
33impl From<ExcessiveSliceRequestedError    > for () { fn from(_: ExcessiveSliceRequestedError) -> Self {} }
34#[cfg(feature = "std")] impl std::error::Error for BannedZeroSizedAllocationsError  { fn description(&self) -> &str { "requested a 0-sized allocation, but those have inconsistent behavior with this allocator" } }
35#[cfg(feature = "std")] impl std::error::Error for ExcessiveAlignmentRequestedError { fn description(&self) -> &str { "requested more alignment than was supported" } }
36#[cfg(feature = "std")] impl std::error::Error for ExcessiveSliceRequestedError     { fn description(&self) -> &str { "requested too many elements" } }
37#[cfg(allocator_api = "*")] impl From<BannedZeroSizedAllocationsError > for core::alloc::AllocError { fn from(_: BannedZeroSizedAllocationsError ) -> Self { core::alloc::AllocError } }
38#[cfg(allocator_api = "*")] impl From<ExcessiveAlignmentRequestedError> for core::alloc::AllocError { fn from(_: ExcessiveAlignmentRequestedError) -> Self { core::alloc::AllocError } }
39#[cfg(allocator_api = "*")] impl From<ExcessiveSliceRequestedError    > for core::alloc::AllocError { fn from(_: ExcessiveSliceRequestedError    ) -> Self { core::alloc::AllocError } }