1//! Compatibility constraints between matrix shapes, e.g., for addition or multiplication.
23use crate::base::dimension::{Dim, DimName, Dynamic};
45/// A type used in `where` clauses for enforcing constraints.
6#[derive(Copy, Clone, Debug)]
7pub struct ShapeConstraint;
89/// Constraints `C1` and `R2` to be equivalent.
10pub trait AreMultipliable<R1: Dim, C1: Dim, R2: Dim, C2: Dim>: DimEq<C1, R2> {}
1112impl<R1: Dim, C1: Dim, R2: Dim, C2: Dim> AreMultipliable<R1, C1, R2, C2> for ShapeConstraint where
13ShapeConstraint: DimEq<C1, R2>
14{
15}
1617/// Constraints `D1` and `D2` to be equivalent.
18pub trait DimEq<D1: Dim, D2: Dim> {
19/// This is either equal to `D1` or `D2`, always choosing the one (if any) which is a type-level
20 /// constant.
21type Representative: Dim;
22}
2324impl<D: Dim> DimEq<D, D> for ShapeConstraint {
25type Representative = D;
26}
2728impl<D: DimName> DimEq<D, Dynamic> for ShapeConstraint {
29type Representative = D;
30}
3132impl<D: DimName> DimEq<Dynamic, D> for ShapeConstraint {
33type Representative = D;
34}
3536macro_rules! equality_trait_decl(
37 ($($doc: expr, $Trait: ident),* $(,)*) => {$(
38// XXX: we can't do something like `DimEq<D1> for D2` because we would require a blancket impl…
39#[doc = $doc]
40pub trait $Trait<D1: Dim, D2: Dim>: DimEq<D1, D2> + DimEq<D2, D1> {
41/// This is either equal to `D1` or `D2`, always choosing the one (if any) which is a type-level
42 /// constant.
43type Representative: Dim;
44 }
4546impl<D: Dim> $Trait<D, D> for ShapeConstraint {
47type Representative = D;
48 }
4950impl<D: DimName> $Trait<D, Dynamic> for ShapeConstraint {
51type Representative = D;
52 }
5354impl<D: DimName> $Trait<Dynamic, D> for ShapeConstraint {
55type Representative = D;
56 }
57 )*}
58);
5960equality_trait_decl!(
61"Constraints `D1` and `D2` to be equivalent. \
62 They are both assumed to be the number of \
63 rows of a matrix.",
64 SameNumberOfRows,
65"Constraints `D1` and `D2` to be equivalent. \
66 They are both assumed to be the number of \
67 columns of a matrix.",
68 SameNumberOfColumns
69);
7071/// Constraints D1 and D2 to be equivalent, where they both designate dimensions of algebraic
72/// entities (e.g. square matrices).
73pub trait SameDimension<D1: Dim, D2: Dim>:
74 SameNumberOfRows<D1, D2> + SameNumberOfColumns<D1, D2>
75{
76/// This is either equal to `D1` or `D2`, always choosing the one (if any) which is a type-level
77 /// constant.
78type Representative: Dim;
79}
8081impl<D: Dim> SameDimension<D, D> for ShapeConstraint {
82type Representative = D;
83}
8485impl<D: DimName> SameDimension<D, Dynamic> for ShapeConstraint {
86type Representative = D;
87}
8889impl<D: DimName> SameDimension<Dynamic, D> for ShapeConstraint {
90type Representative = D;
91}