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![
194                    value.0.try_into().ok().unwrap(),
195                    value.1.try_into().ok().unwrap(),
196                ]))
197            }
198        }
199
200        impl<F1, F2, F3> TryFrom<(F1, F2, F3)> for AxesIndex<$t>
201        where
202            $t: TryFrom<F1> + TryFrom<F2> + TryFrom<F3>,
203        {
204            type Error = Error;
205
206            fn try_from(value: (F1, F2, F3)) -> Result<Self> {
207                Ok(AxesIndex::Vec(vec![
208                    value.0.try_into().ok().unwrap(),
209                    value.1.try_into().ok().unwrap(),
210                    value.2.try_into().ok().unwrap(),
211                ]))
212            }
213        }
214
215        impl<F1, F2, F3, F4> TryFrom<(F1, F2, F3, F4)> for AxesIndex<$t>
216        where
217            $t: TryFrom<F1> + TryFrom<F2> + TryFrom<F3> + TryFrom<F4>,
218        {
219            type Error = Error;
220
221            fn try_from(value: (F1, F2, F3, F4)) -> Result<Self> {
222                Ok(AxesIndex::Vec(vec![
223                    value.0.try_into().ok().unwrap(),
224                    value.1.try_into().ok().unwrap(),
225                    value.2.try_into().ok().unwrap(),
226                    value.3.try_into().ok().unwrap(),
227                ]))
228            }
229        }
230
231        impl<F1, F2, F3, F4, F5> TryFrom<(F1, F2, F3, F4, F5)> for AxesIndex<$t>
232        where
233            $t: TryFrom<F1> + TryFrom<F2> + TryFrom<F3> + TryFrom<F4> + TryFrom<F5>,
234        {
235            type Error = Error;
236
237            fn try_from(value: (F1, F2, F3, F4, F5)) -> Result<Self> {
238                Ok(AxesIndex::Vec(vec![
239                    value.0.try_into().ok().unwrap(),
240                    value.1.try_into().ok().unwrap(),
241                    value.2.try_into().ok().unwrap(),
242                    value.3.try_into().ok().unwrap(),
243                    value.4.try_into().ok().unwrap(),
244                ]))
245            }
246        }
247
248        impl<F1, F2, F3, F4, F5, F6> TryFrom<(F1, F2, F3, F4, F5, F6)> for AxesIndex<$t>
249        where
250            $t: TryFrom<F1> + TryFrom<F2> + TryFrom<F3> + TryFrom<F4> + TryFrom<F5> + TryFrom<F6>,
251        {
252            type Error = Error;
253
254            fn try_from(value: (F1, F2, F3, F4, F5, F6)) -> Result<Self> {
255                Ok(AxesIndex::Vec(vec![
256                    value.0.try_into().ok().unwrap(),
257                    value.1.try_into().ok().unwrap(),
258                    value.2.try_into().ok().unwrap(),
259                    value.3.try_into().ok().unwrap(),
260                    value.4.try_into().ok().unwrap(),
261                    value.5.try_into().ok().unwrap(),
262                ]))
263            }
264        }
265
266        impl<F1, F2, F3, F4, F5, F6, F7> TryFrom<(F1, F2, F3, F4, F5, F6, F7)> for AxesIndex<$t>
267        where
268            $t: TryFrom<F1>
269                + TryFrom<F2>
270                + TryFrom<F3>
271                + TryFrom<F4>
272                + TryFrom<F5>
273                + TryFrom<F6>
274                + TryFrom<F7>,
275        {
276            type Error = Error;
277
278            fn try_from(value: (F1, F2, F3, F4, F5, F6, F7)) -> Result<Self> {
279                Ok(AxesIndex::Vec(vec![
280                    value.0.try_into().ok().unwrap(),
281                    value.1.try_into().ok().unwrap(),
282                    value.2.try_into().ok().unwrap(),
283                    value.3.try_into().ok().unwrap(),
284                    value.4.try_into().ok().unwrap(),
285                    value.5.try_into().ok().unwrap(),
286                    value.6.try_into().ok().unwrap(),
287                ]))
288            }
289        }
290
291        impl<F1, F2, F3, F4, F5, F6, F7, F8> TryFrom<(F1, F2, F3, F4, F5, F6, F7, F8)>
292            for AxesIndex<$t>
293        where
294            $t: TryFrom<F1>
295                + TryFrom<F2>
296                + TryFrom<F3>
297                + TryFrom<F4>
298                + TryFrom<F5>
299                + TryFrom<F6>
300                + TryFrom<F7>
301                + TryFrom<F8>,
302        {
303            type Error = Error;
304
305            fn try_from(value: (F1, F2, F3, F4, F5, F6, F7, F8)) -> Result<Self> {
306                Ok(AxesIndex::Vec(vec![
307                    value.0.try_into().ok().unwrap(),
308                    value.1.try_into().ok().unwrap(),
309                    value.2.try_into().ok().unwrap(),
310                    value.3.try_into().ok().unwrap(),
311                    value.4.try_into().ok().unwrap(),
312                    value.5.try_into().ok().unwrap(),
313                    value.6.try_into().ok().unwrap(),
314                    value.7.try_into().ok().unwrap(),
315                ]))
316            }
317        }
318
319        impl<F1, F2, F3, F4, F5, F6, F7, F8, F9> TryFrom<(F1, F2, F3, F4, F5, F6, F7, F8, F9)>
320            for AxesIndex<$t>
321        where
322            $t: TryFrom<F1>
323                + TryFrom<F2>
324                + TryFrom<F3>
325                + TryFrom<F4>
326                + TryFrom<F5>
327                + TryFrom<F6>
328                + TryFrom<F7>
329                + TryFrom<F8>
330                + TryFrom<F9>,
331        {
332            type Error = Error;
333
334            fn try_from(value: (F1, F2, F3, F4, F5, F6, F7, F8, F9)) -> Result<Self> {
335                Ok(AxesIndex::Vec(vec![
336                    value.0.try_into().ok().unwrap(),
337                    value.1.try_into().ok().unwrap(),
338                    value.2.try_into().ok().unwrap(),
339                    value.3.try_into().ok().unwrap(),
340                    value.4.try_into().ok().unwrap(),
341                    value.5.try_into().ok().unwrap(),
342                    value.6.try_into().ok().unwrap(),
343                    value.7.try_into().ok().unwrap(),
344                    value.8.try_into().ok().unwrap(),
345                ]))
346            }
347        }
348
349        impl<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10>
350            TryFrom<(F1, F2, F3, F4, F5, F6, F7, F8, F9, F10)> for AxesIndex<$t>
351        where
352            $t: TryFrom<F1>
353                + TryFrom<F2>
354                + TryFrom<F3>
355                + TryFrom<F4>
356                + TryFrom<F5>
357                + TryFrom<F6>
358                + TryFrom<F7>
359                + TryFrom<F8>
360                + TryFrom<F9>
361                + TryFrom<F10>,
362        {
363            type Error = Error;
364
365            fn try_from(value: (F1, F2, F3, F4, F5, F6, F7, F8, F9, F10)) -> Result<Self> {
366                Ok(AxesIndex::Vec(vec![
367                    value.0.try_into().ok().unwrap(),
368                    value.1.try_into().ok().unwrap(),
369                    value.2.try_into().ok().unwrap(),
370                    value.3.try_into().ok().unwrap(),
371                    value.4.try_into().ok().unwrap(),
372                    value.5.try_into().ok().unwrap(),
373                    value.6.try_into().ok().unwrap(),
374                    value.7.try_into().ok().unwrap(),
375                    value.8.try_into().ok().unwrap(),
376                    value.9.try_into().ok().unwrap(),
377                ]))
378            }
379        }
380    };
381}
382
383impl_from_tuple_to_axes_index!(isize);
384impl_from_tuple_to_axes_index!(usize);
385
386/* #endregion AxisIndex tuple-type from */