Skip to content

Commit

Permalink
remove unnecessary closures
Browse files Browse the repository at this point in the history
  • Loading branch information
olebedev committed Apr 14, 2024
1 parent db0b224 commit 6bcf27c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
56 changes: 25 additions & 31 deletions src/linked_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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<Node<K, V>>) -> Option<(&K, &mut V)> {
fn peek(&mut self, at: NonNull<Node<K, V>>) -> 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 {
Expand All @@ -1818,27 +1817,27 @@ 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
/// list (that is, moving towards the front).
#[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<Node<K, V>>) {
self.cur = at().as_ptr();
fn muv(&mut self, at: NonNull<Node<K, V>>) {
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.
Expand All @@ -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.
Expand All @@ -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<Node<K, V>>,
) -> Option<V>
fn insert(&mut self, key: K, value: V, before: NonNull<Node<K, V>>) -> Option<V>
where
K: Eq + Hash,
S: BuildHasher,
Expand All @@ -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())
Expand Down
12 changes: 6 additions & 6 deletions tests/linked_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -763,7 +763,7 @@ fn test_cursor_mut_insert_after() {
#[test]
fn test_cursor_front_mut() {
let mut map: LinkedHashMap<i32, i32> = 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());
Expand Down

0 comments on commit 6bcf27c

Please sign in to comment.