flex_alloc/storage/
bytes.rs

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
use core::alloc::Layout;
use core::fmt;
use core::mem::{ManuallyDrop, MaybeUninit};
use core::ptr::NonNull;

use const_default::ConstDefault;

use super::{spill::SpillStorage, utils::layout_aligned_bytes};
use crate::alloc::{AllocError, AllocateIn, Allocator, Fixed, SpillAlloc};

/// A reusable storage buffer consisting of an array of bytes.
#[repr(C)]
pub union ByteStorage<T, const N: usize> {
    _align: [ManuallyDrop<T>; 0],
    data: [MaybeUninit<u8>; N],
}

impl<T, const N: usize> ByteStorage<T, N> {
    /// Access the buffer contents as a mutable slice.
    pub fn as_uninit_slice(&mut self) -> &mut [MaybeUninit<u8>] {
        // SAFETY: `self.data` is always safe to access.
        unsafe { &mut self.data }
    }
}

impl<'a, T, const N: usize> AllocateIn for &'a mut ByteStorage<T, N> {
    type Alloc = Fixed<'a>;

    #[inline]
    fn allocate_in(self, layout: Layout) -> Result<(NonNull<[u8]>, Self::Alloc), AllocError> {
        let ptr = layout_aligned_bytes(self.as_uninit_slice(), layout).map_err(|_| AllocError)?;
        let alloc = Fixed::default();
        Ok((ptr, alloc))
    }
}

impl<T, const N: usize> ConstDefault for ByteStorage<T, N> {
    const DEFAULT: Self = Self {
        // SAFETY: this is the accepted method to construct a `MaybeUninit` slice.
        data: unsafe { MaybeUninit::uninit().assume_init() },
    };
}

impl<T, const N: usize> fmt::Debug for ByteStorage<T, N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ByteStorage").finish_non_exhaustive()
    }
}

impl<T, const N: usize> Default for ByteStorage<T, N> {
    #[inline]
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl<'a, T: 'static, const N: usize> SpillAlloc<'a> for &'a mut ByteStorage<T, N> {
    type NewIn<A: 'a> = SpillStorage<'a, Self, A>;

    #[inline]
    fn spill_alloc_in<A: Allocator + 'a>(self, alloc: A) -> Self::NewIn<A> {
        SpillStorage::new_in(self, alloc)
    }
}

#[cfg(feature = "zeroize")]
impl<T, const N: usize> zeroize::Zeroize for ByteStorage<T, N> {
    #[inline]
    fn zeroize(&mut self) {
        self.as_uninit_slice().zeroize()
    }
}