vec_split/
impls.rs

1use crate::*;
2
3impl<T, const D: usize, V: Vector<T, D>> VectorArray<T, D, V, usize> for Vec<V> {
4    fn get<'a>(&'a self, index: usize) -> Option<&'a V> {
5        <[V]>::get(self, index)
6    }
7
8    fn get_mut<'a>(&'a mut self, index: usize) -> Option<&'a mut V> {
9        <[V]>::get_mut(self, index)
10    }
11}
12
13impl<T, const D: usize, V: RawVector<T, D>> SizedVectorArray<T, D, V, usize> for Vec<V> {
14    fn ptr(&self) -> *const V {
15        <[V]>::as_ptr(self)
16    }
17
18    fn ptr_mut(&mut self) -> *mut V {
19        <[V]>::as_mut_ptr(self)
20    }
21
22    fn len(&self) -> usize {
23        <[V]>::len(self)
24    }
25
26    #[inline]
27    fn convert_index(&self, index: usize) -> usize {
28        index
29    }
30}
31
32impl<T: ?Sized, const D: usize, V: Vector<T, D>> VectorArray<T, D, V, usize> for &mut [V] {
33    fn get<'a>(&'a self, index: usize) -> Option<&'a V> {
34        <[V]>::get(self, index)
35    }
36
37    fn get_mut<'a>(&'a mut self, index: usize) -> Option<&'a mut V> {
38        <[V]>::get_mut(self, index)
39    }
40}
41
42impl<T, const D: usize, V: RawVector<T, D>> SizedVectorArray<T, D, V, usize> for &mut [V] {
43    fn ptr(&self) -> *const V {
44        <[V]>::as_ptr(self)
45    }
46
47    fn ptr_mut(&mut self) -> *mut V {
48        <[V]>::as_mut_ptr(self)
49    }
50
51    fn len(&self) -> usize {
52        <[V]>::len(self)
53    }
54
55    #[inline]
56    fn convert_index(&self, index: usize) -> usize {
57        index
58    }
59}
60
61impl<T: ?Sized, const D: usize, V: Vector<T, D>, const A: usize> VectorArray<T, D, V, usize>
62    for [V; A]
63{
64    fn get<'a>(&'a self, index: usize) -> Option<&'a V> {
65        <[V]>::get(self, index)
66    }
67
68    fn get_mut<'a>(&'a mut self, index: usize) -> Option<&'a mut V> {
69        <[V]>::get_mut(self, index)
70    }
71}
72
73impl<T, const D: usize, V: RawVector<T, D>, const A: usize> SizedVectorArray<T, D, V, usize>
74    for [V; A]
75{
76    fn ptr(&self) -> *const V {
77        <[V]>::as_ptr(self)
78    }
79
80    fn ptr_mut(&mut self) -> *mut V {
81        <[V]>::as_mut_ptr(self)
82    }
83
84    fn len(&self) -> usize {
85        A
86    }
87
88    #[inline]
89    fn convert_index(&self, index: usize) -> usize {
90        index
91    }
92}
93
94macro_rules! impl_tuple {
95    ($type:tt, $amount:expr; $($item:tt ,)*) => {
96        impl<T: Sized> Vector<T, $amount> for $type {
97            fn get<'a>(&'a self, i: usize) -> Option<&'a T> {
98                match i {
99                    $(
100                        $item => Some(&self.$item),
101                    )*
102                    _ => None,
103                }
104            }
105
106            fn get_mut<'a>(&'a mut self, i: usize) -> Option<&'a mut T> {
107                match i {
108                    $(
109                        $item => Some(&mut self.$item),
110                    )*
111                    _ => None,
112                }
113            }
114        }
115        unsafe impl<T: Sized> RawVector<T, $amount> for $type {}
116    };
117}
118
119impl_tuple!((T,                             ),  1; 0,);
120impl_tuple!((T, T                           ),  2; 0, 1,);
121impl_tuple!((T, T, T                        ),  3; 0, 1, 2,);
122impl_tuple!((T, T, T, T                     ),  4; 0, 1, 2, 3,);
123impl_tuple!((T, T, T, T, T                  ),  5; 0, 1, 2, 3, 4,);
124impl_tuple!((T, T, T, T, T, T               ),  6; 0, 1, 2, 3, 4, 5,);
125impl_tuple!((T, T, T, T, T, T, T            ),  7; 0, 1, 2, 3, 4, 5, 6,);
126impl_tuple!((T, T, T, T, T, T, T, T         ),  8; 0, 1, 2, 3, 4, 5, 6, 7,);
127impl_tuple!((T, T, T, T, T, T, T, T, T      ),  9; 0, 1, 2, 3, 4, 5, 6, 7, 8,);
128impl_tuple!((T, T, T, T, T, T, T, T, T, T   ), 10; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,);
129impl_tuple!((T, T, T, T, T, T, T, T, T, T, T), 11; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,);
130
131impl<T, const D: usize> Vector<T, D> for [T; D] {
132    fn get<'a>(&'a self, index: usize) -> Option<&'a T> {
133        <[T]>::get(self, index)
134    }
135
136    fn get_mut<'a>(&'a mut self, index: usize) -> Option<&'a mut T> {
137        <[T]>::get_mut(self, index)
138    }
139}
140
141unsafe impl<T, const D: usize> RawVector<T, D> for [T; D] {}
142
143/// no [`RawVector`] here, as Vec is not aligned properly for that.
144impl<T, const D: usize> Vector<T, D> for Vec<T> {
145    fn get<'a>(&'a self, index: usize) -> Option<&'a T> {
146        <[T]>::get(self, index)
147    }
148
149    fn get_mut<'a>(&'a mut self, index: usize) -> Option<&'a mut T> {
150        <[T]>::get_mut(self, index)
151    }
152}