1pub trait ToArray<T, const N : usize> : Into<[T; N]> { #[inline(always)] fn to_array(self) -> [T; N] { self.into() } }
2impl<C, T, const N : usize> ToArray<T, N> for C where C : Into<[T; N]> {}
3
4pub trait FromArray<T, const N : usize> : From<[T; N]>
5{
6 #[inline(always)] fn from_array(value : [T; N]) -> Self { value.into() }
7 fn from_fn<F>(f : F) -> Self where F : FnMut(usize) -> T { Self::from_array(std::array::from_fn(f)) }
8 fn splat(val : T) -> Self where T: Clone { Self::from_fn(|_| val.clone()) }
9}
10impl<C, T, const N : usize> FromArray<T, N> for C where C : From<[T; N]> {}
11
12pub trait AsArray<T, const N : usize> : AsRef<[T; N]> { #[inline(always)] fn as_array(&self) -> &[T; N] { self.as_ref() } }
13impl<C, T, const N : usize> AsArray<T, N> for C where C : AsRef<[T; N]> {}
14
15pub trait AsArrayMut<T, const N : usize> : AsMut<[T; N]> { #[inline(always)] fn as_array_mut(&mut self) -> &mut [T; N] { self.as_mut() } }
16impl<C, T, const N : usize> AsArrayMut<T, N> for C where C : AsMut<[T; N]> {}
17
18pub trait AsSlice<T> : AsRef<[T]> { #[inline(always)] fn as_slice(&self) -> &[T] { self.as_ref() } }
19impl<C, T> AsSlice<T> for C where C : AsRef<[T]> {}
20
21pub trait AsMutSlice<T> : AsMut<[T]> { #[inline(always)] fn as_mut_slice(&mut self) -> &mut [T] { self.as_mut() } }
22impl<C, T> AsMutSlice<T> for C where C : AsMut<[T]> {}
23
24
25
26
27pub trait ToArray1<T> : Into<[T; 1]> { #[inline(always)] fn to_array1(self) -> [T; 1] { self.into() } }
28impl<C, T> ToArray1<T> for C where C : Into<[T; 1]> {}
29
30pub trait ToArray2<T> : Into<[T; 2]> { #[inline(always)] fn to_array2(self) -> [T; 2] { self.into() } }
31impl<C, T> ToArray2<T> for C where C : Into<[T; 2]> {}
32
33pub trait ToArray3<T> : Into<[T; 3]> { #[inline(always)] fn to_array3(self) -> [T; 3] { self.into() } }
34impl<C, T> ToArray3<T> for C where C : Into<[T; 3]> {}
35
36pub trait ToArray4<T> : Into<[T; 4]> { #[inline(always)] fn to_array4(self) -> [T; 4] { self.into() } }
37impl<C, T> ToArray4<T> for C where C : Into<[T; 4]> {}
38
39pub trait ToArray5<T> : Into<[T; 5]> { #[inline(always)] fn to_array5(self) -> [T; 5] { self.into() } }
40impl<C, T> ToArray5<T> for C where C : Into<[T; 5]> {}
41
42pub trait ToArray6<T> : Into<[T; 6]> { #[inline(always)] fn to_array6(self) -> [T; 6] { self.into() } }
43impl<C, T> ToArray6<T> for C where C : Into<[T; 6]> {}
44
45pub trait ToArray7<T> : Into<[T; 7]> { #[inline(always)] fn to_array7(self) -> [T; 7] { self.into() } }
46impl<C, T> ToArray7<T> for C where C : Into<[T; 7]> {}
47
48pub trait ToArray8<T> : Into<[T; 8]> { #[inline(always)] fn to_array8(self) -> [T; 8] { self.into() } }
49impl<C, T> ToArray8<T> for C where C : Into<[T; 8]> {}
50
51
52
53pub trait ArrayToTuple
54{
55 type Tuple;
56 fn to_tuple(self) -> Self::Tuple;
57 fn from_tuple(value : Self::Tuple) -> Self;
58}
59
60impl<T> ArrayToTuple for [T; 0]
61{
62 type Tuple=();
63 fn to_tuple(self) -> Self::Tuple { }
64 fn from_tuple(_ : Self::Tuple) -> Self { [] }
65}
66
67impl<T> ArrayToTuple for [T; 1]
68{
69 type Tuple=(T,);
70 fn to_tuple(self) -> Self::Tuple { let [a] = self; (a,) }
71 fn from_tuple(t : Self::Tuple) -> Self { [t.0] }
72}
73
74impl<T> ArrayToTuple for [T; 2]
75{
76 type Tuple=(T,T,);
77 fn to_tuple(self) -> Self::Tuple { let [a, b] = self; (a,b) }
78 fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1] }
79}
80
81impl<T> ArrayToTuple for [T; 3]
82{
83 type Tuple=(T,T,T,);
84 fn to_tuple(self) -> Self::Tuple { let [a, b, c] = self; (a,b,c) }
85 fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1, t.2] }
86}
87
88impl<T> ArrayToTuple for [T; 4]
89{
90 type Tuple=(T,T,T,T,);
91 fn to_tuple(self) -> Self::Tuple { let [a, b, c,d] = self; (a,b,c,d) }
92 fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1, t.2,t.3] }
93}
94
95impl<T> ArrayToTuple for [T; 5]
96{
97 type Tuple=(T,T,T,T,T,);
98 fn to_tuple(self) -> Self::Tuple { let [a, b, c,d, e] = self; (a,b,c,d,e) }
99 fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1, t.2,t.3, t.4] }
100}
101
102impl<T> ArrayToTuple for [T; 6]
103{
104 type Tuple=(T,T,T,T,T,T,);
105 fn to_tuple(self) -> Self::Tuple { let [a, b, c,d, e,f] = self; (a,b,c,d,e,f) }
106 fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1, t.2,t.3, t.4, t.5] }
107}
108
109impl<T> ArrayToTuple for [T; 7]
110{
111 type Tuple=(T,T,T,T,T,T,T,);
112 fn to_tuple(self) -> Self::Tuple { let [a, b, c,d, e,f, g] = self; (a,b,c,d,e,f,g) }
113 fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1, t.2,t.3, t.4, t.5, t.6] }
114}
115
116impl<T> ArrayToTuple for [T; 8]
117{
118 type Tuple=(T,T,T,T,T,T,T,T,);
119 fn to_tuple(self) -> Self::Tuple { let [a, b, c,d, e,f, g, h] = self; (a,b,c,d,e,f,g,h) }
120 fn from_tuple(t : Self::Tuple) -> Self { [t.0, t.1, t.2,t.3, t.4, t.5, t.6, t.7] }
121}