Removes a specific entry from the queue, if it exists.
(&mut self, key: &K)
| 153 | |
| 154 | /// Removes a specific entry from the queue, if it exists. |
| 155 | pub fn remove(&mut self, key: &K) -> Option<V> { |
| 156 | if let Some((old_node, old_value)) = self.data.remove(key) { |
| 157 | let LruNode { key: _, prev, next } = &*old_node.lock(); |
| 158 | match (prev, next) { |
| 159 | // single node in the queue |
| 160 | (None, None) => { |
| 161 | self.queue.head = None; |
| 162 | self.queue.tail = None; |
| 163 | } |
| 164 | // removed the head node |
| 165 | (None, Some(n)) => { |
| 166 | let n_strong = |
| 167 | n.upgrade().expect("value has been unexpectedly dropped"); |
| 168 | n_strong.lock().prev = None; |
| 169 | self.queue.head = Some(Weak::clone(n)); |
| 170 | } |
| 171 | // removed the tail node |
| 172 | (Some(p), None) => { |
| 173 | let p_strong = |
| 174 | p.upgrade().expect("value has been unexpectedly dropped"); |
| 175 | p_strong.lock().next = None; |
| 176 | self.queue.tail = Some(Weak::clone(p)); |
| 177 | } |
| 178 | // removed a middle node |
| 179 | (Some(p), Some(n)) => { |
| 180 | let n_strong = |
| 181 | n.upgrade().expect("value has been unexpectedly dropped"); |
| 182 | let p_strong = |
| 183 | p.upgrade().expect("value has been unexpectedly dropped"); |
| 184 | n_strong.lock().prev = Some(Weak::clone(p)); |
| 185 | p_strong.lock().next = Some(Weak::clone(n)); |
| 186 | } |
| 187 | }; |
| 188 | Some(old_value) |
| 189 | } else { |
| 190 | None |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /// Returns the number of entries in the queue. |
| 195 | pub fn len(&self) -> usize { |
no outgoing calls