generic_array_storage/
conv.rs

1use generic_array::{ArrayLength, IntoArrayLength};
2use nalgebra::{Const, DimName, DimNameAdd, DimNameMul, DimNameProd, DimNameSum, U0, U1, U2};
3use typenum::{B0, B1, UInt, UTerm};
4
5/// Convenience trait, used to define type **conv**ersions
6///
7/// Also, this is the only bound to [`GenericMatrix`](super::GenericMatrix) type alias, meaning that all of the following are valid [`GenericMatrix`](super::GenericMatrix)es:
8/// ```rust
9/// # use generic_array_storage::GenericMatrix;
10/// type NalgebraMatrix = GenericMatrix<i32, nalgebra::U3, nalgebra::U4>;
11/// type TypenumMatrix = GenericMatrix<i32, typenum::U3, typenum::U4>;
12/// type TypenumConstMatrix = GenericMatrix<i32, typenum::Const<3>, typenum::Const<3>>;
13/// // (nalgebra::Const are actually aliased by nalgebra::{U1, U2, ...})
14/// ```
15pub trait Conv {
16    /// [`typenum`]-faced type (unsigned int)
17    type TNum: ArrayLength;
18
19    /// [`nalgebra`]-faced type (matrix dimension)
20    type Nalg: DimName;
21
22    /// Constructor method used in [`nalgebra`] implementations
23    fn new_nalg() -> Self::Nalg {
24        Self::Nalg::name()
25    }
26}
27
28impl Conv for UTerm {
29    type Nalg = U0;
30
31    type TNum = Self;
32}
33
34impl<U: Conv> Conv for UInt<U, B1>
35where
36    U: ArrayLength,
37    UInt<U, B0>: Conv,
38    <UInt<U, B0> as Conv>::Nalg: DimNameAdd<U1>,
39{
40    type TNum = Self;
41
42    type Nalg = DimNameSum<<UInt<U, B0> as Conv>::Nalg, U1>;
43}
44
45impl<U: Conv> Conv for UInt<U, B0>
46where
47    U: ArrayLength,
48    U::Nalg: DimNameMul<U2>,
49{
50    type TNum = Self;
51
52    type Nalg = DimNameProd<U::Nalg, U2>;
53}
54type TNum<const N: usize> = typenum::Const<N>;
55
56impl<const N: usize> Conv for TNum<N>
57where
58    Self: IntoArrayLength,
59{
60    type TNum = <Self as IntoArrayLength>::ArrayLength;
61
62    type Nalg = Const<N>;
63}
64
65impl<const N: usize> Conv for Const<N>
66where
67    TNum<N>: IntoArrayLength,
68{
69    type TNum = <TNum<N> as IntoArrayLength>::ArrayLength;
70
71    type Nalg = Self;
72}