pub struct BoxComparator<T> { /* private fields */ }Expand description
A boxed comparator with single ownership.
BoxComparator wraps a comparator function in a Box, providing single
ownership semantics. It is not cloneable and consumes self in
composition operations.
§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
Implementations§
Source§impl<T: 'static> BoxComparator<T>
impl<T: 'static> BoxComparator<T>
Sourcepub fn reversed(self) -> Self
pub fn reversed(self) -> Self
Returns a comparator that imposes the reverse ordering.
§Returns
A new BoxComparator that reverses the comparison order.
§Examples
use prism3_function::comparator::{Comparator, BoxComparator};
use std::cmp::Ordering;
let cmp = BoxComparator::new(|a: &i32, b: &i32| a.cmp(b));
let rev = cmp.reversed();
assert_eq!(rev.compare(&5, &3), Ordering::Less);Sourcepub fn then_comparing(self, other: Self) -> Self
pub fn then_comparing(self, other: Self) -> Self
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. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original comparator, clone it first (if it implementsClone). Can be:- A
BoxComparator<T> - An
RcComparator<T> - An
ArcComparator<T> - Any type implementing
Comparator<T>
- A
§Returns
A new BoxComparator that chains this comparator with another.
§Examples
use prism3_function::comparator::{Comparator, BoxComparator};
use std::cmp::Ordering;
#[derive(Debug)]
struct Person {
name: String,
age: i32,
}
let by_name = BoxComparator::new(|a: &Person, b: &Person| {
a.name.cmp(&b.name)
});
let by_age = BoxComparator::new(|a: &Person, b: &Person| {
a.age.cmp(&b.age)
});
// by_age is moved here
let cmp = by_name.then_comparing(by_age);
let p1 = Person { name: "Alice".to_string(), age: 30 };
let p2 = Person { name: "Alice".to_string(), age: 25 };
assert_eq!(cmp.compare(&p1, &p2), Ordering::Greater);
// by_age.compare(&p1, &p2); // Would not compile - movedSourcepub fn comparing<K, F>(key_fn: F) -> Self
pub fn comparing<K, F>(key_fn: F) -> Self
Returns a comparator that compares values by a key extracted by the given function.
§Parameters
key_fn- A function that extracts a comparable key from values
§Returns
A new BoxComparator that compares by the extracted key.
§Examples
use prism3_function::comparator::{Comparator, BoxComparator};
use std::cmp::Ordering;
#[derive(Debug)]
struct Person {
name: String,
age: i32,
}
let by_age = BoxComparator::comparing(|p: &Person| &p.age);
let p1 = Person { name: "Alice".to_string(), age: 30 };
let p2 = Person { name: "Bob".to_string(), age: 25 };
assert_eq!(by_age.compare(&p1, &p2), Ordering::Greater);Sourcepub fn into_fn(self) -> impl Fn(&T, &T) -> Ordering
pub fn into_fn(self) -> impl Fn(&T, &T) -> Ordering
Converts this comparator into a closure.
§Returns
A closure that implements 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);Trait Implementations§
Source§impl<T> Comparator<T> for BoxComparator<T>
impl<T> Comparator<T> for BoxComparator<T>
Source§fn compare(&self, a: &T, b: &T) -> Ordering
fn compare(&self, a: &T, b: &T) -> Ordering
Compares two values and returns an ordering. Read more
Source§fn into_box(self) -> BoxComparator<T>where
Self: Sized + 'static,
T: 'static,
fn into_box(self) -> BoxComparator<T>where
Self: Sized + 'static,
T: 'static,
Converts this comparator into a
BoxComparator. Read moreSource§fn into_arc(self) -> ArcComparator<T>
fn into_arc(self) -> ArcComparator<T>
Converts this comparator into an
ArcComparator. Read moreAuto Trait Implementations§
impl<T> Freeze for BoxComparator<T>
impl<T> !RefUnwindSafe for BoxComparator<T>
impl<T> !Send for BoxComparator<T>
impl<T> !Sync for BoxComparator<T>
impl<T> Unpin for BoxComparator<T>
impl<T> !UnwindSafe for BoxComparator<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more