diff options
Diffstat (limited to 'src/mutable_keys.rs')
-rw-r--r-- | src/mutable_keys.rs | 42 |
1 files changed, 13 insertions, 29 deletions
diff --git a/src/mutable_keys.rs b/src/mutable_keys.rs index 7efc779..35a90c4 100644 --- a/src/mutable_keys.rs +++ b/src/mutable_keys.rs @@ -1,6 +1,8 @@ use core::hash::{BuildHasher, Hash}; -use super::{Bucket, Entries, Equivalent, IndexMap}; +use super::{Equivalent, IndexMap}; + +pub struct PrivateMarker {} /// Opt-in mutable access to keys. /// @@ -14,15 +16,11 @@ use super::{Bucket, Entries, Equivalent, IndexMap}; /// implementing PartialEq, Eq, or Hash incorrectly would be). /// /// `use` this trait to enable its methods for `IndexMap`. -/// -/// This trait is sealed and cannot be implemented for types outside this crate. -pub trait MutableKeys: private::Sealed { +pub trait MutableKeys { type Key; type Value; /// Return item index, mutable reference to key and value - /// - /// Computes in **O(1)** time (average). fn get_full_mut2<Q: ?Sized>( &mut self, key: &Q, @@ -30,13 +28,6 @@ pub trait MutableKeys: private::Sealed { where Q: Hash + Equivalent<Self::Key>; - /// Return mutable reference to key and value at an index. - /// - /// Valid indices are *0 <= index < self.len()* - /// - /// Computes in **O(1)** time. - fn get_index_mut2(&mut self, index: usize) -> Option<(&mut Self::Key, &mut Self::Value)>; - /// Scan through each key-value pair in the map and keep those where the /// closure `keep` returns `true`. /// @@ -47,6 +38,11 @@ pub trait MutableKeys: private::Sealed { fn retain2<F>(&mut self, keep: F) where F: FnMut(&mut Self::Key, &mut Self::Value) -> bool; + + /// This method is not useful in itself – it is there to “seal” the trait + /// for external implementation, so that we can add methods without + /// causing breaking changes. + fn __private_marker(&self) -> PrivateMarker; } /// Opt-in mutable access to keys. @@ -59,21 +55,11 @@ where { type Key = K; type Value = V; - fn get_full_mut2<Q: ?Sized>(&mut self, key: &Q) -> Option<(usize, &mut K, &mut V)> where Q: Hash + Equivalent<K>, { - if let Some(i) = self.get_index_of(key) { - let entry = &mut self.as_entries_mut()[i]; - Some((i, &mut entry.key, &mut entry.value)) - } else { - None - } - } - - fn get_index_mut2(&mut self, index: usize) -> Option<(&mut K, &mut V)> { - self.as_entries_mut().get_mut(index).map(Bucket::muts) + self.get_full_mut2_impl(key) } fn retain2<F>(&mut self, keep: F) @@ -82,10 +68,8 @@ where { self.retain_mut(keep) } -} - -mod private { - pub trait Sealed {} - impl<K, V, S> Sealed for super::IndexMap<K, V, S> {} + fn __private_marker(&self) -> PrivateMarker { + PrivateMarker {} + } } |