flex_alloc/storage/
inline.rs

1use const_default::ConstDefault;
2
3use super::{array::ArrayStorage, RawBuffer};
4use crate::error::StorageError;
5
6/// A marker type used to indicate the inline allocation strategy, which
7/// stores all items within the collection handle.
8#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
9pub struct Inline<const N: usize>;
10
11/// An inline storage buffer.
12#[derive(Debug)]
13pub struct InlineBuffer<T, const N: usize> {
14    pub(crate) storage: ArrayStorage<T, N>,
15    pub(crate) length: usize,
16}
17
18impl<T, const N: usize> InlineBuffer<T, N> {
19    pub(crate) fn try_for_capacity(capacity: usize, exact: bool) -> Result<Self, StorageError> {
20        if (!exact && capacity < N) || capacity == N {
21            Ok(Self::DEFAULT)
22        } else {
23            Err(StorageError::CapacityLimit)
24        }
25    }
26}
27
28impl<T, const N: usize> ConstDefault for InlineBuffer<T, N> {
29    const DEFAULT: Self = Self {
30        storage: ArrayStorage::DEFAULT,
31        length: 0,
32    };
33}
34
35impl<T, const N: usize> Default for InlineBuffer<T, N> {
36    #[inline]
37    fn default() -> Self {
38        Self::DEFAULT
39    }
40}
41
42impl<'a, T: 'a, const N: usize> RawBuffer for InlineBuffer<T, N> {
43    type RawData = T;
44
45    #[inline]
46    fn data_ptr(&self) -> *const T {
47        self.storage.0.as_ptr().cast()
48    }
49
50    #[inline]
51    fn data_ptr_mut(&mut self) -> *mut T {
52        self.storage.0.as_mut_ptr().cast()
53    }
54}