//! Compatibility constraints between matrix shapes, e.g., for addition or multiplication. use crate::base::dimension::{Dim, DimName, Dyn}; /// A type used in `where` clauses for enforcing constraints. #[derive(Copy, Clone, Debug)] pub struct ShapeConstraint; /// Constrains `C1` and `R2` to be equivalent. pub trait AreMultipliable: DimEq {} impl AreMultipliable for ShapeConstraint where ShapeConstraint: DimEq { } /// Constrains `D1` and `D2` to be equivalent. pub trait DimEq { /// This is either equal to `D1` or `D2`, always choosing the one (if any) which is a type-level /// constant. type Representative: Dim; } impl DimEq for ShapeConstraint { type Representative = D; } impl DimEq for ShapeConstraint { type Representative = D; } impl DimEq for ShapeConstraint { type Representative = D; } macro_rules! equality_trait_decl( ($($doc: expr, $Trait: ident),* $(,)*) => {$( // XXX: we can't do something like `DimEq for D2` because we would require a blanket impl… #[doc = $doc] pub trait $Trait: DimEq + DimEq { /// This is either equal to `D1` or `D2`, always choosing the one (if any) which is a type-level /// constant. type Representative: Dim; } impl $Trait for ShapeConstraint { type Representative = D; } impl $Trait for ShapeConstraint { type Representative = D; } impl $Trait for ShapeConstraint { type Representative = D; } )*} ); equality_trait_decl!( "Constrains `D1` and `D2` to be equivalent. \ They are both assumed to be the number of \ rows of a matrix.", SameNumberOfRows, "Constrains `D1` and `D2` to be equivalent. \ They are both assumed to be the number of \ columns of a matrix.", SameNumberOfColumns ); /// Constrains D1 and D2 to be equivalent, where they both designate dimensions of algebraic /// entities (e.g. square matrices). pub trait SameDimension: SameNumberOfRows + SameNumberOfColumns { /// This is either equal to `D1` or `D2`, always choosing the one (if any) which is a type-level /// constant. type Representative: Dim; } impl SameDimension for ShapeConstraint { type Representative = D; } impl SameDimension for ShapeConstraint { type Representative = D; } impl SameDimension for ShapeConstraint { type Representative = D; }