pub trait Comparator<T> {
// Required method
fn compare(&self, a: &T, b: &T) -> Ordering;
// Provided methods
fn into_box(self) -> BoxComparator<T>
where Self: Sized + 'static,
T: 'static { ... }
fn into_arc(self) -> ArcComparator<T>
where Self: Sized + Send + Sync + 'static,
T: 'static { ... }
fn into_rc(self) -> RcComparator<T>
where Self: Sized + 'static,
T: 'static { ... }
fn into_fn(self) -> impl Fn(&T, &T) -> Ordering
where Self: Sized + 'static,
T: 'static { ... }
}Expand description
A trait for comparison operations.
This trait defines the core comparison operation and conversion methods.
It does NOT include composition methods like reversed or
then_comparing to maintain a clean separation between the trait
interface and specialized implementations.
§Type Parameters
T- The type of values being compared
§Examples
use prism3_function::comparator::{Comparator, BoxComparator};
use std::cmp::Ordering;
let cmp = BoxComparator::new(|a: &i32, b: &i32| a.cmp(b));
assert_eq!(cmp.compare(&5, &3), Ordering::Greater);§Author
Haixing Hu
Required Methods§
Sourcefn compare(&self, a: &T, b: &T) -> Ordering
fn compare(&self, a: &T, b: &T) -> Ordering
Compares two values and returns an ordering.
§Parameters
a- The first value to compareb- The second value to compare
§Returns
An Ordering indicating whether a is less than, equal to, or
greater than b.
§Examples
use prism3_function::comparator::{Comparator, BoxComparator};
use std::cmp::Ordering;
let cmp = BoxComparator::new(|a: &i32, b: &i32| a.cmp(b));
assert_eq!(cmp.compare(&5, &3), Ordering::Greater);
assert_eq!(cmp.compare(&3, &5), Ordering::Less);
assert_eq!(cmp.compare(&5, &5), Ordering::Equal);Provided Methods§
Sourcefn into_box(self) -> BoxComparator<T>where
Self: Sized + 'static,
T: 'static,
fn into_box(self) -> BoxComparator<T>where
Self: Sized + 'static,
T: 'static,
Sourcefn into_arc(self) -> ArcComparator<T>
fn into_arc(self) -> ArcComparator<T>
Sourcefn into_rc(self) -> RcComparator<T>where
Self: Sized + 'static,
T: 'static,
fn into_rc(self) -> RcComparator<T>where
Self: Sized + 'static,
T: 'static,
Sourcefn into_fn(self) -> impl Fn(&T, &T) -> Orderingwhere
Self: Sized + 'static,
T: 'static,
fn into_fn(self) -> impl Fn(&T, &T) -> Orderingwhere
Self: Sized + 'static,
T: 'static,
Converts this comparator into a closure that implements
Fn(&T, &T) -> Ordering.
This method consumes the comparator and returns a closure that can be used anywhere a function or closure is expected.
§Returns
An implementation that can be called as Fn(&T, &T) -> Ordering.
§Examples
use prism3_function::comparator::{Comparator, BoxComparator};
use std::cmp::Ordering;
let cmp = BoxComparator::new(|a: &i32, b: &i32| a.cmp(b));
let func = cmp.into_fn();
assert_eq!(func(&5, &3), Ordering::Greater);Implementors§
impl<T> Comparator<T> for ArcComparator<T>
impl<T> Comparator<T> for BoxComparator<T>
impl<T> Comparator<T> for RcComparator<T>
impl<T, F> Comparator<T> for F
Blanket implementation of Comparator for all closures and function
pointers.
This allows any closure or function with the signature
Fn(&T, &T) -> Ordering to be used as a comparator.
§Examples
use prism3_function::comparator::Comparator;
use std::cmp::Ordering;
let cmp = |a: &i32, b: &i32| a.cmp(b);
assert_eq!(cmp.compare(&5, &3), Ordering::Greater);