nalgebra/base/
constraint.rs1use crate::base::dimension::{Dim, DimName, Dyn};
4
5#[derive(Copy, Clone, Debug)]
7pub struct ShapeConstraint;
8
9pub trait AreMultipliable<R1: Dim, C1: Dim, R2: Dim, C2: Dim>: DimEq<C1, R2> {}
11
12impl<R1: Dim, C1: Dim, R2: Dim, C2: Dim> AreMultipliable<R1, C1, R2, C2> for ShapeConstraint where
13    ShapeConstraint: DimEq<C1, R2>
14{
15}
16
17pub trait DimEq<D1: Dim, D2: Dim> {
19    type Representative: Dim;
22
23    fn representative(d1: D1, d2: D2) -> Option<Self::Representative> {
26        if d1.value() != d2.value() {
27            None
28        } else {
29            Some(Self::Representative::from_usize(d1.value()))
30        }
31    }
32}
33
34impl<D: Dim> DimEq<D, D> for ShapeConstraint {
35    type Representative = D;
36}
37
38impl<D: DimName> DimEq<D, Dyn> for ShapeConstraint {
39    type Representative = D;
40}
41
42impl<D: DimName> DimEq<Dyn, D> for ShapeConstraint {
43    type Representative = D;
44}
45
46macro_rules! equality_trait_decl(
47    ($($doc: expr, $Trait: ident),* $(,)*) => {$(
48        #[doc = $doc]
50        pub trait $Trait<D1: Dim, D2: Dim>: DimEq<D1, D2> + DimEq<D2, D1> {
51            type Representative: Dim;
54
55            fn representative(d1: D1, d2: D2) -> Option<<Self as $Trait<D1, D2>>::Representative> {
58                <Self as DimEq<D1, D2>>::representative(d1, d2)
59                    .map(|common_dim| <Self as $Trait<D1, D2>>::Representative::from_usize(common_dim.value()))
60            }
61        }
62
63        impl<D: Dim> $Trait<D, D> for ShapeConstraint {
64            type Representative = D;
65        }
66
67        impl<D: DimName> $Trait<D, Dyn> for ShapeConstraint {
68            type Representative = D;
69        }
70
71        impl<D: DimName> $Trait<Dyn, D> for ShapeConstraint {
72            type Representative = D;
73        }
74    )*}
75);
76
77equality_trait_decl!(
78    "Constrains `D1` and `D2` to be equivalent. \
79     They are both assumed to be the number of \
80     rows of a matrix.",
81    SameNumberOfRows,
82    "Constrains `D1` and `D2` to be equivalent. \
83     They are both assumed to be the number of \
84     columns of a matrix.",
85    SameNumberOfColumns
86);
87
88pub trait SameDimension<D1: Dim, D2: Dim>:
91    SameNumberOfRows<D1, D2> + SameNumberOfColumns<D1, D2>
92{
93    type Representative: Dim;
96}
97
98impl<D: Dim> SameDimension<D, D> for ShapeConstraint {
99    type Representative = D;
100}
101
102impl<D: DimName> SameDimension<D, Dyn> for ShapeConstraint {
103    type Representative = D;
104}
105
106impl<D: DimName> SameDimension<Dyn, D> for ShapeConstraint {
107    type Representative = D;
108}