Skip to content

Commit

Permalink
feat: use and reexport indexmap::Equivalent trait
Browse files Browse the repository at this point in the history
Allow get_ methods of `index_map::FrozenIndexMap` and `index_set::FrozenIndexSet` to accept values that implement `Equivalent<K>` or `Equivalent<T>` as keys.
  • Loading branch information
yegorvk committed Jul 16, 2024
1 parent 07bec53 commit 1ae1448
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 38 deletions.
49 changes: 19 additions & 30 deletions src/index_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::borrow::Borrow;
use std::cell::{Cell, UnsafeCell};
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash};
Expand Down Expand Up @@ -43,10 +42,6 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
///
/// Existing values are never overwritten.
///
/// The key may be any borrowed form of the map's key type, but
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
/// the key type.
///
/// # Example
/// ```
/// use elsa::index_map::FrozenIndexMap;
Expand Down Expand Up @@ -74,10 +69,6 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
///
/// Existing values are never overwritten.
///
/// The key may be any borrowed form of the map's key type, but
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
/// the key type.
///
/// # Example
/// ```
/// use elsa::index_map::FrozenIndexMap;
Expand All @@ -99,10 +90,9 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
}

/// Returns a reference to the value corresponding to the key.
///
/// The key may be any borrowed form of the map's key type, but
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
/// the key type.
///
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<K>`].
///
/// # Examples
///
Expand All @@ -114,10 +104,9 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
/// assert_eq!(map.get(&1), Some(&"a"));
/// assert_eq!(map.get(&2), None);
/// ```
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V::Target>
pub fn get<Q>(&self, k: &Q) -> Option<&V::Target>
where
K: Borrow<Q>,
Q: Hash + Eq,
Q: ?Sized + Hash + Equivalent<K>,
{
assert!(!self.in_use.get());
self.in_use.set(true);
Expand All @@ -130,6 +119,9 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
}

/// Returns the index corresponding to the key
///
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<K>`].
///
/// # Examples
///
Expand Down Expand Up @@ -157,10 +149,6 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {

/// Returns a reference to the key-value mapping corresponding to an index.
///
/// The key may be any borrowed form of the map's key type, but
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
/// the key type.
///
/// # Examples
///
/// ```
Expand All @@ -187,6 +175,9 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
}

/// Returns a reference to the key, along with its index and a reference to its value
///
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<K>`].
///
/// # Examples
///
Expand Down Expand Up @@ -215,9 +206,8 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {

/// Applies a function to the owner of the value corresponding to the key (if any).
///
/// The key may be any borrowed form of the map's key type, but
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
/// the key type.
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<K>`].
///
/// # Examples
///
Expand All @@ -229,10 +219,9 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
/// assert_eq!(map.map_get(&1, Clone::clone), Some(Box::new("a")));
/// assert_eq!(map.map_get(&2, Clone::clone), None);
/// ```
pub fn map_get<Q: ?Sized, T, F>(&self, k: &Q, f: F) -> Option<T>
pub fn map_get<Q, T, F>(&self, k: &Q, f: F) -> Option<T>
where
K: Borrow<Q>,
Q: Hash + Eq,
Q: ?Sized + Hash + Equivalent<K>,
F: FnOnce(&V) -> T,
{
assert!(!self.in_use.get());
Expand Down Expand Up @@ -301,10 +290,10 @@ impl<K, V, S> From<IndexMap<K, V, S>> for FrozenIndexMap<K, V, S> {
}
}

impl<Q: ?Sized, K: Eq + Hash, V: StableDeref, S: BuildHasher> Index<&Q> for FrozenIndexMap<K, V, S>
impl<Q, K, V, S> Index<&Q> for FrozenIndexMap<K, V, S>
where
Q: Eq + Hash,
K: Eq + Hash + Borrow<Q>,
Q: ?Sized + Hash + Equivalent<K>,
K: Eq + Hash,
V: StableDeref,
S: BuildHasher,
{
Expand All @@ -320,7 +309,7 @@ where
/// assert_eq!(map[&1], "a");
/// ```
fn index(&self, idx: &Q) -> &V::Target {
self.get(&idx)
self.get(idx)
.expect("attempted to index FrozenIndexMap with unknown key")
}
}
Expand Down
22 changes: 14 additions & 8 deletions src/index_set.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::borrow::Borrow;
use std::cell::{Cell, UnsafeCell};
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash};
Expand Down Expand Up @@ -118,6 +117,9 @@ impl<T: Eq + Hash + StableDeref, S: BuildHasher> FrozenIndexSet<T, S> {
// }

/// Returns a reference to the value passed as argument if present in the set.
///
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<T>`].
///
/// # Examples
///
Expand All @@ -129,10 +131,9 @@ impl<T: Eq + Hash + StableDeref, S: BuildHasher> FrozenIndexSet<T, S> {
/// assert_eq!(set.get(&Box::new("a")), Some(&"a"));
/// assert_eq!(set.get(&Box::new("b")), None);
/// ```
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&T::Target>
pub fn get<Q>(&self, k: &Q) -> Option<&T::Target>
where
T: Borrow<Q>,
Q: Hash + Eq,
Q: ?Sized + Hash + Equivalent<T>,
{
assert!(!self.in_use.get());
self.in_use.set(true);
Expand All @@ -145,7 +146,10 @@ impl<T: Eq + Hash + StableDeref, S: BuildHasher> FrozenIndexSet<T, S> {
}

/// Returns the index corresponding to the value if present in the set
///
///
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<T>`].
///
/// # Examples
///
/// ```
Expand All @@ -172,6 +176,9 @@ impl<T: Eq + Hash + StableDeref, S: BuildHasher> FrozenIndexSet<T, S> {

/// Returns a reference to the value passed as argument if present in the set,
/// along with its index
///
/// # Arguments
/// * `k` may be any type that implements [`Equivalent<T>`].
///
/// # Examples
///
Expand All @@ -183,10 +190,9 @@ impl<T: Eq + Hash + StableDeref, S: BuildHasher> FrozenIndexSet<T, S> {
/// assert_eq!(set.get_full(&Box::new("a")), Some((0, &"a")));
/// assert_eq!(set.get_full(&Box::new("b")), None);
/// ```
pub fn get_full<Q: ?Sized>(&self, k: &Q) -> Option<(usize, &T::Target)>
pub fn get_full<Q>(&self, k: &Q) -> Option<(usize, &T::Target)>
where
T: Borrow<Q>,
Q: Hash + Eq,
Q: ?Sized + Hash + Equivalent<T>,
{
assert!(!self.in_use.get());
self.in_use.set(true);
Expand Down

0 comments on commit 1ae1448

Please sign in to comment.