1 //! Central map trait to ease modifications and extensions down the road. 2 3 use crate::iter::{Iter, IterMut}; 4 use crate::lock::{RwLockReadGuard, RwLockWriteGuard}; 5 use crate::mapref::entry::Entry; 6 use crate::mapref::one::{Ref, RefMut}; 7 use crate::try_result::TryResult; 8 use crate::HashMap; 9 use core::borrow::Borrow; 10 use core::hash::{BuildHasher, Hash}; 11 12 /// Implementation detail that is exposed due to generic constraints in public types. 13 pub trait Map<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + Clone + BuildHasher> { _shard_count(&self) -> usize14 fn _shard_count(&self) -> usize; 15 16 /// # Safety 17 /// 18 /// The index must not be out of bounds. _get_read_shard(&'a self, i: usize) -> &'a HashMap<K, V, S>19 unsafe fn _get_read_shard(&'a self, i: usize) -> &'a HashMap<K, V, S>; 20 21 /// # Safety 22 /// 23 /// The index must not be out of bounds. _yield_read_shard(&'a self, i: usize) -> RwLockReadGuard<'a, HashMap<K, V, S>>24 unsafe fn _yield_read_shard(&'a self, i: usize) -> RwLockReadGuard<'a, HashMap<K, V, S>>; 25 26 /// # Safety 27 /// 28 /// The index must not be out of bounds. _yield_write_shard(&'a self, i: usize) -> RwLockWriteGuard<'a, HashMap<K, V, S>>29 unsafe fn _yield_write_shard(&'a self, i: usize) -> RwLockWriteGuard<'a, HashMap<K, V, S>>; 30 31 /// # Safety 32 /// 33 /// The index must not be out of bounds. _try_yield_read_shard( &'a self, i: usize, ) -> Option<RwLockReadGuard<'a, HashMap<K, V, S>>>34 unsafe fn _try_yield_read_shard( 35 &'a self, 36 i: usize, 37 ) -> Option<RwLockReadGuard<'a, HashMap<K, V, S>>>; 38 39 /// # Safety 40 /// 41 /// The index must not be out of bounds. _try_yield_write_shard( &'a self, i: usize, ) -> Option<RwLockWriteGuard<'a, HashMap<K, V, S>>>42 unsafe fn _try_yield_write_shard( 43 &'a self, 44 i: usize, 45 ) -> Option<RwLockWriteGuard<'a, HashMap<K, V, S>>>; 46 _insert(&self, key: K, value: V) -> Option<V>47 fn _insert(&self, key: K, value: V) -> Option<V>; 48 _remove<Q>(&self, key: &Q) -> Option<(K, V)> where K: Borrow<Q>, Q: Hash + Eq + ?Sized49 fn _remove<Q>(&self, key: &Q) -> Option<(K, V)> 50 where 51 K: Borrow<Q>, 52 Q: Hash + Eq + ?Sized; 53 _remove_if<Q>(&self, key: &Q, f: impl FnOnce(&K, &V) -> bool) -> Option<(K, V)> where K: Borrow<Q>, Q: Hash + Eq + ?Sized54 fn _remove_if<Q>(&self, key: &Q, f: impl FnOnce(&K, &V) -> bool) -> Option<(K, V)> 55 where 56 K: Borrow<Q>, 57 Q: Hash + Eq + ?Sized; 58 _remove_if_mut<Q>(&self, key: &Q, f: impl FnOnce(&K, &mut V) -> bool) -> Option<(K, V)> where K: Borrow<Q>, Q: Hash + Eq + ?Sized59 fn _remove_if_mut<Q>(&self, key: &Q, f: impl FnOnce(&K, &mut V) -> bool) -> Option<(K, V)> 60 where 61 K: Borrow<Q>, 62 Q: Hash + Eq + ?Sized; 63 _iter(&'a self) -> Iter<'a, K, V, S, Self> where Self: Sized64 fn _iter(&'a self) -> Iter<'a, K, V, S, Self> 65 where 66 Self: Sized; 67 _iter_mut(&'a self) -> IterMut<'a, K, V, S, Self> where Self: Sized68 fn _iter_mut(&'a self) -> IterMut<'a, K, V, S, Self> 69 where 70 Self: Sized; 71 _get<Q>(&'a self, key: &Q) -> Option<Ref<'a, K, V, S>> where K: Borrow<Q>, Q: Hash + Eq + ?Sized72 fn _get<Q>(&'a self, key: &Q) -> Option<Ref<'a, K, V, S>> 73 where 74 K: Borrow<Q>, 75 Q: Hash + Eq + ?Sized; 76 _get_mut<Q>(&'a self, key: &Q) -> Option<RefMut<'a, K, V, S>> where K: Borrow<Q>, Q: Hash + Eq + ?Sized77 fn _get_mut<Q>(&'a self, key: &Q) -> Option<RefMut<'a, K, V, S>> 78 where 79 K: Borrow<Q>, 80 Q: Hash + Eq + ?Sized; 81 _try_get<Q>(&'a self, key: &Q) -> TryResult<Ref<'a, K, V, S>> where K: Borrow<Q>, Q: Hash + Eq + ?Sized82 fn _try_get<Q>(&'a self, key: &Q) -> TryResult<Ref<'a, K, V, S>> 83 where 84 K: Borrow<Q>, 85 Q: Hash + Eq + ?Sized; 86 _try_get_mut<Q>(&'a self, key: &Q) -> TryResult<RefMut<'a, K, V, S>> where K: Borrow<Q>, Q: Hash + Eq + ?Sized87 fn _try_get_mut<Q>(&'a self, key: &Q) -> TryResult<RefMut<'a, K, V, S>> 88 where 89 K: Borrow<Q>, 90 Q: Hash + Eq + ?Sized; 91 _shrink_to_fit(&self)92 fn _shrink_to_fit(&self); 93 _retain(&self, f: impl FnMut(&K, &mut V) -> bool)94 fn _retain(&self, f: impl FnMut(&K, &mut V) -> bool); 95 _len(&self) -> usize96 fn _len(&self) -> usize; 97 _capacity(&self) -> usize98 fn _capacity(&self) -> usize; 99 _alter<Q>(&self, key: &Q, f: impl FnOnce(&K, V) -> V) where K: Borrow<Q>, Q: Hash + Eq + ?Sized100 fn _alter<Q>(&self, key: &Q, f: impl FnOnce(&K, V) -> V) 101 where 102 K: Borrow<Q>, 103 Q: Hash + Eq + ?Sized; 104 _alter_all(&self, f: impl FnMut(&K, V) -> V)105 fn _alter_all(&self, f: impl FnMut(&K, V) -> V); 106 _view<Q, R>(&self, key: &Q, f: impl FnOnce(&K, &V) -> R) -> Option<R> where K: Borrow<Q>, Q: Hash + Eq + ?Sized107 fn _view<Q, R>(&self, key: &Q, f: impl FnOnce(&K, &V) -> R) -> Option<R> 108 where 109 K: Borrow<Q>, 110 Q: Hash + Eq + ?Sized; 111 _entry(&'a self, key: K) -> Entry<'a, K, V, S>112 fn _entry(&'a self, key: K) -> Entry<'a, K, V, S>; 113 _try_entry(&'a self, key: K) -> Option<Entry<'a, K, V, S>>114 fn _try_entry(&'a self, key: K) -> Option<Entry<'a, K, V, S>>; 115 _hasher(&self) -> S116 fn _hasher(&self) -> S; 117 118 // provided _clear(&self)119 fn _clear(&self) { 120 self._retain(|_, _| false) 121 } 122 _contains_key<Q>(&'a self, key: &Q) -> bool where K: Borrow<Q>, Q: Hash + Eq + ?Sized,123 fn _contains_key<Q>(&'a self, key: &Q) -> bool 124 where 125 K: Borrow<Q>, 126 Q: Hash + Eq + ?Sized, 127 { 128 self._get(key).is_some() 129 } 130 _is_empty(&self) -> bool131 fn _is_empty(&self) -> bool { 132 self._len() == 0 133 } 134 } 135