@@ -117,6 +117,39 @@ pub mod hash_set {
117
117
pub use crate :: map:: HashMap ;
118
118
pub use crate :: set:: HashSet ;
119
119
120
+ /// Key equivalence trait.
121
+ ///
122
+ /// This trait defines the function used to compare the input value with the
123
+ /// map keys (or set values) during a lookup operation sur as [`HashMap::get`]
124
+ /// or [`HashSet::contains`].
125
+ /// It is provided with a blanket implementation based on the
126
+ /// [`Borrow`](core::borrow::Borrow) trait.
127
+ ///
128
+ /// # Correctness
129
+ ///
130
+ /// Equivalent values must hash to the same value.
131
+ pub trait Equivalent < K : ?Sized > {
132
+ /// Checks if this value is equivalent to the given key.
133
+ ///
134
+ /// Returns `true` if oth values are equivalent, and `false` otherwise.
135
+ ///
136
+ /// # Correctness
137
+ ///
138
+ /// When this function returns `true`, both `self` and `key` must hash to
139
+ /// the same value.
140
+ fn equivalent ( & self , key : & K ) -> bool ;
141
+ }
142
+
143
+ impl < Q : ?Sized , K : ?Sized > Equivalent < K > for Q
144
+ where
145
+ Q : Eq ,
146
+ K : core:: borrow:: Borrow < Q > ,
147
+ {
148
+ fn equivalent ( & self , key : & K ) -> bool {
149
+ self == key. borrow ( )
150
+ }
151
+ }
152
+
120
153
/// The error type for `try_reserve` methods.
121
154
#[ derive( Clone , PartialEq , Eq , Debug ) ]
122
155
pub enum TryReserveError {
0 commit comments