#[repr(transparent)]pub struct NoOrder<T>(pub T);Expand description
A helper struct for no ordering, where every instance of NoOrder
This struct is a helper to be used with data structures like std::collections::BinaryHeap or std::collections::HashSet,
and can be used to ignore ordering or hashing of part of a key.
Can be used for faster performance or when an key does not implement std::cmp::Ord or std::hash::Hash,
and is not relavant.
§Examples
use no_order::NoOrder;
use std::collections::BinaryHeap;
use std::collections::HashSet;
// Any NoOrder is equivalent to any other.
assert_eq!(NoOrder(1), NoOrder(2));
assert_eq!(NoOrder(1), NoOrder(1));
// This means that they also share the same key.
let set = HashSet::from([NoOrder(1), NoOrder(1), NoOrder(2), NoOrder(3)]);
assert_eq!(set.len(), 1);
// And can be used to ignore the the second value in a tuple.
let set_tuple = HashSet::from([(1, NoOrder(1)), (2, NoOrder(1)), (2, NoOrder(2))]);
assert_eq!(set_tuple.len(), 2);
// When used in a heap or tree like data structure, we can store extra data without sorting on it.
// This means that we can use types that do not traditionally implement Ord.
let mut heap = BinaryHeap::new();
heap.push((2, NoOrder(1.24)));
heap.push((1, NoOrder(2.15)));
heap.push((2, NoOrder(3.74)));
heap.push((2, NoOrder(2.96)));
// It only acts as a max heap on the first value,
// and the ordering on the second value is undefiend.
while let Some((a, NoOrder(b))) = heap.pop() {
println!("{a}, {b}");
}Tuple Fields§
§0: TTrait Implementations§
Source§impl<T> Ord for NoOrder<T>
impl<T> Ord for NoOrder<T>
Source§impl<T> PartialOrd for NoOrder<T>
impl<T> PartialOrd for NoOrder<T>
impl<T: Copy> Copy for NoOrder<T>
impl<T> Eq for NoOrder<T>
Auto Trait Implementations§
impl<T> Freeze for NoOrder<T>where
T: Freeze,
impl<T> RefUnwindSafe for NoOrder<T>where
T: RefUnwindSafe,
impl<T> Send for NoOrder<T>where
T: Send,
impl<T> Sync for NoOrder<T>where
T: Sync,
impl<T> Unpin for NoOrder<T>where
T: Unpin,
impl<T> UnwindSafe for NoOrder<T>where
T: UnwindSafe,
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