Skip to content

Tracking Issue for Comparable #145986

@conradludgate

Description

@conradludgate

Feature gate: #![feature(comparable_trait)]

This is a tracking issue for adding the Comparable trait to core, to be used by BTreeMap.

See the https://docs.rs/equivalent/latest/equivalent/ trait.

The Equivalent and Comparable traits allow for more generalised accessors for BTreeMap or HashMap than what Borrow + Ord/Borrow + Eq can allow today.

Public API

// core::cmp

pub(crate) trait Equivalent<Q: ?Sized> {
    fn equivalent(&self, key: &Q) -> bool;
}

impl<K: ?Sized, Q: ?Sized> Equivalent<Q> for K
where
    K: Borrow<Q>,
    Q: Eq,
{
}

pub(crate) trait Comparable<Q: ?Sized>: Equivalent<Q> {
    fn compare(&self, key: &Q) -> Ordering;
}

impl<K: ?Sized, Q: ?Sized> Comparable<Q> for K
where
    K: Borrow<Q>,
    Q: Ord,
{
}

// alloc::collections::btree
impl<K, V> BTreeMap<K, V> {
    fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        Q: ?Sized,
        K: Comparable<Q>;
}

Steps / History

(Remember to update the S-tracking-* label when checking boxes.)

Unresolved Questions

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-unimplementedStatus: The feature has not been implemented.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions