boseiju/utils/
containers.rs

1#[derive(serde::Serialize, serde::Deserialize)]
2#[derive(Debug, Clone, PartialEq, Eq)]
3pub struct HeapArrayVec<T, const CAP: usize> {
4    inner: Box<arrayvec::ArrayVec<T, CAP>>,
5}
6
7impl<T, const CAP: usize> HeapArrayVec<T, CAP> {
8    pub fn new() -> Self {
9        // let layout = std::alloc::Layout::new::<Box<arrayvec::ArrayVec<T, CAP>>>();
10        // let allocation = unsafe { std::alloc::alloc(layout) };
11        //
12        // let mut result = unsafe { Box::from_raw(allocation as *mut arrayvec::ArrayVec<T, CAP>) };
13        // unsafe { result.set_len(0) };
14
15        /* Fixme: this seems to work for now ? */
16        /* but it causes stack overflows if the array is too big and the compiler can't optimize it away */
17        let result = Box::new(arrayvec::ArrayVec::new_const());
18
19        Self { inner: result }
20    }
21}
22
23impl<T, const CAP: usize> std::ops::Deref for HeapArrayVec<T, CAP> {
24    type Target = arrayvec::ArrayVec<T, CAP>;
25    fn deref(&self) -> &Self::Target {
26        self.inner.deref()
27    }
28}
29
30impl<T, const CAP: usize> std::ops::DerefMut for HeapArrayVec<T, CAP> {
31    fn deref_mut(&mut self) -> &mut Self::Target {
32        self.inner.deref_mut()
33    }
34}
35
36impl<T, const CAP: usize> crate::utils::DummyInit for HeapArrayVec<T, CAP> {
37    fn dummy_init() -> Self {
38        Self::new()
39    }
40}