Comparator

Trait Comparator 

Source
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§

Source

fn compare(&self, a: &T, b: &T) -> Ordering

Compares two values and returns an ordering.

§Parameters
  • a - The first value to compare
  • b - 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§

Source

fn into_box(self) -> BoxComparator<T>
where Self: Sized + 'static, T: 'static,

Converts this comparator into a BoxComparator.

§Returns

A new BoxComparator wrapping this comparator.

§Examples
use prism3_function::comparator::{Comparator, BoxComparator};

let cmp = BoxComparator::new(|a: &i32, b: &i32| a.cmp(b));
let boxed = cmp.into_box();
Source

fn into_arc(self) -> ArcComparator<T>
where Self: Sized + Send + Sync + 'static, T: 'static,

Converts this comparator into an ArcComparator.

§Returns

A new ArcComparator wrapping this comparator.

§Examples
use prism3_function::comparator::{Comparator, ArcComparator};

let cmp = ArcComparator::new(|a: &i32, b: &i32| a.cmp(b));
let arc = cmp.into_arc();
Source

fn into_rc(self) -> RcComparator<T>
where Self: Sized + 'static, T: 'static,

Converts this comparator into an RcComparator.

§Returns

A new RcComparator wrapping this comparator.

§Examples
use prism3_function::comparator::{Comparator, RcComparator};

let cmp = RcComparator::new(|a: &i32, b: &i32| a.cmp(b));
let rc = cmp.into_rc();
Source

fn into_fn(self) -> impl Fn(&T, &T) -> Ordering
where 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§

Source§

impl<T> Comparator<T> for ArcComparator<T>

Source§

impl<T> Comparator<T> for BoxComparator<T>

Source§

impl<T> Comparator<T> for RcComparator<T>

Source§

impl<T, F> Comparator<T> for F
where F: Fn(&T, &T) -> Ordering,

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);