Use enum to predicate something, support | and & operator.

Overview

Predicate

crates.io version

Use enum to predicate something.

Just need to implement Predicate Trait with predicate-macros crate, support | and & operator.

Don't implement traits by self.

How to work

how_to_work

Feature

  • rc (default)
  • arc

Enable Arc Feature:

[dependencies.predicate]
version = "0.1"
default-features = false
features = ["arc"]

Example

#[add_field]
#[derive(BitAnd, BitOr, OpUnitTrait)]
enum NumType {
    Odd,
    Even,
    DivByThree,
    DivByFour,
    DivByFive,
    IsMagicNum(i32),
    More(Box<dyn Fn(&i32) -> bool>),
}

impl Predicate for NumType {
    type Item = i32;

    fn rules(&self, item: &Self::Item) -> bool {
        match self {
            NumType::Odd => item % 2 != 0,
            NumType::Even => item % 2 == 0,
            NumType::DivByThree => item % 3 == 0,
            NumType::DivByFour => item % 4 == 0,
            NumType::DivByFive => item % 5 == 0,
            NumType::IsMagicNum(num) => item == num,
            NumType::More(f) => f(item),
            _ => false,
        }
    }
}

fn main() {
    let nums = vec![1, 2, 3, 4, 5, 6, 9, 12, 15, 16, 20, 22, 24, 1024];
    let test = NumType::Odd
        | NumType::Even & NumType::DivByThree & NumType::DivByFour
        | NumType::DivByFive;
    let result = nums
        .clone()
        .into_iter()
        .filter(test.wrap_ret().predicate_ref_one())
        .collect::<Vec<_>>();
    assert_eq!(vec![1, 3, 5, 9, 12, 15, 20, 24], result);

    let test = NumType::Odd & NumType::Even;
    let result = nums
        .clone()
        .into_iter()
        .filter(test.wrap_ret().predicate_ref_one())
        .collect::<Vec<_>>();
    assert!(result.is_empty());

    let test = NumType::Odd | NumType::Even;
    let result = nums
        .clone()
        .into_iter()
        .filter(test.wrap_ret().predicate_ref_one())
        .collect::<Vec<_>>();
    assert_eq!(result, nums);

    let test = NumType::DivByThree & NumType::Odd | NumType::DivByFive;
    let result = nums
        .clone()
        .iter()
        .filter(test.wrap_ret().predicate_ref_double())
        .map(|num| *num)
        .collect::<Vec<_>>();
    assert_eq!(vec![3, 5, 9, 15, 20], result);

    let test = NumType::More(Box::new(|i| i % 6 == 0));
    let ret = test.wrap_ret();
    let result = nums
        .clone()
        .into_iter()
        .filter(ret.predicate_ref_one())
        .collect::<Vec<_>>();
    assert_eq!(vec![6, 12, 24], result);
    assert!(ret.predicate_self()(36));

    let test = NumType::IsMagicNum(1024);
    assert!(test.wrap_ret().predicate_self()(1024));
}
You might also like...
MeiliSearch is a powerful, fast, open-source, easy to use and deploy search engine
MeiliSearch is a powerful, fast, open-source, easy to use and deploy search engine

MeiliSearch is a powerful, fast, open-source, easy to use and deploy search engine. Both searching and indexing are highly customizable. Features such as typo-tolerance, filters, and synonyms are provided out-of-the-box. For more information about features go to our documentation.

A minimal RedDSA implementation for use in Zebra and zcashd.

A minimal RedDSA implementation for use in Zcash. Two specializations of RedDSA are used in Zcash: RedJubjub and RedPallas. For each of these, two par

Use explicit container types with Scrypto! Leverage the Rust compiler's type checking to increase security and productivity when developing Radix blueprints.

Scrypto Static Types Use explicit container types with Scrypto! Leverage the Rust compiler's type checking to increase security and productivity when

A high-level Rust crate around the Discord API, aimed to be easy and straight-forward to use.
A high-level Rust crate around the Discord API, aimed to be easy and straight-forward to use.

rs-cord A high-level Rust crate around the Discord API, aimed to be easy and straight-forward to use. Documentation • Crates.io • Discord Navigation M

Easy-to-use optional function arguments for Rust

OptArgs uses const generics to ensure compile-time correctness. I've taken the liberty of expanding and humanizing the macros in the reference examples.

very cool esoteric language pls use

okfrick has one memory pointer has less than 5 characters hopefully works well is turing complete (possibly) + - increase memeory pointer value ( - st

 Achieve it! How you ask? Well, it's pretty simple; just use greatness!
Achieve it! How you ask? Well, it's pretty simple; just use greatness!

Greatness! Achieve it! How you ask? Well, it's pretty simple; just use greatness! Disclaimer I do not believe that greatness is the best. It fits a me

use variant nesting information to flter overlapping sites from vg deconstruct output

vcfbub popping bubbles in vg deconstruct VCFs overview The VCF output produced by a command like vg deconstruct -e -a -H '#' ... includes information

Here are a few cargo-generate templates for use when creating bevy applications
Here are a few cargo-generate templates for use when creating bevy applications

Bevy-template-rs Here are a few cargo-generate templates for use when creating bevy applications. Templates Game This is a template for starting a new

Owner
上铺小哥
在学习中实践,实践中学习。
上铺小哥
Ointers is a library for representing pointers where some bits have been stolen so that they may be used by the programmer for something else

Ointers is a library for representing pointers where some bits have been stolen so that they may be used by the programmer for something else. In effect, it's a small amount of free storage

Irrustible 8 Jun 4, 2022
Jonathan Kelley 33 Dec 6, 2022
Single-operator public liveness notification

Single-operator public liveness notification

Charlotte Som 1 Feb 16, 2022
RustSBI support on SiFive FU740 board; FU740 is a five-core heterogeneous processor with four SiFive U74 cores, and one SiFive S7 core

RustSBI 在 HiFive Unmatched 主板的支持软件 这个项目的目的是在SiFive HiFive Unmatched主板上支持RustSBI。 RustSBI是一个引导程序环境;主板上电时,RustSBI将会先行启动,而后,它将会找到一个可引导的操作系统,引导启动这个操作系统。 在

RustSBI 15 Dec 1, 2022
Simple library to host lv2 plugins. Is not meant to support any kind of GUI.

lv2-host-minimal Simple library to host lv2 plugins. Is not meant to support any kind of GUI. Host fx plugins (audio in, audio out) Set parameters Hos

Cody Bloemhard 11 Aug 31, 2022
A simple entity-component-system crate for rust with serialization support

Gallium A simple entity-component-system crate for rust with serialization support Usage You can include the library using carge: [dependencies] galli

null 7 Aug 31, 2021
Serde support for (rusty_)v8

serde_v8 Serde support for (rusty_)v8 WIP: see denoland/deno#9540 TODO Experiment with KeyCache to optimize struct keys Experiment with external v8 st

Aaron O'Mullan 13 Nov 28, 2022
3MF (3D Manufacturing Format) support for Rust

3MF (3D Manufacturing Format) support for Rust About This library provides support for 3MF files to programs written in the Rust programming language.

Hanno Braun 21 Dec 17, 2022
A (mostly) drop-in replacement for Rust's Result that provides backtrace support

Errant A (mostly) drop-in replacement for Rust's Result that provides backtrace support. Please note that Errant is still very early in development an

Joshua Barretto 17 Dec 26, 2022
Add toast support in your dioxus project

Add toast support in your dioxus project

YuKun Liu 8 Dec 16, 2022