From 6bcf27c9778497631d8a4a5f7c1599ba4c6763de Mon Sep 17 00:00:00 2001 From: Oleg Lebedev Date: Sun, 14 Apr 2024 16:57:21 +1000 Subject: [PATCH] remove unnecessary closures --- src/linked_hash_map.rs | 56 ++++++++++++++++++---------------------- tests/linked_hash_map.rs | 12 ++++----- 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/src/linked_hash_map.rs b/src/linked_hash_map.rs index f8b8bd1..1b6df36 100644 --- a/src/linked_hash_map.rs +++ b/src/linked_hash_map.rs @@ -1421,7 +1421,7 @@ pub struct Drain<'a, K, V> { /// or start of the list. From this position, the cursor can move in either direction as the /// linked list is circular, with the guard node connecting the two ends. /// - The current implementation does not include an `index` method, as it does not track the index -/// of its elements. It operates by providing items as key-value tuples, allowing the value to be +/// of its elements. It operates by providing elements as key-value tuples, allowing the value to be /// modified via a mutable reference while the key could not be changed. /// pub struct CursorMut<'a, K, V, S> { @@ -1772,7 +1772,7 @@ impl<'a, K, V, S> CursorMut<'a, K, V, S> { pub fn current(&mut self) -> Option<(&K, &mut V)> { unsafe { let at = NonNull::new_unchecked(self.cur); - self.peek(|| at) + self.peek(at) } } @@ -1781,7 +1781,7 @@ impl<'a, K, V, S> CursorMut<'a, K, V, S> { pub fn peek_next(&mut self) -> Option<(&K, &mut V)> { unsafe { let at = (*self.cur).links.value.next; - self.peek(|| at) + self.peek(at) } } @@ -1790,17 +1790,16 @@ impl<'a, K, V, S> CursorMut<'a, K, V, S> { pub fn peek_prev(&mut self) -> Option<(&K, &mut V)> { unsafe { let at = (*self.cur).links.value.prev; - self.peek(|| at) + self.peek(at) } } - // Retrieves the element specified by the at closure function without advancing current - // position to it. + // Retrieves the element without advancing current position to it. #[inline] - fn peek(&mut self, at: impl FnOnce() -> NonNull>) -> Option<(&K, &mut V)> { + fn peek(&mut self, at: NonNull>) -> Option<(&K, &mut V)> { if let Some(values) = self.values { unsafe { - let node = at().as_ptr(); + let node = at.as_ptr(); if node == values.as_ptr() { None } else { @@ -1818,7 +1817,7 @@ impl<'a, K, V, S> CursorMut<'a, K, V, S> { #[inline] pub fn move_next(&mut self) { let at = unsafe { (*self.cur).links.value.next }; - self.muv(|| at); + self.muv(at); } /// Updates the pointer to the current element to the previous element in the @@ -1826,19 +1825,19 @@ impl<'a, K, V, S> CursorMut<'a, K, V, S> { #[inline] pub fn move_prev(&mut self) { let at = unsafe { (*self.cur).links.value.prev }; - self.muv(|| at); + self.muv(at); } // Updates the pointer to the current element to the one returned by the at closure function. #[inline] - fn muv(&mut self, at: impl FnOnce() -> NonNull>) { - self.cur = at().as_ptr(); + fn muv(&mut self, at: NonNull>) { + self.cur = at.as_ptr(); } - /// Inserts the provided key and value before the current item. It checks if an entry - /// with the given key exists and, if so, replaces its value with the provided `1` - /// parameter from the given tuple. The key is not updated; this matters for types that - /// can be `==` without being identical. + /// Inserts the provided key and value before the current element. It checks if an entry + /// with the given key exists and, if so, replaces its value with the provided `key` + /// parameter. The key is not updated; this matters for types that can be `==` without + /// being identical. /// /// If the entry doesn't exist, it creates a new one. If a value has been updated, the /// function returns the *old* value wrapped with `Some` and `None` otherwise. @@ -1849,13 +1848,13 @@ impl<'a, K, V, S> CursorMut<'a, K, V, S> { S: BuildHasher, { let before = unsafe { NonNull::new_unchecked(self.cur) }; - self.insert(key, value, || before) + self.insert(key, value, before) } - /// Inserts the provided key and value after the current item. It checks if an entry - /// with the given key exists and, if so, replaces its value with the provided `1` - /// parameter from the given tuple. The key is not updated; this matters for types that - /// can be `==` without being identical. + /// Inserts the provided key and value after the current element. It checks if an entry + /// with the given key exists and, if so, replaces its value with the provided `key` + /// parameter. The key is not updated; this matters for types that can be `==` without + /// being identical. /// /// If the entry doesn't exist, it creates a new one. If a value has been updated, the /// function returns the *old* value wrapped with `Some` and `None` otherwise. @@ -1866,17 +1865,12 @@ impl<'a, K, V, S> CursorMut<'a, K, V, S> { S: BuildHasher, { let before = unsafe { (*self.cur).links.value.next }; - self.insert(key, value, || before) + self.insert(key, value, before) } - // Inserts `item` immediately before the element returned by the `before` closure function. + // Inserts an element immediately before the given `before` node. #[inline] - fn insert( - &mut self, - key: K, - value: V, - before: impl FnOnce() -> NonNull>, - ) -> Option + fn insert(&mut self, key: K, value: V, before: NonNull>) -> Option where K: Eq + Hash, S: BuildHasher, @@ -1892,13 +1886,13 @@ impl<'a, K, V, S> CursorMut<'a, K, V, S> { let mut node = *occupied.into_mut(); let pv = mem::replace(&mut node.as_mut().entry_mut().1, value); detach_node(node); - attach_before(node, before()); + attach_before(node, before); Some(pv) } Err(_) => { let mut new_node = allocate_node(self.free); new_node.as_mut().put_entry((key, value)); - attach_before(new_node, before()); + attach_before(new_node, before); let hash_builder = self.hash_builder; self.table.insert_unique(hash, new_node, move |k| { hash_key(hash_builder, (*k).as_ref().key_ref()) diff --git a/tests/linked_hash_map.rs b/tests/linked_hash_map.rs index c1d0573..eb85550 100644 --- a/tests/linked_hash_map.rs +++ b/tests/linked_hash_map.rs @@ -702,7 +702,7 @@ fn test_cursor_mut_insert_before() { map.insert(3, 3); map.insert(4, 4); - // Insert new item in the middle + // Insert new element in the middle if let linked_hash_map::Entry::Occupied(entry) = map.entry(4) { entry.cursor_mut().insert_before(5, 5); assert!(map @@ -711,7 +711,7 @@ fn test_cursor_mut_insert_before() { .eq([(3, 3), (5, 5), (4, 4)].iter().copied())); } - // Insert new item at the very end of the list + // Insert new element at the very end of the list if let linked_hash_map::Entry::Occupied(entry) = map.entry(3) { let mut cursor = entry.cursor_mut(); cursor.move_prev(); @@ -722,7 +722,7 @@ fn test_cursor_mut_insert_before() { .eq([(3, 3), (5, 5), (4, 4), (6, 6)].iter().copied())); } - // Relocate item and override value + // Relocate element and override value if let linked_hash_map::Entry::Occupied(entry) = map.entry(5) { entry.cursor_mut().insert_before(4, 42); assert!(map @@ -739,7 +739,7 @@ fn test_cursor_mut_insert_after() { map.insert(3, 3); map.insert(4, 4); - // Insert new item in the middle. + // Insert new element in the middle. if let linked_hash_map::Entry::Occupied(entry) = map.entry(3) { entry.cursor_mut().insert_after(5, 5); assert!(map @@ -748,7 +748,7 @@ fn test_cursor_mut_insert_after() { .eq([(3, 3), (5, 5), (4, 4)].iter().copied())); } - // Insert new item as the first one. + // Insert new element as the first one. if let linked_hash_map::Entry::Occupied(entry) = map.entry(4) { let mut cursor = entry.cursor_mut(); cursor.move_next(); @@ -763,7 +763,7 @@ fn test_cursor_mut_insert_after() { #[test] fn test_cursor_front_mut() { let mut map: LinkedHashMap = LinkedHashMap::new(); - // the CursorMut in an empty LinkedHashMap will always return `None` as its + // The `CursorMut`` in an empty LinkedHashMap will always return `None` as its // current element, regardless of any move in any direction. let mut cursor = map.cursor_front_mut(); assert!(cursor.current().is_none());