From 0bea53da0d5d460529d3beb6d234502e1d6726e8 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Fri, 15 Mar 2024 12:07:40 -0700 Subject: [PATCH] Added a few trait impls + 0.2.4 --- CHANGELOG.md | 7 +++++++ Cargo.toml | 2 +- src/map.rs | 39 ++++++++++++++++++++++++++++++++++++++- src/set.rs | 6 +++--- src/tests.rs | 3 ++- 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c6c15819..38a5d7d8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## v0.2.4 + +### Added + +- `Map` now implements `Index` and `IndexMut`. +- `&mut Map` now implements `IntoIterator`. + ## v0.2.3 ### Added diff --git a/Cargo.toml b/Cargo.toml index 98bda71ec..53ee925e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kempt" -version = "0.2.3" +version = "0.2.4" edition = "2021" license = "MIT OR Apache-2.0" rust-version = "1.65.0" diff --git a/src/map.rs b/src/map.rs index f5e72a1c5..da1142e21 100644 --- a/src/map.rs +++ b/src/map.rs @@ -331,7 +331,7 @@ where #[must_use] #[inline] pub fn iter_mut(&mut self) -> IterMut<'_, Key, Value> { - IterMut(self.fields.iter_mut()) + self.into_iter() } /// Returns an iterator over the keys in this object. @@ -570,6 +570,30 @@ where } } +impl<'a, SearchFor, Key, V> core::ops::Index<&'a SearchFor> for Map +where + Key: Sort, + Key: Sort, + SearchFor: ?Sized, +{ + type Output = V; + + fn index(&self, index: &'a SearchFor) -> &Self::Output { + self.get(index).expect("key not found") + } +} + +impl<'a, SearchFor, Key, V> core::ops::IndexMut<&'a SearchFor> for Map +where + Key: Sort, + Key: Sort, + SearchFor: ?Sized, +{ + fn index_mut(&mut self, index: &'a SearchFor) -> &mut Self::Output { + self.get_mut(index).expect("key not found") + } +} + /// A key provided to the [`Map::entry`] function. /// /// This is a [`Cow`](alloc::borrow::Cow)-like type that is slightly more @@ -653,6 +677,19 @@ where } } +impl<'a, Key, Value> IntoIterator for &'a mut Map +where + Key: Sort, +{ + type IntoIter = IterMut<'a, Key, Value>; + type Item = (&'a Key, &'a mut Value); + + #[inline] + fn into_iter(self) -> Self::IntoIter { + IterMut(self.fields.iter_mut()) + } +} + impl IntoIterator for Map where Key: Sort, diff --git a/src/set.rs b/src/set.rs index f78f555fb..3d0d51bf6 100644 --- a/src/set.rs +++ b/src/set.rs @@ -293,7 +293,7 @@ where fn next(&mut self) -> Option { self.0 .next() - .map(|unioned| unioned.map_both(|_, _, _| OwnedOrRef::Owned(())).key) + .map(|unioned| unioned.map_both(|_, (), ()| OwnedOrRef::Owned(())).key) } #[inline] @@ -319,7 +319,7 @@ where #[inline] fn next(&mut self) -> Option { - self.0.next().map(|(k, _, _)| k) + self.0.next().map(|(k, (), ())| k) } #[inline] @@ -345,7 +345,7 @@ where #[inline] fn next(&mut self) -> Option { - self.0.next().map(|(k, _)| k) + self.0.next().map(|(k, ())| k) } #[inline] diff --git a/src/tests.rs b/src/tests.rs index d70633d00..677e47aff 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -48,6 +48,7 @@ fn basics() { assert_eq!(iter.next().unwrap(), 3); // Increment via iter_mut + #[allow(clippy::explicit_iter_loop)] // for coverage for (_, value) in map.iter_mut() { *value += 1; } @@ -133,7 +134,7 @@ fn entry() { unreachable!() }; assert_eq!(entry.replace(2), 1); - assert_eq!(map.get("b"), Some(&2)); + assert_eq!(map["b"], 2); assert_eq!(*map.entry("c").or_default(), 0);