#[repr(transparent)]pub struct Ordered<T>(pub T);
Expand description
A wrapper type that implements PartialOrd
and Ord
.
Examples
use core::{cmp::Ordering, fmt};
use ordered::{ArbitraryOrd, Ordered};
/// A point in 2D space.
///
/// We do not want users to be able to write `a < b` because it is not well defined.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Point {
x: u32,
y: u32,
}
impl ArbitraryOrd for Point {
fn arbitrary_cmp(&self, other: &Self) -> Ordering {
// Just use whatever order tuple cmp gives us.
(self.x, self.y).cmp(&(other.x, other.y))
}
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}
let point = Point { x: 0, y: 1 };
let ordered = Ordered(point);
println!("We can explicitly deref (*ordered): {}", *ordered);
println!("Or use deref coercion (ordered): {}", ordered);
println!("Or we can use borrow (&ordered): {}", &ordered);
Tuple Fields§
§0: T
Implementations§
source§impl<T> Ordered<T>
impl<T> Ordered<T>
sourcepub const fn new(inner: T) -> Self
pub const fn new(inner: T) -> Self
Creates a new wrapped ordered type.
The inner type is public so this function is never explicitly needed.
sourcepub const fn as_inner(&self) -> &T
pub const fn as_inner(&self) -> &T
Returns a reference to the inner object.
We also implement core::borrow::Borrow
so this function is never explicitly needed.
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Returns the inner object.
We also implement core::ops::Deref
so this function is never explicitly needed.
sourcepub fn from_ref(value: &T) -> &Self
pub fn from_ref(value: &T) -> &Self
Creates an Ordered<T>
from a reference.
This allows: let found = map.get(Ordered::from_ref(&a));
Examples found in repository?
examples/point.rs (line 20)
9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() {
let mut map = BTreeMap::new();
let a = Point { x: 2, y: 3 };
let b = Point { x: 1, y: 5 };
map.insert(Ordered::from(a), "some interesting value");
map.insert(Ordered::from(b), "some other interesting value");
println!();
println!("Looking in map for key: {}", a);
let found = map.get(Ordered::from_ref(&a)).expect("failed to look up key");
println!("Found it, with value: {}", found);
}
Trait Implementations§
source§impl<T> BorrowMut<T> for Ordered<T>
impl<T> BorrowMut<T> for Ordered<T>
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T: ArbitraryOrd> Ord for Ordered<T>
impl<T: ArbitraryOrd> Ord for Ordered<T>
source§impl<T: PartialEq> PartialEq for Ordered<T>
impl<T: PartialEq> PartialEq for Ordered<T>
source§impl<T: ArbitraryOrd> PartialOrd for Ordered<T>
impl<T: ArbitraryOrd> PartialOrd for Ordered<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moreimpl<T: Copy> Copy for Ordered<T>
impl<T: Eq> Eq for Ordered<T>
impl<T> StructuralEq for Ordered<T>
impl<T> StructuralPartialEq for Ordered<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for Ordered<T>where
T: RefUnwindSafe,
impl<T> Send for Ordered<T>where
T: Send,
impl<T> Sync for Ordered<T>where
T: Sync,
impl<T> Unpin for Ordered<T>where
T: Unpin,
impl<T> UnwindSafe for Ordered<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