scc/
equivalent.rs

1//! Vendors the [`equivalent`](https://crates.io/crates/equivalent) crate to avoid conflicts.
2
3use std::borrow::Borrow;
4use std::cmp::Ordering;
5
6/// Key equivalence trait.
7///
8/// [`Hash`](std::hash::Hash) must be implemented to ensure that the same hash value
9/// is generated for equivalent keys.
10pub trait Equivalent<K: ?Sized> {
11    /// Compares `self` with `key` and returns `true` if they are equal.
12    fn equivalent(&self, key: &K) -> bool;
13}
14
15impl<Q: ?Sized, K: ?Sized> Equivalent<K> for Q
16where
17    Q: Eq,
18    K: Borrow<Q>,
19{
20    #[inline]
21    fn equivalent(&self, key: &K) -> bool {
22        PartialEq::eq(self, key.borrow())
23    }
24}
25
26/// Key ordering trait.
27pub trait Comparable<K: ?Sized>: Equivalent<K> {
28    /// Compares `self` with `key` and returns their ordering.
29    fn compare(&self, key: &K) -> Ordering;
30}
31
32impl<Q: ?Sized, K: ?Sized> Comparable<K> for Q
33where
34    Q: Ord,
35    K: Borrow<Q>,
36{
37    #[inline]
38    fn compare(&self, key: &K) -> Ordering {
39        Ord::cmp(self, key.borrow())
40    }
41}