predicate-macros

Crates.iopredicate-macros
lib.rspredicate-macros
version0.1.0
sourcesrc
created_at2021-10-19 16:58:25.92143
updated_at2021-10-19 16:58:25.92143
descriptionEasy to implement predicate crate traits
homepagehttps://github.com/Spxg/predicate-macros
repositoryhttps://github.com/Spxg/predicate-macros
max_upload_size
id467426
size7,487
上铺小哥 (Spxg)

documentation

https://docs.rs/predicate-macros

README

predicate-macros

Easy to implement predicate traits.

Macros

  • add_field
  • BitAnd
  • BitOr
  • OpUnitTrait

Example

#[add_field]
#[derive(BitAnd, BitOr, OpUnitTrait)]
enum NumType {
    Odd,
    Even,
    DivByThree,
    DivByFour,
    DivByFive,
    IsMagicNum(i32),
}

expand:

enum NumType {
    Odd,
    Even,
    DivByThree,
    DivByFour,
    DivByFive,
    IsMagicNum(i32),
    Unit(OpUnitInnerType<OpUnit<NumType>>),
}

impl std::ops::BitAnd for NumType {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        let node = OpUnit::new(
            Some(OpUnitInnerType::new(self)),
            Some(OpUnitInnerType::new(rhs)),
            Operation::And,
        );
        NumType::Unit(OpUnitInnerType::new(node))
    }
}

impl std::ops::BitOr for NumType {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        let node = OpUnit::new(
            Some(OpUnitInnerType::new(self)),
            Some(OpUnitInnerType::new(rhs)),
            Operation::Or,
        );
        NumType::Unit(OpUnitInnerType::new(node))
    }
}

impl OpUnitTrait for NumType {
    fn get_op_unit(self: &OpUnitInnerType<Self>) -> OpUnitInnerType<OpUnit<Self>> {
        match self.as_ref() {
            NumType::Unit(unit) => unit.clone(),
            _ => OpUnitInnerType::new(OpUnit::new(Some(self.clone()), None, Operation::Single)),
        }
    }
}
Commit count: 16

cargo fmt