Skip to main content

aeon_tensor/
storage.rs

1pub trait TensorStorageRef {
2    /// Reference to underlying buffer for tensor storage.
3    /// This is only gaurenteed to be >= the most recent call to length.
4    fn buffer(&self) -> &[f64];
5}
6
7pub trait TensorStorageMut: TensorStorageRef {
8    fn buffer_mut(&mut self) -> &mut [f64];
9}
10
11pub trait TensorStorageOwned: TensorStorageMut {
12    fn resize(&mut self, length: usize);
13}
14
15// *********************************
16// Implementations
17
18impl TensorStorageRef for f64 {
19    fn buffer(&self) -> &[f64] {
20        std::slice::from_ref(self)
21    }
22}
23
24impl TensorStorageMut for f64 {
25    fn buffer_mut(&mut self) -> &mut [f64] {
26        std::slice::from_mut(self)
27    }
28}
29
30impl TensorStorageOwned for f64 {
31    fn resize(&mut self, length: usize) {
32        assert!(
33            length <= 1,
34            "f64 storage does not have capacity for {} elements",
35            length
36        );
37    }
38}
39
40impl<const L: usize> TensorStorageRef for [f64; L] {
41    fn buffer(&self) -> &[f64] {
42        self.as_slice()
43    }
44}
45
46impl<const L: usize> TensorStorageMut for [f64; L] {
47    fn buffer_mut(&mut self) -> &mut [f64] {
48        self.as_mut_slice()
49    }
50}
51
52impl<const L: usize> TensorStorageOwned for [f64; L] {
53    fn resize(&mut self, length: usize) {
54        assert!(
55            length <= L,
56            "static storage of length {} does not have capacity for {} elements",
57            L,
58            length
59        );
60    }
61}
62
63impl<'a> TensorStorageRef for &'a [f64] {
64    fn buffer(&self) -> &[f64] {
65        self
66    }
67}
68
69impl<'a> TensorStorageRef for &'a mut [f64] {
70    fn buffer(&self) -> &[f64] {
71        self
72    }
73}
74
75impl<'a> TensorStorageMut for &'a mut [f64] {
76    fn buffer_mut(&mut self) -> &mut [f64] {
77        self
78    }
79}
80
81impl TensorStorageRef for Vec<f64> {
82    fn buffer(&self) -> &[f64] {
83        self
84    }
85}
86
87impl<'a> TensorStorageMut for Vec<f64> {
88    fn buffer_mut(&mut self) -> &mut [f64] {
89        self
90    }
91}
92
93impl<'a> TensorStorageOwned for Vec<f64> {
94    fn resize(&mut self, length: usize) {
95        assert!(
96            length <= self.len(),
97            "slice storage of length {} does not have capacity for {} elements",
98            self.len(),
99            length
100        );
101    }
102}