ospf_rust_math/algebra/concept/
variant.rs1use crate::SemiArithmetic;
2
3pub trait Variant: SemiArithmetic {
4 type ValueType: SemiArithmetic;
5
6 fn value(&self) -> Option<&Self::ValueType> {
7 None
8 }
9 fn equiv(&self, rhs: &Self::ValueType) -> bool;
10}
11
12pub fn equiv<Lhs: Variant>(lhs: &Lhs, rhs: &Lhs::ValueType) -> bool {
13 lhs.equiv(rhs)
14}
15
16pub trait Invariant: SemiArithmetic {
17 type ValueType: SemiArithmetic;
18
19 fn from(value: &Self::ValueType) -> Self;
20 fn value(&self) -> &Self::ValueType;
21}
22
23macro_rules! invariant_template {
24 ($($type:ident)*) => ($(
25 impl Invariant for $type {
26 type ValueType = Self;
27
28 fn from(value: &Self::ValueType) -> Self {
29 value.clone()
30 }
31
32 fn value(&self) -> &Self::ValueType {
33 &self
34 }
35 }
36 )*)
37}
38invariant_template! { bool i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize f32 f64 }