From 68e68386a181277b9423267dbc94d9f947f87f4c Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Tue, 9 May 2023 21:44:14 +0100 Subject: [PATCH] Improve implementation of next --- agb-hashmap/src/lib.rs | 4 ++-- agb-hashmap/src/node.rs | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/agb-hashmap/src/lib.rs b/agb-hashmap/src/lib.rs index 1192667e..3bd6b13d 100644 --- a/agb-hashmap/src/lib.rs +++ b/agb-hashmap/src/lib.rs @@ -471,9 +471,9 @@ impl<'a, K, V, ALLOCATOR: ClonableAllocator> Iterator for Iter<'a, K, V, ALLOCAT let node = &self.map.nodes.node_at(self.at); self.at += 1; - if node.has_value() { + if let Some(key_value) = node.key_value_ref() { self.num_found += 1; - return Some((node.key_ref().unwrap(), node.value_ref().unwrap())); + return Some(key_value); } } } diff --git a/agb-hashmap/src/node.rs b/agb-hashmap/src/node.rs index dc96ceba..b6f32bf4 100644 --- a/agb-hashmap/src/node.rs +++ b/agb-hashmap/src/node.rs @@ -34,14 +34,6 @@ impl Node { } } - pub(crate) fn value_ref(&self) -> Option<&V> { - if self.has_value() { - Some(unsafe { self.value_ref_unchecked() }) - } else { - None - } - } - pub(crate) unsafe fn value_ref_unchecked(&self) -> &V { self.value.assume_init_ref() } @@ -66,6 +58,14 @@ impl Node { } } + pub(crate) fn key_value_ref(&self) -> Option<(&K, &V)> { + if self.has_value() { + Some(unsafe { self.key_value_ref_unchecked() }) + } else { + None + } + } + pub(crate) unsafe fn key_value_ref_unchecked(&self) -> (&K, &V) { (self.key.assume_init_ref(), self.value.assume_init_ref()) }