Skip to content

Commit

Permalink
Added a few trait impls + 0.2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Mar 15, 2024
1 parent a090033 commit 0bea53d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
39 changes: 38 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -570,6 +570,30 @@ where
}
}

impl<'a, SearchFor, Key, V> core::ops::Index<&'a SearchFor> for Map<Key, V>
where
Key: Sort<Key>,
Key: Sort<SearchFor>,
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<Key, V>
where
Key: Sort<Key>,
Key: Sort<SearchFor>,
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
Expand Down Expand Up @@ -653,6 +677,19 @@ where
}
}

impl<'a, Key, Value> IntoIterator for &'a mut Map<Key, Value>
where
Key: Sort<Key>,
{
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<Key, Value> IntoIterator for Map<Key, Value>
where
Key: Sort<Key>,
Expand Down
6 changes: 3 additions & 3 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ where
fn next(&mut self) -> Option<Self::Item> {
self.0
.next()
.map(|unioned| unioned.map_both(|_, _, _| OwnedOrRef::Owned(())).key)
.map(|unioned| unioned.map_both(|_, (), ()| OwnedOrRef::Owned(())).key)
}

#[inline]
Expand All @@ -319,7 +319,7 @@ where

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(k, _, _)| k)
self.0.next().map(|(k, (), ())| k)
}

#[inline]
Expand All @@ -345,7 +345,7 @@ where

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(k, _)| k)
self.0.next().map(|(k, ())| k)
}

#[inline]
Expand Down
3 changes: 2 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 0bea53d

Please sign in to comment.