flex_alloc/storage/
array.rs

1use core::fmt;
2use core::mem::MaybeUninit;
3
4use const_default::ConstDefault;
5
6use super::spill::SpillStorage;
7use crate::alloc::{Allocator, SpillAlloc};
8
9/// A storage buffer consisting of an uninitialized `MaybeUnit` array.
10#[repr(transparent)]
11pub struct ArrayStorage<T, const N: usize>(pub [MaybeUninit<T>; N]);
12
13impl<T, const N: usize> ArrayStorage<T, N> {
14    /// Access the buffer contents as a mutable slice.
15    pub fn as_uninit_slice(&mut self) -> &mut [MaybeUninit<T>] {
16        &mut self.0
17    }
18}
19
20impl<T, const N: usize> fmt::Debug for ArrayStorage<T, N> {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        f.debug_struct("ArrayStorage").finish_non_exhaustive()
23    }
24}
25
26impl<T, const N: usize> ConstDefault for ArrayStorage<T, N> {
27    const DEFAULT: Self = Self(unsafe { MaybeUninit::uninit().assume_init() });
28}
29
30impl<T, const N: usize> Default for ArrayStorage<T, N> {
31    #[inline]
32    fn default() -> Self {
33        Self::DEFAULT
34    }
35}
36
37impl<'a, T: 'a, const N: usize> SpillAlloc<'a> for &'a mut ArrayStorage<T, N> {
38    type NewIn<A: 'a> = SpillStorage<'a, &'a mut [MaybeUninit<T>], A>;
39
40    #[inline]
41    fn spill_alloc_in<A: Allocator + 'a>(self, alloc: A) -> Self::NewIn<A> {
42        SpillStorage::new_in(&mut self.0, alloc)
43    }
44}
45
46#[cfg(feature = "zeroize")]
47impl<T, const N: usize> zeroize::Zeroize for ArrayStorage<T, N> {
48    #[inline]
49    fn zeroize(&mut self) {
50        self.0.zeroize()
51    }
52}