hexga_array/
extension.rs

1pub trait ExtensionToArray<T, const N : usize> : Into<[T; N]> { fn to_array(self) -> [T; N] { self.into() } }
2impl<C, T, const N : usize> ExtensionToArray<T, N> for C where C : Into<[T; N]> {}
3
4pub trait ExtensionFromArray<T, const N : usize> : From<[T; N]> { fn from_array(value : [T; N]) -> Self { value.into() } }
5impl<C, T, const N : usize> ExtensionFromArray<T, N> for C where C : From<[T; N]> {}
6
7pub trait ExtensionAsArray<T, const N : usize> : AsRef<[T; N]> { fn as_array(&self) -> &[T; N] { self.as_ref() } }
8impl<C, T, const N : usize> ExtensionAsArray<T, N> for C where C : AsRef<[T; N]> {}
9
10pub trait ExtensionAsArrayMut<T, const N : usize> : AsMut<[T; N]> { fn as_array_mut(&mut self) -> &mut [T; N] { self.as_mut() } }
11impl<C, T, const N : usize> ExtensionAsArrayMut<T, N> for C where C : AsMut<[T; N]> {}
12
13pub trait ExtensionAsSlice<T> : AsRef<[T]> { fn as_slice(&self) -> &[T] { self.as_ref() } }
14impl<C, T> ExtensionAsSlice<T> for C where C : AsRef<[T]> {}
15
16pub trait ExtensionAsSliceMut<T> : AsMut<[T]> { fn as_mut_slice(&mut self) -> &mut [T] { self.as_mut() } }
17impl<C, T> ExtensionAsSliceMut<T> for C where C : AsMut<[T]> {}
18
19
20
21pub trait ExtensionArrayToTuple
22{
23    type Tuple;
24    fn to_tuple(self) -> Self::Tuple;
25    fn from_tuple(value : Self::Tuple) -> Self;
26}
27
28impl<T> ExtensionArrayToTuple for [T; 0]
29{
30    type Tuple=();
31    fn to_tuple(self) -> Self::Tuple { }
32    fn from_tuple(_ : Self::Tuple) -> Self { [] }
33}
34
35impl<T> ExtensionArrayToTuple for [T; 1]
36{
37    type Tuple=(T,);
38    fn to_tuple(self) -> Self::Tuple { let [a] = self; (a,) }
39    fn from_tuple(t : Self::Tuple) -> Self { [t.0] }
40}
41
42impl<T> ExtensionArrayToTuple for [T; 2]
43{
44    type Tuple=(T,T,);
45    fn to_tuple(self) -> Self::Tuple { let [a, b] = self; (a,b) }
46    fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1] }
47}
48
49impl<T> ExtensionArrayToTuple for [T; 3]
50{
51    type Tuple=(T,T,T,);
52    fn to_tuple(self) -> Self::Tuple { let [a, b, c] = self; (a,b,c) }
53    fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1, t.2] }
54}
55
56impl<T> ExtensionArrayToTuple for [T; 4]
57{
58    type Tuple=(T,T,T,T,);
59    fn to_tuple(self) -> Self::Tuple { let [a, b, c,d] = self; (a,b,c,d) }
60    fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1, t.2,t.3] }
61}
62
63impl<T> ExtensionArrayToTuple for [T; 5]
64{
65    type Tuple=(T,T,T,T,T,);
66    fn to_tuple(self) -> Self::Tuple { let [a, b, c,d, e] = self; (a,b,c,d,e) }
67    fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1, t.2,t.3, t.4] }
68}
69
70impl<T> ExtensionArrayToTuple for [T; 6]
71{
72    type Tuple=(T,T,T,T,T,T,);
73    fn to_tuple(self) -> Self::Tuple { let [a, b, c,d, e,f] = self; (a,b,c,d,e,f) }
74    fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1, t.2,t.3, t.4, t.5] }
75}
76
77impl<T> ExtensionArrayToTuple for [T; 7]
78{
79    type Tuple=(T,T,T,T,T,T,T,);
80    fn to_tuple(self) -> Self::Tuple { let [a, b, c,d, e,f, g] = self; (a,b,c,d,e,f,g) }
81    fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1, t.2,t.3, t.4, t.5, t.6] }
82}
83
84impl<T> ExtensionArrayToTuple for [T; 8]
85{
86    type Tuple=(T,T,T,T,T,T,T,T,);
87    fn to_tuple(self) -> Self::Tuple { let [a, b, c,d, e,f, g, h] = self; (a,b,c,d,e,f,g,h) }
88    fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1, t.2,t.3, t.4, t.5, t.6, t.7] }
89}
90