scc/
equivalent.rs

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