ArcComparator

Struct ArcComparator 

Source
pub struct ArcComparator<T> { /* private fields */ }
Expand description

An Arc-based thread-safe comparator with shared ownership.

ArcComparator wraps a comparator function in an Arc, providing thread-safe shared ownership semantics. It is cloneable and uses &self in composition operations.

§Type Parameters

  • T - The type of values being compared

§Examples

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

let cmp = ArcComparator::new(|a: &i32, b: &i32| a.cmp(b));
let cloned = cmp.clone();
assert_eq!(cmp.compare(&5, &3), Ordering::Greater);
assert_eq!(cloned.compare(&5, &3), Ordering::Greater);

§Author

Haixing Hu

Implementations§

Source§

impl<T: 'static> ArcComparator<T>

Source

pub fn new<F>(f: F) -> Self
where F: Fn(&T, &T) -> Ordering + Send + Sync + 'static,

Creates a new ArcComparator from a closure.

§Parameters
  • f - The closure to wrap
§Returns

A new ArcComparator instance.

§Examples
use prism3_function::comparator::ArcComparator;

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

pub fn reversed(&self) -> Self

Returns a comparator that imposes the reverse ordering.

§Returns

A new ArcComparator that reverses the comparison order.

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

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

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

A new ArcComparator that chains this comparator with another.

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

let cmp1 = ArcComparator::new(|a: &i32, b: &i32| {
    (a % 2).cmp(&(b % 2))
});
let cmp2 = ArcComparator::new(|a: &i32, b: &i32| a.cmp(b));
let chained = cmp1.then_comparing(&cmp2);
assert_eq!(chained.compare(&4, &2), Ordering::Greater);
Source

pub fn comparing<K, F>(key_fn: F) -> Self
where K: Ord, F: Fn(&T) -> &K + Send + Sync + 'static,

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 ArcComparator that compares by the extracted key.

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

#[derive(Debug)]
struct Person {
    name: String,
    age: i32,
}

let by_age = ArcComparator::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);
Source

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, ArcComparator};
use std::cmp::Ordering;

let cmp = ArcComparator::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: Clone> Clone for ArcComparator<T>

Source§

fn clone(&self) -> ArcComparator<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

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,

Converts this comparator into a BoxComparator. Read more
Source§

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

Converts this comparator into an ArcComparator. Read more
Source§

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

Converts this comparator into an RcComparator. Read more
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. Read more

Auto Trait Implementations§

§

impl<T> Freeze for ArcComparator<T>

§

impl<T> !RefUnwindSafe for ArcComparator<T>

§

impl<T> Send for ArcComparator<T>

§

impl<T> Sync for ArcComparator<T>

§

impl<T> Unpin for ArcComparator<T>

§

impl<T> !UnwindSafe for ArcComparator<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.