Skip to content

Commit 1e1bb23

Browse files
committed
Auto merge of #295 - mwkmwkmwk:into_keys_values, r=Amanieu
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.
2 parents 4fa0e2c + ffe9595 commit 1e1bb23

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

src/map.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,52 @@ 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 {
823+
inner: self.into_iter(),
824+
}
825+
}
826+
827+
/// Creates a consuming iterator visiting all the values in arbitrary order.
828+
/// The map cannot be used after calling this.
829+
/// The iterator element type is `V`.
830+
///
831+
/// # Examples
832+
///
833+
/// ```
834+
/// use hashbrown::HashMap;
835+
///
836+
/// let mut map = HashMap::new();
837+
/// map.insert("a", 1);
838+
/// map.insert("b", 2);
839+
/// map.insert("c", 3);
840+
///
841+
/// let vec: Vec<i32> = map.into_values().collect();
842+
/// ```
843+
#[inline]
844+
pub fn into_values(self) -> IntoValues<K, V, A> {
845+
IntoValues {
846+
inner: self.into_iter(),
847+
}
848+
}
803849
}
804850

805851
impl<K, V, S, A> HashMap<K, V, S, A>
@@ -1614,6 +1660,88 @@ impl<K, V, A: Allocator + Clone> IntoIter<K, V, A> {
16141660
}
16151661
}
16161662

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

4149+
#[test]
4150+
fn test_into_keys() {
4151+
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
4152+
let map: HashMap<_, _> = vec.into_iter().collect();
4153+
let keys: Vec<_> = map.into_keys().collect();
4154+
4155+
assert_eq!(keys.len(), 3);
4156+
assert!(keys.contains(&1));
4157+
assert!(keys.contains(&2));
4158+
assert!(keys.contains(&3));
4159+
}
4160+
4161+
#[test]
4162+
fn test_into_values() {
4163+
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
4164+
let map: HashMap<_, _> = vec.into_iter().collect();
4165+
let values: Vec<_> = map.into_values().collect();
4166+
4167+
assert_eq!(values.len(), 3);
4168+
assert!(values.contains(&'a'));
4169+
assert!(values.contains(&'b'));
4170+
assert!(values.contains(&'c'));
4171+
}
4172+
40214173
#[test]
40224174
fn test_find() {
40234175
let mut m = HashMap::new();

0 commit comments

Comments
 (0)