Crate ordered

Source
Expand description

Provides a wrapper for types that can technically implement PartialOrd/Ord but for semantic reasons it is nonsensical.

PartialOrd and Ord are often useful and/or required. For example, Ordered allows one to use such a type as a key in a BTreeMap (which requires ordered keys).

For a full example see examples/point.rs.

§Examples

use core::cmp::Ordering;
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))
    }
}

/// `Ordered` allows users to derive `PartialOrd` on types that include a `Point`.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Adt {
    name: String,
    point: Ordered<Point>,
}

Structs§

  • A wrapper type that implements PartialOrd and Ord.

Traits§

  • Trait for types that perform an arbitrary ordering.