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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use core::alloc::Layout;
use core::fmt::Debug;
use core::marker::PhantomData;
use core::mem::{size_of, MaybeUninit};
use core::slice;

use crate::error::StorageError;
use crate::index::Index;
use crate::storage::alloc::{AllocHandle, AllocHandleNew, AllocHeader, AllocLayout};
use crate::storage::utils::array_layout;
use crate::storage::{InlineBuffer, RawBuffer};

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct VecHeader<I: Index = usize> {
    pub capacity: I,
    pub length: I,
}

impl<I: Index> AllocHeader for VecHeader<I> {
    const EMPTY: Self = VecHeader {
        capacity: I::ZERO,
        length: I::ZERO,
    };

    #[inline]
    fn is_empty(&self) -> bool {
        self.capacity == I::ZERO
    }
}

#[derive(Debug)]
pub struct VecData<T, I: Index = usize>(PhantomData<(T, I)>);

impl<T, I: Index> AllocLayout for VecData<T, I> {
    type Header = VecHeader<I>;
    type Data = T;

    #[inline]
    fn layout(header: &Self::Header) -> Result<Layout, StorageError> {
        array_layout::<T>(header.capacity.to_usize())
    }

    #[inline]
    fn update_header(header: &mut Self::Header, layout: Layout) {
        let t_size = size_of::<T>();
        header.capacity = I::from_usize(if t_size > 0 {
            (layout.size() / t_size).min(I::MAX_USIZE)
        } else {
            I::MAX_USIZE
        });
    }
}

pub trait VecBuffer: RawBuffer<RawData = Self::Data> {
    type Data;
    type Index: Index;

    fn capacity(&self) -> Self::Index;

    fn length(&self) -> Self::Index;

    /// # Safety
    /// The capacity of the buffer must be established as greater than zero,
    /// otherwise this method may attempt to write into unallocated memory.
    unsafe fn set_length(&mut self, len: Self::Index);

    #[inline]
    fn as_uninit_slice(&mut self) -> &mut [MaybeUninit<Self::Data>] {
        unsafe { slice::from_raw_parts_mut(self.data_ptr_mut().cast(), self.capacity().to_usize()) }
    }

    #[inline]
    fn as_slice(&self) -> &[Self::Data] {
        unsafe { slice::from_raw_parts(self.data_ptr(), self.length().to_usize()) }
    }

    #[inline]
    fn as_mut_slice(&mut self) -> &mut [Self::Data] {
        unsafe { slice::from_raw_parts_mut(self.data_ptr_mut(), self.length().to_usize()) }
    }

    #[inline]
    unsafe fn uninit_index(&mut self, index: usize) -> &mut MaybeUninit<Self::Data> {
        &mut *self.data_ptr_mut().add(index).cast()
    }

    fn vec_try_resize(&mut self, capacity: Self::Index, exact: bool) -> Result<(), StorageError>;
}

impl<B, T, I: Index> VecBuffer for B
where
    B: AllocHandle<Meta = VecData<T, I>>,
{
    type Data = T;
    type Index = I;

    #[inline]
    fn capacity(&self) -> I {
        if self.is_empty_handle() {
            I::ZERO
        } else {
            unsafe { self.header() }.capacity
        }
    }

    #[inline]
    fn length(&self) -> I {
        if self.is_empty_handle() {
            I::ZERO
        } else {
            unsafe { self.header() }.length
        }
    }

    #[inline]
    unsafe fn set_length(&mut self, len: I) {
        self.header_mut().length = len;
    }

    #[inline]
    fn vec_try_resize(&mut self, capacity: Self::Index, exact: bool) -> Result<(), StorageError> {
        let length = self.length();
        self.resize_handle(VecHeader { capacity, length }, exact)?;
        Ok(())
    }
}

pub trait VecBufferNew: VecBufferSpawn {
    const NEW: Self;

    fn vec_try_new(capacity: Self::Index, exact: bool) -> Result<Self, StorageError>;
}

impl<B> VecBufferNew for B
where
    B: VecBufferSpawn + AllocHandleNew<Meta = VecData<Self::Data, Self::Index>>,
{
    const NEW: Self = Self::NEW;

    #[inline]
    fn vec_try_new(capacity: Self::Index, exact: bool) -> Result<Self, StorageError> {
        Self::alloc_handle_in(
            Self::NEW_ALLOC,
            VecHeader {
                capacity,
                length: Self::Index::ZERO,
            },
            exact,
        )
    }
}

pub trait VecBufferSpawn: VecBuffer {
    fn vec_try_spawn(&self, capacity: Self::Index, exact: bool) -> Result<Self, StorageError>;
}

impl<B, T, I: Index> VecBufferSpawn for B
where
    B: AllocHandle<Meta = VecData<T, I>>,
    B::Alloc: Clone,
{
    #[inline]
    fn vec_try_spawn(&self, capacity: Self::Index, exact: bool) -> Result<Self, StorageError> {
        self.spawn_handle(
            VecHeader {
                capacity,
                length: Index::ZERO,
            },
            exact,
        )
    }
}

impl<'a, T: 'a, const N: usize> VecBuffer for InlineBuffer<T, N> {
    type Data = T;
    type Index = usize;

    #[inline]
    fn capacity(&self) -> usize {
        N
    }

    #[inline]
    fn length(&self) -> usize {
        self.length
    }

    #[inline]
    unsafe fn set_length(&mut self, len: usize) {
        self.length = len;
    }

    #[inline]
    fn vec_try_resize(&mut self, capacity: Self::Index, exact: bool) -> Result<(), StorageError> {
        if (!exact && capacity.to_usize() < N) || capacity.to_usize() == N {
            Ok(())
        } else {
            Err(StorageError::CapacityLimit)
        }
    }
}

impl<T, const N: usize> VecBufferNew for InlineBuffer<T, N> {
    const NEW: Self = InlineBuffer::new();

    #[inline]
    fn vec_try_new(capacity: Self::Index, exact: bool) -> Result<Self, StorageError> {
        if (!exact && capacity.to_usize() < N) || capacity.to_usize() == N {
            Ok(Self::NEW)
        } else {
            Err(StorageError::CapacityLimit)
        }
    }
}

impl<T, const N: usize> VecBufferSpawn for InlineBuffer<T, N> {
    #[inline]
    fn vec_try_spawn(&self, capacity: Self::Index, exact: bool) -> Result<Self, StorageError> {
        Self::vec_try_new(capacity, exact)
    }
}