rstsr_common/
axis_index.rs

1use crate::prelude_dev::*;
2
3/// Enum for Axes indexing
4pub enum AxesIndex<T> {
5    Val(T),
6    Vec(Vec<T>),
7}
8
9impl<T> AsRef<[T]> for AxesIndex<T> {
10    fn as_ref(&self) -> &[T] {
11        match self {
12            AxesIndex::Val(v) => core::slice::from_ref(v),
13            AxesIndex::Vec(v) => v.as_slice(),
14        }
15    }
16}
17
18/* #region AxisIndex self-type from */
19
20impl<T> From<T> for AxesIndex<T> {
21    fn from(value: T) -> Self {
22        AxesIndex::Val(value)
23    }
24}
25
26impl<T> From<&T> for AxesIndex<T>
27where
28    T: Clone,
29{
30    fn from(value: &T) -> Self {
31        AxesIndex::Val(value.clone())
32    }
33}
34
35impl<T> TryFrom<Vec<T>> for AxesIndex<T> {
36    type Error = Error;
37
38    fn try_from(value: Vec<T>) -> Result<Self> {
39        Ok(AxesIndex::Vec(value))
40    }
41}
42
43impl<T, const N: usize> TryFrom<[T; N]> for AxesIndex<T>
44where
45    T: Clone,
46{
47    type Error = Error;
48
49    fn try_from(value: [T; N]) -> Result<Self> {
50        Ok(AxesIndex::Vec(value.to_vec()))
51    }
52}
53
54impl<T> TryFrom<&Vec<T>> for AxesIndex<T>
55where
56    T: Clone,
57{
58    type Error = Error;
59
60    fn try_from(value: &Vec<T>) -> Result<Self> {
61        Ok(AxesIndex::Vec(value.clone()))
62    }
63}
64
65impl<T> TryFrom<&[T]> for AxesIndex<T>
66where
67    T: Clone,
68{
69    type Error = Error;
70
71    fn try_from(value: &[T]) -> Result<Self> {
72        Ok(AxesIndex::Vec(value.to_vec()))
73    }
74}
75
76impl<T, const N: usize> TryFrom<&[T; N]> for AxesIndex<T>
77where
78    T: Clone,
79{
80    type Error = Error;
81
82    fn try_from(value: &[T; N]) -> Result<Self> {
83        Ok(AxesIndex::Vec(value.to_vec()))
84    }
85}
86
87impl TryFrom<()> for AxesIndex<usize> {
88    type Error = Error;
89
90    fn try_from(_: ()) -> Result<Self> {
91        Ok(AxesIndex::Vec(vec![]))
92    }
93}
94
95impl TryFrom<()> for AxesIndex<isize> {
96    type Error = Error;
97
98    fn try_from(_: ()) -> Result<Self> {
99        Ok(AxesIndex::Vec(vec![]))
100    }
101}
102
103/* #endregion AxisIndex self-type from */
104
105/* #region AxisIndex other-type from */
106
107macro_rules! impl_try_from_axes_index {
108    ($t1:ty, $($t2:ty),*) => {
109        $(
110            impl TryFrom<$t2> for AxesIndex<$t1> {
111                type Error = Error;
112
113                fn try_from(value: $t2) -> Result<Self> {
114                    Ok(AxesIndex::Val(value.try_into()?))
115                }
116            }
117
118            impl TryFrom<&$t2> for AxesIndex<$t1> {
119                type Error = Error;
120
121                fn try_from(value: &$t2) -> Result<Self> {
122                    Ok(AxesIndex::Val((*value).try_into()?))
123                }
124            }
125
126            impl TryFrom<Vec<$t2>> for AxesIndex<$t1> {
127                type Error = Error;
128
129                fn try_from(value: Vec<$t2>) -> Result<Self> {
130                    let value = value
131                        .into_iter()
132                        .map(|v| v.try_into().map_err(|_| Error::TryFromIntError(String::new())))
133                        .collect::<Result<Vec<$t1>>>()?;
134                    Ok(AxesIndex::Vec(value))
135                }
136            }
137
138            impl<const N: usize> TryFrom<[$t2; N]> for AxesIndex<$t1> {
139                type Error = Error;
140
141                fn try_from(value: [$t2; N]) -> Result<Self> {
142                    value.to_vec().try_into()
143                }
144            }
145
146            impl TryFrom<&Vec<$t2>> for AxesIndex<$t1> {
147                type Error = Error;
148
149                fn try_from(value: &Vec<$t2>) -> Result<Self> {
150                    value.to_vec().try_into()
151                }
152            }
153
154            impl TryFrom<&[$t2]> for AxesIndex<$t1> {
155                type Error = Error;
156
157                fn try_from(value: &[$t2]) -> Result<Self> {
158                    value.to_vec().try_into()
159                }
160            }
161
162            impl<const N: usize> TryFrom<&[$t2; N]> for AxesIndex<$t1> {
163                type Error = Error;
164
165                fn try_from(value: &[$t2; N]) -> Result<Self> {
166                    value.to_vec().try_into()
167                }
168            }
169        )*
170    };
171}
172
173impl_try_from_axes_index!(usize, isize, u32, u64, i32, i64);
174impl_try_from_axes_index!(isize, usize, u32, u64, i32, i64);
175
176/* #endregion AxisIndex other-type from */
177
178/* #region AxisIndex tuple-type from */
179
180// it seems that this directly implementing arbitary AxesIndex<T> will cause
181// conflicting implementation so make a macro for this task
182
183#[macro_export]
184macro_rules! impl_from_tuple_to_axes_index {
185    ($t: ty) => {
186        impl<F1, F2> TryFrom<(F1, F2)> for AxesIndex<$t>
187        where
188            $t: TryFrom<F1> + TryFrom<F2>,
189        {
190            type Error = Error;
191
192            fn try_from(value: (F1, F2)) -> Result<Self> {
193                Ok(AxesIndex::Vec(vec![value.0.try_into().ok().unwrap(), value.1.try_into().ok().unwrap()]))
194            }
195        }
196
197        impl<F1, F2, F3> TryFrom<(F1, F2, F3)> for AxesIndex<$t>
198        where
199            $t: TryFrom<F1> + TryFrom<F2> + TryFrom<F3>,
200        {
201            type Error = Error;
202
203            fn try_from(value: (F1, F2, F3)) -> Result<Self> {
204                Ok(AxesIndex::Vec(vec![
205                    value.0.try_into().ok().unwrap(),
206                    value.1.try_into().ok().unwrap(),
207                    value.2.try_into().ok().unwrap(),
208                ]))
209            }
210        }
211
212        impl<F1, F2, F3, F4> TryFrom<(F1, F2, F3, F4)> for AxesIndex<$t>
213        where
214            $t: TryFrom<F1> + TryFrom<F2> + TryFrom<F3> + TryFrom<F4>,
215        {
216            type Error = Error;
217
218            fn try_from(value: (F1, F2, F3, F4)) -> Result<Self> {
219                Ok(AxesIndex::Vec(vec![
220                    value.0.try_into().ok().unwrap(),
221                    value.1.try_into().ok().unwrap(),
222                    value.2.try_into().ok().unwrap(),
223                    value.3.try_into().ok().unwrap(),
224                ]))
225            }
226        }
227
228        impl<F1, F2, F3, F4, F5> TryFrom<(F1, F2, F3, F4, F5)> for AxesIndex<$t>
229        where
230            $t: TryFrom<F1> + TryFrom<F2> + TryFrom<F3> + TryFrom<F4> + TryFrom<F5>,
231        {
232            type Error = Error;
233
234            fn try_from(value: (F1, F2, F3, F4, F5)) -> Result<Self> {
235                Ok(AxesIndex::Vec(vec![
236                    value.0.try_into().ok().unwrap(),
237                    value.1.try_into().ok().unwrap(),
238                    value.2.try_into().ok().unwrap(),
239                    value.3.try_into().ok().unwrap(),
240                    value.4.try_into().ok().unwrap(),
241                ]))
242            }
243        }
244
245        impl<F1, F2, F3, F4, F5, F6> TryFrom<(F1, F2, F3, F4, F5, F6)> for AxesIndex<$t>
246        where
247            $t: TryFrom<F1> + TryFrom<F2> + TryFrom<F3> + TryFrom<F4> + TryFrom<F5> + TryFrom<F6>,
248        {
249            type Error = Error;
250
251            fn try_from(value: (F1, F2, F3, F4, F5, F6)) -> Result<Self> {
252                Ok(AxesIndex::Vec(vec![
253                    value.0.try_into().ok().unwrap(),
254                    value.1.try_into().ok().unwrap(),
255                    value.2.try_into().ok().unwrap(),
256                    value.3.try_into().ok().unwrap(),
257                    value.4.try_into().ok().unwrap(),
258                    value.5.try_into().ok().unwrap(),
259                ]))
260            }
261        }
262
263        impl<F1, F2, F3, F4, F5, F6, F7> TryFrom<(F1, F2, F3, F4, F5, F6, F7)> for AxesIndex<$t>
264        where
265            $t: TryFrom<F1> + TryFrom<F2> + TryFrom<F3> + TryFrom<F4> + TryFrom<F5> + TryFrom<F6> + TryFrom<F7>,
266        {
267            type Error = Error;
268
269            fn try_from(value: (F1, F2, F3, F4, F5, F6, F7)) -> Result<Self> {
270                Ok(AxesIndex::Vec(vec![
271                    value.0.try_into().ok().unwrap(),
272                    value.1.try_into().ok().unwrap(),
273                    value.2.try_into().ok().unwrap(),
274                    value.3.try_into().ok().unwrap(),
275                    value.4.try_into().ok().unwrap(),
276                    value.5.try_into().ok().unwrap(),
277                    value.6.try_into().ok().unwrap(),
278                ]))
279            }
280        }
281
282        impl<F1, F2, F3, F4, F5, F6, F7, F8> TryFrom<(F1, F2, F3, F4, F5, F6, F7, F8)> for AxesIndex<$t>
283        where
284            $t: TryFrom<F1>
285                + TryFrom<F2>
286                + TryFrom<F3>
287                + TryFrom<F4>
288                + TryFrom<F5>
289                + TryFrom<F6>
290                + TryFrom<F7>
291                + TryFrom<F8>,
292        {
293            type Error = Error;
294
295            fn try_from(value: (F1, F2, F3, F4, F5, F6, F7, F8)) -> Result<Self> {
296                Ok(AxesIndex::Vec(vec![
297                    value.0.try_into().ok().unwrap(),
298                    value.1.try_into().ok().unwrap(),
299                    value.2.try_into().ok().unwrap(),
300                    value.3.try_into().ok().unwrap(),
301                    value.4.try_into().ok().unwrap(),
302                    value.5.try_into().ok().unwrap(),
303                    value.6.try_into().ok().unwrap(),
304                    value.7.try_into().ok().unwrap(),
305                ]))
306            }
307        }
308
309        impl<F1, F2, F3, F4, F5, F6, F7, F8, F9> TryFrom<(F1, F2, F3, F4, F5, F6, F7, F8, F9)> for AxesIndex<$t>
310        where
311            $t: TryFrom<F1>
312                + TryFrom<F2>
313                + TryFrom<F3>
314                + TryFrom<F4>
315                + TryFrom<F5>
316                + TryFrom<F6>
317                + TryFrom<F7>
318                + TryFrom<F8>
319                + TryFrom<F9>,
320        {
321            type Error = Error;
322
323            fn try_from(value: (F1, F2, F3, F4, F5, F6, F7, F8, F9)) -> Result<Self> {
324                Ok(AxesIndex::Vec(vec![
325                    value.0.try_into().ok().unwrap(),
326                    value.1.try_into().ok().unwrap(),
327                    value.2.try_into().ok().unwrap(),
328                    value.3.try_into().ok().unwrap(),
329                    value.4.try_into().ok().unwrap(),
330                    value.5.try_into().ok().unwrap(),
331                    value.6.try_into().ok().unwrap(),
332                    value.7.try_into().ok().unwrap(),
333                    value.8.try_into().ok().unwrap(),
334                ]))
335            }
336        }
337
338        impl<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10> TryFrom<(F1, F2, F3, F4, F5, F6, F7, F8, F9, F10)>
339            for AxesIndex<$t>
340        where
341            $t: TryFrom<F1>
342                + TryFrom<F2>
343                + TryFrom<F3>
344                + TryFrom<F4>
345                + TryFrom<F5>
346                + TryFrom<F6>
347                + TryFrom<F7>
348                + TryFrom<F8>
349                + TryFrom<F9>
350                + TryFrom<F10>,
351        {
352            type Error = Error;
353
354            fn try_from(value: (F1, F2, F3, F4, F5, F6, F7, F8, F9, F10)) -> Result<Self> {
355                Ok(AxesIndex::Vec(vec![
356                    value.0.try_into().ok().unwrap(),
357                    value.1.try_into().ok().unwrap(),
358                    value.2.try_into().ok().unwrap(),
359                    value.3.try_into().ok().unwrap(),
360                    value.4.try_into().ok().unwrap(),
361                    value.5.try_into().ok().unwrap(),
362                    value.6.try_into().ok().unwrap(),
363                    value.7.try_into().ok().unwrap(),
364                    value.8.try_into().ok().unwrap(),
365                    value.9.try_into().ok().unwrap(),
366                ]))
367            }
368        }
369    };
370}
371
372impl_from_tuple_to_axes_index!(isize);
373impl_from_tuple_to_axes_index!(usize);
374
375/* #endregion AxisIndex tuple-type from */