rust-lang / rust-lang/rfcs

Some total ordering relations can only be expressed as partial orders

Open
#2,511 7 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

TL;DR: one cannot impl Ord<Foo> for Bar.

Currently, we can stablish strict and non-strict partial ordering relations between types:

impl PartialEq<Foo> for Bar { ... }
impl PartialOrd<Foo> for Bar { ... }

but there is no way to state that such a relation is a total order because Ord and Eq are not generic over the RHS like PartialOrd and PartialEq are.

Is this an oversight?

I would find this useful when implementing ordering relations for wrapper types for which it makes no sense to implement Deref. For example:

impl PartialOrd<Wrapping<i32>> for i32 { ... } // TODAY OK
impl PartialEq<Wrapping<i32>> for i32 { ... } // TODAY OK

impl Eq<Wrapping<i32>> for i32 { ... }  // TODAY ERROR
impl Ord<Wrapping<i32>> for i32 { ... }  // TODAY ERROR

To solve this we would probably need to change Eq and Ord from:

pub trait Eq: PartialEq<Self> { ... }
pub trait Ord: Eq + PartialOrd<Self> { ... }

to

pub trait Eq<RHS=Self>: PartialEq<RHS> { ... }
pub trait Ord<RHS=Self>: Eq<RHS> + PartialOrd<RHS> { ... }

Would this be a breaking change? (if so, could it be fixed automatically by rustfix?)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the issue's proposed changes to Rust's Eq, Ord, PartialEq, and PartialOrd traits, including the wrapper-type examples. Determine whether making Eq and Ord generic would be breaking and whether rustfix could automate compatibility changes. Done means documenting a resolved design direction or turning the proposal into an RFC-ready change.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.