FnComparatorOps

Trait FnComparatorOps 

Source
pub trait FnComparatorOps<T>: Fn(&T, &T) -> Ordering + Sized {
    // Provided methods
    fn reversed(self) -> BoxComparator<T>
       where Self: 'static,
             T: 'static { ... }
    fn then_comparing(self, other: BoxComparator<T>) -> BoxComparator<T>
       where Self: 'static,
             T: 'static { ... }
}
Expand description

Extension trait providing composition methods for closures and function pointers.

This trait is automatically implemented for all closures and function pointers with the signature Fn(&T, &T) -> Ordering, allowing them to be composed directly without explicit wrapping.

§Examples

use prism3_function::comparator::{Comparator, FnComparatorOps};
use std::cmp::Ordering;

let cmp = (|a: &i32, b: &i32| a.cmp(b))
    .reversed()
    .then_comparing(BoxComparator::new(|a, b| b.cmp(a)));

assert_eq!(cmp.compare(&5, &3), Ordering::Less);

§Author

Haixing Hu

Provided Methods§

Source

fn reversed(self) -> BoxComparator<T>
where Self: 'static, T: 'static,

Returns a comparator that imposes the reverse ordering.

§Returns

A new BoxComparator that reverses the comparison order.

§Examples
use prism3_function::comparator::{Comparator, FnComparatorOps};
use std::cmp::Ordering;

let rev = (|a: &i32, b: &i32| a.cmp(b)).reversed();
assert_eq!(rev.compare(&5, &3), Ordering::Less);
Source

fn then_comparing(self, other: BoxComparator<T>) -> BoxComparator<T>
where Self: 'static, T: 'static,

Returns a comparator that uses this comparator first, then another comparator if this one considers the values equal.

§Parameters
  • other - The comparator to use for tie-breaking
§Returns

A new BoxComparator that chains this comparator with another.

§Examples
use prism3_function::comparator::{Comparator, FnComparatorOps,
                                  BoxComparator};
use std::cmp::Ordering;

let cmp = (|a: &i32, b: &i32| (a % 2).cmp(&(b % 2)))
    .then_comparing(BoxComparator::new(|a, b| a.cmp(b)));
assert_eq!(cmp.compare(&4, &2), Ordering::Greater);

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> FnComparatorOps<T> for F
where F: Fn(&T, &T) -> Ordering,