FnPredicateOps

Trait FnPredicateOps 

Source
pub trait FnPredicateOps<T>:
    Fn(&T) -> bool
    + Sized
    + 'static {
    // Provided methods
    fn and<P>(self, other: P) -> BoxPredicate<T>
       where P: Predicate<T> + 'static,
             T: 'static { ... }
    fn or<P>(self, other: P) -> BoxPredicate<T>
       where P: Predicate<T> + 'static,
             T: 'static { ... }
    fn not(self) -> BoxPredicate<T>
       where T: 'static { ... }
    fn nand<P>(self, other: P) -> BoxPredicate<T>
       where P: Predicate<T> + 'static,
             T: 'static { ... }
    fn xor<P>(self, other: P) -> BoxPredicate<T>
       where P: Predicate<T> + 'static,
             T: 'static { ... }
    fn nor<P>(self, other: P) -> BoxPredicate<T>
       where P: Predicate<T> + 'static,
             T: 'static { ... }
}
Expand description

Extension trait providing logical composition methods for closures.

This trait is automatically implemented for all closures and function pointers that match Fn(&T) -> bool, enabling method chaining starting from a closure.

§Examples

use prism3_function::predicate::{Predicate, FnPredicateOps};

let is_positive = |x: &i32| *x > 0;
let is_even = |x: &i32| x % 2 == 0;

// Combine predicates using extension methods
let pred = is_positive.and(is_even);
assert!(pred.test(&4));
assert!(!pred.test(&3));

§Author

Haixing Hu

Provided Methods§

Source

fn and<P>(self, other: P) -> BoxPredicate<T>
where P: Predicate<T> + 'static, T: 'static,

Returns a predicate that represents the logical AND of this predicate and another.

§Parameters
  • other - The other predicate to combine with. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original predicate, clone it first (if it implements Clone). Can be:
    • Another closure
    • A function pointer
    • A BoxPredicate<T>, RcPredicate<T>, or ArcPredicate<T>
§Returns

A BoxPredicate representing the logical AND.

§Examples
use prism3_function::predicate::{Predicate, FnPredicateOps};

let is_positive = |x: &i32| *x > 0;
let is_even = |x: &i32| x % 2 == 0;

let combined = is_positive.and(is_even);
assert!(combined.test(&4));
assert!(!combined.test(&3));
Source

fn or<P>(self, other: P) -> BoxPredicate<T>
where P: Predicate<T> + 'static, T: 'static,

Returns a predicate that represents the logical OR of this predicate and another.

§Parameters
  • other - The other predicate to combine with. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original predicate, clone it first (if it implements Clone). Can be:
    • Another closure
    • A function pointer
    • A BoxPredicate<T>, RcPredicate<T>, or ArcPredicate<T>
    • Any type implementing Predicate<T>
§Returns

A BoxPredicate representing the logical OR.

§Examples
use prism3_function::predicate::{Predicate, FnPredicateOps};

let is_negative = |x: &i32| *x < 0;
let is_large = |x: &i32| *x > 100;

let combined = is_negative.or(is_large);
assert!(combined.test(&-5));
assert!(combined.test(&150));
assert!(!combined.test(&50));
Source

fn not(self) -> BoxPredicate<T>
where T: 'static,

Returns a predicate that represents the logical negation of this predicate.

§Returns

A BoxPredicate representing the logical negation.

Source

fn nand<P>(self, other: P) -> BoxPredicate<T>
where P: Predicate<T> + 'static, T: 'static,

Returns a predicate that represents the logical NAND (NOT AND) of this predicate and another.

NAND returns true unless both predicates are true. Equivalent to !(self AND other).

§Parameters
  • other - The other predicate to combine with. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original predicate, clone it first (if it implements Clone). Accepts closures, function pointers, or any Predicate<T> implementation.
§Returns

A BoxPredicate representing the logical NAND.

§Examples
use prism3_function::predicate::{Predicate, FnPredicateOps};

let is_positive = |x: &i32| *x > 0;
let is_even = |x: &i32| x % 2 == 0;

let nand = is_positive.nand(is_even);
assert!(nand.test(&3));   // !(true && false) = true
assert!(!nand.test(&4));  // !(true && true) = false
Source

fn xor<P>(self, other: P) -> BoxPredicate<T>
where P: Predicate<T> + 'static, T: 'static,

Returns a predicate that represents the logical XOR (exclusive OR) of this predicate and another.

XOR returns true if exactly one of the predicates is true.

§Parameters
  • other - The other predicate to combine with. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original predicate, clone it first (if it implements Clone). Accepts closures, function pointers, or any Predicate<T> implementation.
§Returns

A BoxPredicate representing the logical XOR.

§Examples
use prism3_function::predicate::{Predicate, FnPredicateOps};

let is_positive = |x: &i32| *x > 0;
let is_even = |x: &i32| x % 2 == 0;

let xor = is_positive.xor(is_even);
assert!(xor.test(&3));    // true ^ false = true
assert!(!xor.test(&4));   // true ^ true = false
assert!(!xor.test(&-1));  // false ^ false = false
Source

fn nor<P>(self, other: P) -> BoxPredicate<T>
where P: Predicate<T> + 'static, T: 'static,

Returns a predicate that represents the logical NOR (NOT OR) of this predicate and another.

NOR returns true only when both predicates are false. Equivalent to !(self OR other).

§Parameters
  • other - The other predicate to combine with. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original predicate, clone it first (if it implements Clone). Accepts closures, function pointers, or any Predicate<T> implementation.
§Returns

A BoxPredicate representing the logical NOR.

§Examples
use prism3_function::predicate::{Predicate, FnPredicateOps};

let is_positive = |x: &i32| *x > 0;
let is_even = |x: &i32| x % 2 == 0;

let nor = is_positive.nor(is_even);
assert!(nor.test(&-3));   // !(false || false) = true
assert!(!nor.test(&4));   // !(true || true) = false
assert!(!nor.test(&3));   // !(true || false) = false

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T, F> FnPredicateOps<T> for F
where F: Fn(&T) -> bool + 'static,