Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl ArbitraryOrd for std smart pointers and various generic types #37

Open
Kixunil opened this issue Jan 30, 2025 · 3 comments
Open

Impl ArbitraryOrd for std smart pointers and various generic types #37

Kixunil opened this issue Jan 30, 2025 · 3 comments

Comments

@Kixunil
Copy link

Kixunil commented Jan 30, 2025

  • Box
  • Rc
  • Arc
  • [T]
  • Vec
  • Option
  • Result
  • ManuallyDrop

Basically anything that has Ord already.

@tcharding
Copy link
Member

tcharding commented Jan 30, 2025

What is the use case? I tried to think it through for Vec but I got to this code that builds already.

/// 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 { (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) }
}

/// A type that derives `Ord` and friends.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Adt {
    // point: Point,  // Doesn't build, this is the reason we provide `Ordered`.
    point: Ordered<Point>,
    // v: Vec<Point>, // Also doesn't build but we can wrap in `Ordered` again.
    v: Vec<Ordered<Point>>,
}

@Kixunil
Copy link
Author

Kixunil commented Jan 31, 2025

The use case is being able to write things like BTreeMap<Ordered<Box<HugeStructWithArbitrarryOrd>>> etc. Generally, crates implement their traits for all sensible std types. For Vec, dealing with Ordered<Vec<Point>> is very likely less annoying than Vec<Ordered<Point>.

@tcharding
Copy link
Member

I'm cutting the 1.0-alpha release but flagging that I intend on doing this before 1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants