Skip to content

Commit 0a5cb8c

Browse files
committed
Add into_keys and into_values to HashMap.
This is pretty much a direct copy of rust-lang/rust#75163, aligning the hashbrown API with current std API.
1 parent 4fa0e2c commit 0a5cb8c

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

src/map.rs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,48 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
800800
pub fn clear(&mut self) {
801801
self.table.clear();
802802
}
803+
804+
/// Creates a consuming iterator visiting all the keys in arbitrary order.
805+
/// The map cannot be used after calling this.
806+
/// The iterator element type is `K`.
807+
///
808+
/// # Examples
809+
///
810+
/// ```
811+
/// use hashbrown::HashMap;
812+
///
813+
/// let mut map = HashMap::new();
814+
/// map.insert("a", 1);
815+
/// map.insert("b", 2);
816+
/// map.insert("c", 3);
817+
///
818+
/// let vec: Vec<&str> = map.into_keys().collect();
819+
/// ```
820+
#[inline]
821+
pub fn into_keys(self) -> IntoKeys<K, V, A> {
822+
IntoKeys { inner: self.into_iter() }
823+
}
824+
825+
/// Creates a consuming iterator visiting all the values in arbitrary order.
826+
/// The map cannot be used after calling this.
827+
/// The iterator element type is `V`.
828+
///
829+
/// # Examples
830+
///
831+
/// ```
832+
/// use hashbrown::HashMap;
833+
///
834+
/// let mut map = HashMap::new();
835+
/// map.insert("a", 1);
836+
/// map.insert("b", 2);
837+
/// map.insert("c", 3);
838+
///
839+
/// let vec: Vec<i32> = map.into_values().collect();
840+
/// ```
841+
#[inline]
842+
pub fn into_values(self) -> IntoValues<K, V, A> {
843+
IntoValues { inner: self.into_iter() }
844+
}
803845
}
804846

805847
impl<K, V, S, A> HashMap<K, V, S, A>
@@ -1614,6 +1656,84 @@ impl<K, V, A: Allocator + Clone> IntoIter<K, V, A> {
16141656
}
16151657
}
16161658

1659+
/// An owning iterator over the keys of a `HashMap`.
1660+
///
1661+
/// This `struct` is created by the [`into_keys`] method on [`HashMap`].
1662+
/// See its documentation for more.
1663+
///
1664+
/// [`into_keys`]: struct.HashMap.html#method.into_keys
1665+
/// [`HashMap`]: struct.HashMap.html
1666+
pub struct IntoKeys<K, V, A: Allocator + Clone = Global> {
1667+
inner: IntoIter<K, V, A>,
1668+
}
1669+
1670+
impl<K, V, A: Allocator + Clone> Iterator for IntoKeys<K, V, A> {
1671+
type Item = K;
1672+
1673+
#[inline]
1674+
fn next(&mut self) -> Option<K> {
1675+
self.inner.next().map(|(k, _)| k)
1676+
}
1677+
#[inline]
1678+
fn size_hint(&self) -> (usize, Option<usize>) {
1679+
self.inner.size_hint()
1680+
}
1681+
}
1682+
1683+
impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoKeys<K, V, A> {
1684+
#[inline]
1685+
fn len(&self) -> usize {
1686+
self.inner.len()
1687+
}
1688+
}
1689+
1690+
impl<K, V, A: Allocator + Clone> FusedIterator for IntoKeys<K, V, A> {}
1691+
1692+
impl<K: Debug, V: Debug, A: Allocator + Clone> fmt::Debug for IntoKeys<K, V, A> {
1693+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1694+
f.debug_list().entries(self.inner.iter().map(|(k, _)| k)).finish()
1695+
}
1696+
}
1697+
1698+
/// An owning iterator over the values of a `HashMap`.
1699+
///
1700+
/// This `struct` is created by the [`into_values`] method on [`HashMap`].
1701+
/// See its documentation for more.
1702+
///
1703+
/// [`into_values`]: struct.HashMap.html#method.into_values
1704+
/// [`HashMap`]: struct.HashMap.html
1705+
pub struct IntoValues<K, V, A: Allocator + Clone = Global> {
1706+
inner: IntoIter<K, V, A>,
1707+
}
1708+
1709+
impl<K, V, A: Allocator + Clone> Iterator for IntoValues<K, V, A> {
1710+
type Item = V;
1711+
1712+
#[inline]
1713+
fn next(&mut self) -> Option<V> {
1714+
self.inner.next().map(|(_, v)| v)
1715+
}
1716+
#[inline]
1717+
fn size_hint(&self) -> (usize, Option<usize>) {
1718+
self.inner.size_hint()
1719+
}
1720+
}
1721+
1722+
impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoValues<K, V, A> {
1723+
#[inline]
1724+
fn len(&self) -> usize {
1725+
self.inner.len()
1726+
}
1727+
}
1728+
1729+
impl<K, V, A: Allocator + Clone> FusedIterator for IntoValues<K, V, A> {}
1730+
1731+
impl<K: Debug, V: Debug, A: Allocator + Clone> fmt::Debug for IntoValues<K, V, A> {
1732+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1733+
f.debug_list().entries(self.inner.iter().map(|(k, _)| k)).finish()
1734+
}
1735+
}
1736+
16171737
/// An iterator over the keys of a `HashMap`.
16181738
///
16191739
/// This `struct` is created by the [`keys`] method on [`HashMap`]. See its
@@ -4018,6 +4138,30 @@ mod test_map {
40184138
assert!(values.contains(&6));
40194139
}
40204140

4141+
#[test]
4142+
fn test_into_keys() {
4143+
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
4144+
let map: HashMap<_, _> = vec.into_iter().collect();
4145+
let keys: Vec<_> = map.into_keys().collect();
4146+
4147+
assert_eq!(keys.len(), 3);
4148+
assert!(keys.contains(&1));
4149+
assert!(keys.contains(&2));
4150+
assert!(keys.contains(&3));
4151+
}
4152+
4153+
#[test]
4154+
fn test_into_values() {
4155+
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
4156+
let map: HashMap<_, _> = vec.into_iter().collect();
4157+
let values: Vec<_> = map.into_values().collect();
4158+
4159+
assert_eq!(values.len(), 3);
4160+
assert!(values.contains(&'a'));
4161+
assert!(values.contains(&'b'));
4162+
assert!(values.contains(&'c'));
4163+
}
4164+
40214165
#[test]
40224166
fn test_find() {
40234167
let mut m = HashMap::new();

0 commit comments

Comments
 (0)