pub struct OrdContext<T> {
pub compare: fn(&T, &T) -> Ordering,
}Expand description
A context providing a custom comparison function.
When used with WithContext, implements PartialEq, Eq,
PartialOrd, and Ord by dispatching through the stored comparator.
§Examples
use context_trait::{WithContext, OrdContext};
// Sort in reverse order
let ctx = OrdContext { compare: |a: &i32, b: &i32| b.cmp(a) };
let mut items: Vec<_> = [3, 1, 2].iter()
.map(|&v| WithContext { inner: v, ctx })
.collect();
items.sort();
let values: Vec<i32> = items.into_iter().map(|w| w.inner).collect();
assert_eq!(values, vec![3, 2, 1]);Fields§
§compare: fn(&T, &T) -> OrderingThe comparison function.
Trait Implementations§
Source§impl<T> Clone for OrdContext<T>
impl<T> Clone for OrdContext<T>
Source§fn clone(&self) -> OrdContext<T>
fn clone(&self) -> OrdContext<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T> Debug for OrdContext<T>
impl<T> Debug for OrdContext<T>
impl<T> Copy for OrdContext<T>
Auto Trait Implementations§
impl<T> Freeze for OrdContext<T>
impl<T> RefUnwindSafe for OrdContext<T>
impl<T> Send for OrdContext<T>
impl<T> Sync for OrdContext<T>
impl<T> Unpin for OrdContext<T>
impl<T> UnsafeUnpin for OrdContext<T>
impl<T> UnwindSafe for OrdContext<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