Skip to content

Some total ordering relations can only be expressed as partial orders #2511

@gnzlbg

Description

@gnzlbg

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?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions