Skip to content

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

Open

Description

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

Basically anything that has Ord already.

Activity

  1. tcharding commented on Jan 30, 2025

    @tcharding
    Member

    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>>,
    }
  2. Kixunil commented on Jan 31, 2025

    @Kixunil
    Author

    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>.

  3. tcharding commented on Feb 3, 2025

    @tcharding
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions