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
use core::{
    ops::{Deref, DerefMut},
    ptr::null_mut,
};

use crate::{shape::Shape, CommonPtrs, PtrType, ShallowCopy};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StackArray<S: Shape, T> {
    pub array: S::ARR<T>,
    _private: (),
}

impl<S: Shape, T: Default + Copy> StackArray<S, T> {
    #[inline]
    pub fn new() -> Self {
        // TODO: one day... use const expressions
        assert!(S::LEN > 0,
            "The size (N) of a stack allocated buffer must be greater than 0."
        );
        StackArray { array: S::new(), _private: () }
    }
}

impl<S: Shape, T> StackArray<S, T> {
    pub fn from_array(array: S::ARR<T>) -> Self {
        assert!(S::LEN > 0,
            "The size (N) of a stack allocated buffer must be greater than 0."
        );
        
        StackArray {
            array,
            _private: ()
        }
    }
}

impl<S: Shape, T> StackArray<S, T> {
    #[inline]
    pub const fn as_ptr(&self) -> *const T {
        &self.array as *const S::ARR<T> as *const T
    }

    #[inline]
    pub fn as_ptr_mut(&mut self) -> *mut T {
        &mut self.array as *mut S::ARR<T> as *mut T
    }

    #[inline]
    pub const unsafe fn flatten(&self) -> &[T] {
        core::slice::from_raw_parts(self.as_ptr(), S::LEN)
    }

    #[inline]
    pub unsafe fn flatten_mut(&mut self) -> &mut [T] {
        core::slice::from_raw_parts_mut(self.as_ptr_mut(), S::LEN)
    }
}

impl<S: Shape, T> Deref for StackArray<S, T> {
    type Target = [T];

    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe { self.flatten() }
    }
}

impl<S: Shape, T> DerefMut for StackArray<S, T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { self.flatten_mut() }
    }
}

impl<S: Shape, T> PtrType for StackArray<S, T> {
    #[inline]
    fn len(&self) -> usize {
        S::LEN
    }

    #[inline]
    fn flag(&self) -> crate::flag::AllocFlag {
        crate::flag::AllocFlag::None
    }
}

impl<S: Shape, T> CommonPtrs<T> for StackArray<S, T> {
    #[inline]
    fn ptrs(&self) -> (*const T, *mut core::ffi::c_void, u64) {
        (self.as_ptr(), null_mut(), 0)
    }

    #[inline]
    fn ptrs_mut(&mut self) -> (*mut T, *mut core::ffi::c_void, u64) {
        (self.as_mut_ptr(), null_mut(), 0)
    }
}

impl<S: Shape, T> ShallowCopy for StackArray<S, T>
where
    S::ARR<T>: Copy,
{
    #[inline]
    unsafe fn shallow(&self) -> Self {
        StackArray { array: self.array, _private: () }
    }
}