External Publication
Visit Post

Named impl with Implementation Selection Variant

Rust Internals [Unofficial] June 8, 2026
Source

Your last example could just forbid the cast from SortedList<(i32, i32)> to SortedList<(i32, i32) + PartialOrd use SortBy1> and not the named implementation SortBy1 itself. After all, PartialOrd and PartialOrd use SortBy1 are different traits in a single namespace, which happen to share one interface.

That makes me think that for the first half no compiler changes are needed at all: just make your traits generic. Using it ergonomically (i.e. type aliases) is another problem, though, and should depend on current codebase patterns.

pub enum StandardDef {}
pub enum RevStandardDef {}

pub trait PartialOrd<Rhs=Self, OrderingKind=StandardDef> {
    fn partial_cmp(self, rhs: Rhs) -> Option<Ordering>;
}

impl<Lhs, Rhs> PartialOrd<Lhs, RevStandardDef> for Rhs
where
    Lhs: PartialOrd<Rhs, StandardDef>
{
    fn partial_cmp(self, other: Lhs) -> Option<Ordering> {
        <Lhs as PartialOrd<Rhs, StandardDef>>::partial_cmp(other, self)
    }
}

// Or another implementation option doing approximately the same:
/*
impl<Lhs, Rhs> PartialOrd<Rhs, RevStandardDef> for Lhs
where
    Lhs: PartialOrd<Rhs, StandardDef>
{
    fn partial_cmp(self, rhs: Rhs) -> Option<Ordering> {
        <Lhs as PartialOrd<Rhs, StandardDef>>::partial_cmp(self, other)
            .map(|order| order.flip())
    }
}
*/

Discussion in the ATmosphere

Loading comments...