flex_array/
alloc.rs

1//! Contains mainly allocator types and traits used by `FlexArr` The most important being
2//! the `AltAllocator` trait, and the `AllocError` type.
3//!
4//! If the `alloc_unstable` feature is enabled, the `AltAllocator`
5//! trait is implemented for types that implement the allocator api `Allocator` trait.
6//!
7//! If built with the `std_alloc` feature, a wrapper called `Global` is also
8//! provided. Further, if the `alloc_unstable` feature is enabled,
9//! the allocator APIs `Global` is re-exported instead.
10
11#[cfg(all(feature = "alloc_api2", not(feature = "alloc_unstable")))]
12mod alloc_api2;
13#[cfg(feature = "alloc_unstable")]
14mod alloc_unstable;
15mod alt_alloc;
16#[cfg(feature = "std_alloc")]
17mod std_alloc;
18
19#[cfg(feature = "alloc_unstable")]
20pub use core::alloc::AllocError;
21
22#[cfg(not(feature = "alloc_unstable"))]
23pub use alloc_error::AllocError;
24pub use alt_alloc::AltAllocator;
25#[cfg(feature = "std_alloc")]
26pub use std_alloc::Global;
27
28#[cfg(not(feature = "alloc_unstable"))]
29mod alloc_error {
30    use core::error::Error;
31    use core::fmt;
32
33    /// This indicates some sort of memory allocation error for the alt allocator.
34    ///
35    /// If the rust allocator API is enabled this will be the same error type as
36    /// the Allocator API.
37    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
38    pub struct AllocError;
39
40    impl Error for AllocError {}
41
42    impl fmt::Display for AllocError {
43        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44            f.write_str("A memory allocation error occurred.")
45        }
46    }
47}