Skip to content

Spxg/predicate-macros

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

predicate-macros

crates.io version

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(OpUnitRcType<OpUnit<NumType>>),
}

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

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

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

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

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

About

Easy to implement predicate traits.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages