Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! A concurrent insert only hash map.
2 //!
3 //! This crate implements a "memo map" which is in many ways similar to a
4 //! [`HashMap`] with some crucial differences:
5 //!
6 //! * Unlike a regular hash map, a memo map is thread safe and synchronized.
7 //! * Adding or retrieving keys works through a shared reference, removing only
8 //! through a mutable reference.
9 //! * Retrieving a value from a memo map returns a plain old reference.
10 //!
11 //! Together these purposes allow one to use this type of structure to
12 //! implement something similar to lazy loading in places where the API
13 //! has been constrained to references before.
14 //!
15 //! The values in the map are individually boxed up so that resizing of the
16 //! map retains the previously issued references.
17 //!
18 //! ```
19 //! use memo_map::MemoMap;
20 //!
21 //! let memo = MemoMap::new();
22 //! let one = memo.get_or_insert(&1, || "one".to_string());
23 //! let one2 = memo.get_or_insert(&1, || "not one".to_string());
24 //! assert_eq!(one, "one");
25 //! assert_eq!(one2, "one");
26 //! ```
27 //!
28 //! # Notes on Iteration
29 //!
30 //! Because the memo map internally uses a mutex it needs to be held during
31 //! iteration. This is potentially dangerous as it means you can easily
32 //! deadlock yourself when trying to use the memo map while iterating. The
33 //! iteration functionality thus has to be used with great care.
34 //!
35 //! # Notes on Removal
36 //!
37 //! Items can be removed from a memo map but this operation requires a mutable
38 //! reference to the memo map. This is so that it can ensure that there are no
39 //! borrows outstanding that would be invalidated through the removal of the item.
40 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/Borrow#;
41 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::hash_maprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash_map/::{Entryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/Entry#, RandomStaterust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/RandomState#};
42 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#;
43 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::Infalliblerust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Infallible#;
44 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::{BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher#, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#};
45 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::{transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute()., ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#};
46 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::{Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#, MutexGuardrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/MutexGuard#};
47
48 macro_rules! lockrust-analyzer cargo memo-map 0.3.1 lock! {
49 ($mutex:expr) => {
50 match $mutex.lock() {
51 Ok(guard) => guard,
52 Err(poisoned) => poisoned.into_inner(),
53 }
54 };
55 }
56
57 /// An insert only, thread safe hash map to memoize values.
58 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
59 pub struct MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#<K, V, S = RandomStaterust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/RandomState#> {
60 innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.: Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<V>, S>>,
61 }
62
63 impl<K: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, V: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, S: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#<K, V, S> {
64 fn clonerust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`][Clone]clone().(&self) -> Selfrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`][Clone] {
65 Selfrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`][Clone] {
66 innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.: Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]new().(lockrust-analyzer cargo memo-map 0.3.1 lock!!(self.innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.).clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][Clone]clone().()),
67 }
68 }
69 }
70
71 impl<K, V, S: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#> Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#<K, V, S> {
72 fn defaultrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`][Default]default().() -> Selfrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`][Default] {
73 MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap# {
74 innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.: Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]new().(HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::defaultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S>`][Default]default().()),
75 }
76 }
77 }
78
79 impl<K, V> MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#<K, V, RandomStaterust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/RandomState#> {
80 /// Creates an empty `MemoMap`.
81 pub fn newrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V>`]new().() -> MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#<K, V, RandomStaterust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/RandomState#> {
82 MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap# {
83 innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.: Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#::defaultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`][Default]default().(),
84 }
85 }
86 }
87
88 impl<K, V, S> MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#<K, V, S> {
89 /// Creates an empty `MemoMap` which will use the given hash builder to hash
90 /// keys.
91 pub fn with_hasherrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]with_hasher().(hash_builder: S) -> MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#<K, V, S> {
92 MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap# {
93 innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.: Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]new().(HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::with_hasherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S>`]with_hasher().(hash_builder)),
94 }
95 }
96 }
97
98 impl<K, V, S> MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#<K, V, S>
99 where
100 K: Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#,
101 S: BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher#,
102 {
103 /// Inserts a value into the memo map.
104 ///
105 /// This inserts a value for a specific key into the memo map. If the
106 /// key already exists, this method does nothing and instead returns `false`.
107 /// Otherwise the value is inserted and `true` is returned. It's generally
108 /// recommended to instead use [`get_or_insert`](Self::get_or_insert) or
109 /// it's sibling [`get_or_try_insert`](Self::get_or_try_insert).
110 pub fn insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(&self, key: K, value: V) -> bool {
111 let mut inner = lockrust-analyzer cargo memo-map 0.3.1 lock!!(self.innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.);
112 match inner.entryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]entry().(key) {
113 Entryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/Entry#::Occupiedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/Entry#Occupied#(_) => false,
114 Entryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/Entry#::Vacantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/Entry#Vacant#(vacant) => {
115 vacant.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`VacantEntry<'a, K, V, A>`]insert().(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(value));
116 true
117 }
118 }
119 }
120
121 /// Inserts a value into the memo map replacing the old value.
122 ///
123 /// This has the same restrictions as [`remove`](Self::remove) and
124 /// [`clear`](Self::clear) in that it requires a mutable reference to
125 /// the map.
126 pub fn replacerust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]replace().(&mut self, key: K, value: V) {
127 lockrust-analyzer cargo memo-map 0.3.1 lock!!(self.innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.).insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(key, Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(value));
128 }
129
130 /// Returns true if the map contains a value for the specified key.
131 ///
132 /// The key may be any borrowed form of the map's key type, but [`Hash`] and
133 /// [`Eq`] on the borrowed form must match those for the key type.
134 pub fn contains_keyrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]contains_key().<Q>(&self, key: &Q) -> bool
135 where
136 Q: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
137 K: Borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/Borrow#<Q>,
138 {
139 lockrust-analyzer cargo memo-map 0.3.1 lock!!(self.innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.).contains_keyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]contains_key().(key)
140 }
141
142 /// Returns a reference to the value corresponding to the key.
143 ///
144 /// The key may be any borrowed form of the map's key type, but [`Hash`] and
145 /// [`Eq`] on the borrowed form must match those for the key type.
146 pub fn getrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]get().<Q>(&self, key: &Q) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&V>
147 where
148 Q: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
149 K: Borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/Borrow#<Q>,
150 {
151 let inner = lockrust-analyzer cargo memo-map 0.3.1 lock!!(self.innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.);
152 let value = inner.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(key)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
153 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(unsafe { transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().::<_, _>(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().value) })
154 }
155
156 /// Returns a reference to the value corresponding to the key or inserts.
157 ///
158 /// This is the preferred way to work with a memo map: if the value has not
159 /// been in the map yet the creator function is invoked to create the value,
160 /// otherwise the already stored value is returned. The creator function itself
161 /// can be falliable and the error is passed through.
162 ///
163 /// If the creator is infallible, [`get_or_insert`](Self::get_or_insert) can be used.
164 pub fn get_or_try_insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]get_or_try_insert().<Q, F, E>(&self, key: &Q, creator: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&V, E>
165 where
166 Q: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + ToOwnedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/ToOwned#<Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/ToOwned#Owned# = K> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
167 K: Borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/Borrow#<Q>,
168 F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<V, E>,
169 {
170 let mut inner = lockrust-analyzer cargo memo-map 0.3.1 lock!!(self.innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.);
171 let value = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(value) = inner.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(key) {
172 value
173 } else {
174 inner.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(key.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/ToOwned#to_owned().(), Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(creator()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().));
175 inner.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(key).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()
176 };
177 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe { transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().::<_, _>(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().value) })
178 }
179
180 /// Returns a reference to the value corresponding to the key or inserts.
181 ///
182 /// This is the preferred way to work with a memo map: if the value has not
183 /// been in the map yet the creator function is invoked to create the value,
184 /// otherwise the already stored value is returned.
185 ///
186 /// If the creator is fallible, [`get_or_try_insert`](Self::get_or_try_insert) can be used.
187 ///
188 /// # Example
189 ///
190 /// ```
191 /// # use memo_map::MemoMap;
192 /// let memo = MemoMap::new();
193 ///
194 /// // first time inserts
195 /// let value = memo.get_or_insert("key", || "23");
196 /// assert_eq!(*value, "23");
197 ///
198 /// // second time returns old value
199 /// let value = memo.get_or_insert("key", || "24");
200 /// assert_eq!(*value, "23");
201 /// ```
202 pub fn get_or_insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]get_or_insert().<Q, F>(&self, key: &Q, creator: F) -> &V
203 where
204 Q: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + ToOwnedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/ToOwned#<Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/ToOwned#Owned# = K> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
205 K: Borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/Borrow#<Q>,
206 F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> V,
207 {
208 self.get_or_try_insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]get_or_try_insert().::<_, _, Infalliblerust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Infallible#>(key, || Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(creator()))
209 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
210 }
211
212 /// Removes a key from the memo map, returning the value at the key if the key
213 /// was previously in the map.
214 ///
215 /// A key can only be removed if a mutable reference to the memo map exists.
216 /// In other words a key can not be removed if there can be borrows to the item.
217 pub fn removerust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]remove().<Q>(&mut self, key: &Q) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<V>
218 where
219 Q: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
220 K: Borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/Borrow#<Q>,
221 {
222 lockrust-analyzer cargo memo-map 0.3.1 lock!!(self.innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.).removerust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]remove().(key).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|x| *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().x)
223 }
224
225 /// Clears the map, removing all elements.
226 pub fn clearrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]clear().(&mut self) {
227 lockrust-analyzer cargo memo-map 0.3.1 lock!!(self.innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.).clearrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]clear().();
228 }
229
230 /// Returns the number of items in the map.
231 ///
232 /// # Example
233 ///
234 /// ```
235 /// # use memo_map::MemoMap;
236 /// let memo = MemoMap::new();
237 ///
238 /// assert_eq!(memo.len(), 0);
239 /// memo.insert(1, "a");
240 /// memo.insert(2, "b");
241 /// memo.insert(2, "not b");
242 /// assert_eq!(memo.len(), 2);
243 /// ```
244 pub fn lenrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]len().(&self) -> usize {
245 lockrust-analyzer cargo memo-map 0.3.1 lock!!(self.innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.).lenrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]len().()
246 }
247
248 /// Returns `true` if the memo map contains no items.
249 pub fn is_emptyrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]is_empty().(&self) -> bool {
250 lockrust-analyzer cargo memo-map 0.3.1 lock!!(self.innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.).is_emptyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]is_empty().()
251 }
252
253 /// An iterator visiting all key-value pairs in arbitrary order. The
254 /// iterator element type is `(&'a K, &'a V)`.
255 ///
256 /// Important note: during iteration the map is locked! This means that you
257 /// must not perform modifications to the map or you will run into deadlocks.
258 pub fn iterrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]iter().(&self) -> Iterrust-analyzer cargo memo-map 0.3.1 Iter#<'_, K, V, S> {
259 let guard = lockrust-analyzer cargo memo-map 0.3.1 lock!!(self.innerrust-analyzer cargo memo-map 0.3.1 MemoMap#inner.);
260 let iter = guard.iterrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]iter().();
261 Iterrust-analyzer cargo memo-map 0.3.1 Iter# {
262 iterrust-analyzer cargo memo-map 0.3.1 Iter#iter.: unsafe { transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().::<_, _>(iter) },
263 guardrust-analyzer cargo memo-map 0.3.1 Iter#guard.: ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(guard),
264 }
265 }
266
267 /// An iterator visiting all keys in arbitrary order. The iterator element
268 /// type is `&'a K`.
269 pub fn keysrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]keys().(&self) -> Keysrust-analyzer cargo memo-map 0.3.1 Keys#<'_, K, V, S> {
270 Keysrust-analyzer cargo memo-map 0.3.1 Keys# { iterrust-analyzer cargo memo-map 0.3.1 Keys#iter.: self.iterrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]iter().() }
271 }
272 }
273
274 /// An iterator over the items of a [`MemoMap`].
275 ///
276 /// This struct is created by the [`iter`](MemoMap::iter) method on [`MemoMap`].
277 /// See its documentation for more information.
278 pub struct Iterrust-analyzer cargo memo-map 0.3.1 Iter#<'a, K, V, S> {
279 guardrust-analyzer cargo memo-map 0.3.1 Iter#guard.: ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#<MutexGuardrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/MutexGuard#<'a, HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<V>, S>>>,
280 iterrust-analyzer cargo memo-map 0.3.1 Iter#iter.: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::hash_maprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash_map/::Iterrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/Iter#<'a, K, Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<V>>,
281 }
282
283 impl<'a, K, V, S> Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for Iterrust-analyzer cargo memo-map 0.3.1 Iter#<'a, K, V, S> {
284 fn droprust-analyzer cargo memo-map 0.3.1 impl#[`Iter<'a, K, V, S>`][Drop]drop().(&mut self) {
285 unsafe {
286 ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]drop().(&mut self.guardrust-analyzer cargo memo-map 0.3.1 Iter#guard.);
287 }
288 }
289 }
290
291 impl<'a, K, V, S> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for Iterrust-analyzer cargo memo-map 0.3.1 Iter#<'a, K, V, S> {
292 type Itemrust-analyzer cargo memo-map 0.3.1 impl#[`Iter<'a, K, V, S>`][Iterator]Item# = (&'a K, &'a V);
293
294 fn nextrust-analyzer cargo memo-map 0.3.1 impl#[`Iter<'a, K, V, S>`][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo memo-map 0.3.1 impl#[`Iter<'a, K, V, S>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> {
295 self.iterrust-analyzer cargo memo-map 0.3.1 Iter#iter..nextrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`Iter<'a, K, V>`][Iterator]next().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|(k, v)| (k, &*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v))
296 }
297 }
298
299 /// An iterator over the keys of a [`MemoMap`].
300 ///
301 /// This struct is created by the [`keys`](MemoMap::keys) method on [`MemoMap`].
302 /// See its documentation for more information.
303 pub struct Keysrust-analyzer cargo memo-map 0.3.1 Keys#<'a, K, V, S> {
304 iterrust-analyzer cargo memo-map 0.3.1 Keys#iter.: Iterrust-analyzer cargo memo-map 0.3.1 Iter#<'a, K, V, S>,
305 }
306
307 impl<'a, K, V, S> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for Keysrust-analyzer cargo memo-map 0.3.1 Keys#<'a, K, V, S> {
308 type Itemrust-analyzer cargo memo-map 0.3.1 impl#[`Keys<'a, K, V, S>`][Iterator]Item# = &'a K;
309
310 fn nextrust-analyzer cargo memo-map 0.3.1 impl#[`Keys<'a, K, V, S>`][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo memo-map 0.3.1 impl#[`Keys<'a, K, V, S>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> {
311 self.iterrust-analyzer cargo memo-map 0.3.1 Keys#iter..nextrust-analyzer cargo memo-map 0.3.1 impl#[`Iter<'a, K, V, S>`][Iterator]next().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|(k, _)| k)
312 }
313 }
314
315 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
316 fn test_insertrust-analyzer cargo memo-map 0.3.1 test_insert().() {
317 let memo = MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#::newrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V>`]new().();
318 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(23u32, Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(1u32)));
319 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(23u32, Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(2u32)));
320 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(memo.getrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]get().(&23u32).clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<&T>`]cloned().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(1)));
321 }
322
323 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
324 fn test_iterrust-analyzer cargo memo-map 0.3.1 test_iter().() {
325 let memo = MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#::newrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V>`]new().();
326 memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(1, "one");
327 memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(2, "two");
328 memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(3, "three");
329 let mut values = memo.iterrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(k, v)| (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().k, *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v)).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>();
330 values.sortrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort().();
331 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(values, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![(1, "one"), (2, "two"), (3, "three")]);
332 }
333
334 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
335 fn test_keysrust-analyzer cargo memo-map 0.3.1 test_keys().() {
336 let memo = MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#::newrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V>`]new().();
337 memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(1, "one");
338 memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(2, "two");
339 memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(3, "three");
340 let mut values = memo.keysrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]keys().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|k| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().k).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>();
341 values.sortrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort().();
342 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(values, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1, 2, 3]);
343 }
344
345 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
346 fn test_containsrust-analyzer cargo memo-map 0.3.1 test_contains().() {
347 let memo = MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#::newrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V>`]new().();
348 memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(1, "one");
349 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(memo.contains_keyrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]contains_key().(&1));
350 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().memo.contains_keyrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]contains_key().(&2));
351 }
352
353 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
354 fn test_removerust-analyzer cargo memo-map 0.3.1 test_remove().() {
355 let mut memo = MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#::newrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V>`]new().();
356 memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(1, "one");
357 let value = memo.getrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]get().(&1);
358 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(value.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().());
359 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(value);
360 let old_value = memo.removerust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]remove().(&1);
361 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(old_value, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("one"));
362 let value = memo.getrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]get().(&1);
363 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(value.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().());
364 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(value);
365 }
366
367 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
368 fn test_clearrust-analyzer cargo memo-map 0.3.1 test_clear().() {
369 let mut memo = MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#::newrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V>`]new().();
370 memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(1, "one");
371 memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().(2, "two");
372 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(memo.lenrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]len().(), 2);
373 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().memo.is_emptyrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]is_empty().());
374 memo.clearrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]clear().();
375 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(memo.lenrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]len().(), 0);
376 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(memo.is_emptyrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]is_empty().());
377 }
378
379 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
380 fn test_ref_after_resizerust-analyzer cargo memo-map 0.3.1 test_ref_after_resize().() {
381 let memo = MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#::newrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V>`]new().();
382 let mut refs = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
383
384 let iterations = if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(miri) { 100 } else { 10000 };
385
386 for key in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#iterations {
387 refs.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().((key, memo.get_or_insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]get_or_insert().(&key, || Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(key))));
388 }
389 for (key, val) in refs {
390 dbgrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/dbg!!(key, val);
391 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(memo.getrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]get().(&key), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(val));
392 }
393 }
394
395 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
396 fn test_replacerust-analyzer cargo memo-map 0.3.1 test_replace().() {
397 let mut memo = MemoMaprust-analyzer cargo memo-map 0.3.1 MemoMap#::newrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V>`]new().();
398 memo.insertrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]insert().("foo", "bar");
399 memo.replacerust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]replace().("foo", "bar2");
400 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(memo.getrust-analyzer cargo memo-map 0.3.1 impl#[`MemoMap<K, V, S>`]get().("foo"), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&"bar2"));
401 }