Skip to content

Commit 32e04b3

Browse files
committed
HashMap/HashSet: forward fold implementations of iterators
1 parent 287ae4d commit 32e04b3

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

library/std/src/collections/hash/map.rs

+72
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,14 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
22352235
fn size_hint(&self) -> (usize, Option<usize>) {
22362236
self.base.size_hint()
22372237
}
2238+
#[inline]
2239+
fn fold<B, F>(self, init: B, f: F) -> B
2240+
where
2241+
Self: Sized,
2242+
F: FnMut(B, Self::Item) -> B,
2243+
{
2244+
self.base.fold(init, f)
2245+
}
22382246
}
22392247
#[stable(feature = "rust1", since = "1.0.0")]
22402248
impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
@@ -2259,6 +2267,14 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
22592267
fn size_hint(&self) -> (usize, Option<usize>) {
22602268
self.base.size_hint()
22612269
}
2270+
#[inline]
2271+
fn fold<B, F>(self, init: B, f: F) -> B
2272+
where
2273+
Self: Sized,
2274+
F: FnMut(B, Self::Item) -> B,
2275+
{
2276+
self.base.fold(init, f)
2277+
}
22622278
}
22632279
#[stable(feature = "rust1", since = "1.0.0")]
22642280
impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
@@ -2293,6 +2309,14 @@ impl<K, V> Iterator for IntoIter<K, V> {
22932309
fn size_hint(&self) -> (usize, Option<usize>) {
22942310
self.base.size_hint()
22952311
}
2312+
#[inline]
2313+
fn fold<B, F>(self, init: B, f: F) -> B
2314+
where
2315+
Self: Sized,
2316+
F: FnMut(B, Self::Item) -> B,
2317+
{
2318+
self.base.fold(init, f)
2319+
}
22962320
}
22972321
#[stable(feature = "rust1", since = "1.0.0")]
22982322
impl<K, V> ExactSizeIterator for IntoIter<K, V> {
@@ -2323,6 +2347,14 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
23232347
fn size_hint(&self) -> (usize, Option<usize>) {
23242348
self.inner.size_hint()
23252349
}
2350+
#[inline]
2351+
fn fold<B, F>(self, init: B, mut f: F) -> B
2352+
where
2353+
Self: Sized,
2354+
F: FnMut(B, Self::Item) -> B,
2355+
{
2356+
self.inner.fold(init, |acc, (k, _)| f(acc, k))
2357+
}
23262358
}
23272359
#[stable(feature = "rust1", since = "1.0.0")]
23282360
impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
@@ -2346,6 +2378,14 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
23462378
fn size_hint(&self) -> (usize, Option<usize>) {
23472379
self.inner.size_hint()
23482380
}
2381+
#[inline]
2382+
fn fold<B, F>(self, init: B, mut f: F) -> B
2383+
where
2384+
Self: Sized,
2385+
F: FnMut(B, Self::Item) -> B,
2386+
{
2387+
self.inner.fold(init, |acc, (_, v)| f(acc, v))
2388+
}
23492389
}
23502390
#[stable(feature = "rust1", since = "1.0.0")]
23512391
impl<K, V> ExactSizeIterator for Values<'_, K, V> {
@@ -2369,6 +2409,14 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
23692409
fn size_hint(&self) -> (usize, Option<usize>) {
23702410
self.inner.size_hint()
23712411
}
2412+
#[inline]
2413+
fn fold<B, F>(self, init: B, mut f: F) -> B
2414+
where
2415+
Self: Sized,
2416+
F: FnMut(B, Self::Item) -> B,
2417+
{
2418+
self.inner.fold(init, |acc, (_, v)| f(acc, v))
2419+
}
23722420
}
23732421
#[stable(feature = "map_values_mut", since = "1.10.0")]
23742422
impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
@@ -2399,6 +2447,14 @@ impl<K, V> Iterator for IntoKeys<K, V> {
23992447
fn size_hint(&self) -> (usize, Option<usize>) {
24002448
self.inner.size_hint()
24012449
}
2450+
#[inline]
2451+
fn fold<B, F>(self, init: B, mut f: F) -> B
2452+
where
2453+
Self: Sized,
2454+
F: FnMut(B, Self::Item) -> B,
2455+
{
2456+
self.inner.fold(init, |acc, (k, _)| f(acc, k))
2457+
}
24022458
}
24032459
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
24042460
impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
@@ -2429,6 +2485,14 @@ impl<K, V> Iterator for IntoValues<K, V> {
24292485
fn size_hint(&self) -> (usize, Option<usize>) {
24302486
self.inner.size_hint()
24312487
}
2488+
#[inline]
2489+
fn fold<B, F>(self, init: B, mut f: F) -> B
2490+
where
2491+
Self: Sized,
2492+
F: FnMut(B, Self::Item) -> B,
2493+
{
2494+
self.inner.fold(init, |acc, (_, v)| f(acc, v))
2495+
}
24322496
}
24332497
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
24342498
impl<K, V> ExactSizeIterator for IntoValues<K, V> {
@@ -2459,6 +2523,14 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> {
24592523
fn size_hint(&self) -> (usize, Option<usize>) {
24602524
self.base.size_hint()
24612525
}
2526+
#[inline]
2527+
fn fold<B, F>(self, init: B, f: F) -> B
2528+
where
2529+
Self: Sized,
2530+
F: FnMut(B, Self::Item) -> B,
2531+
{
2532+
self.base.fold(init, f)
2533+
}
24622534
}
24632535
#[stable(feature = "drain", since = "1.6.0")]
24642536
impl<K, V> ExactSizeIterator for Drain<'_, K, V> {

library/std/src/collections/hash/set.rs

+58
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,14 @@ impl<'a, K> Iterator for Iter<'a, K> {
15001500
fn size_hint(&self) -> (usize, Option<usize>) {
15011501
self.base.size_hint()
15021502
}
1503+
#[inline]
1504+
fn fold<B, F>(self, init: B, f: F) -> B
1505+
where
1506+
Self: Sized,
1507+
F: FnMut(B, Self::Item) -> B,
1508+
{
1509+
self.base.fold(init, f)
1510+
}
15031511
}
15041512
#[stable(feature = "rust1", since = "1.0.0")]
15051513
impl<K> ExactSizeIterator for Iter<'_, K> {
@@ -1530,6 +1538,14 @@ impl<K> Iterator for IntoIter<K> {
15301538
fn size_hint(&self) -> (usize, Option<usize>) {
15311539
self.base.size_hint()
15321540
}
1541+
#[inline]
1542+
fn fold<B, F>(self, init: B, f: F) -> B
1543+
where
1544+
Self: Sized,
1545+
F: FnMut(B, Self::Item) -> B,
1546+
{
1547+
self.base.fold(init, f)
1548+
}
15331549
}
15341550
#[stable(feature = "rust1", since = "1.0.0")]
15351551
impl<K> ExactSizeIterator for IntoIter<K> {
@@ -1560,6 +1576,14 @@ impl<'a, K> Iterator for Drain<'a, K> {
15601576
fn size_hint(&self) -> (usize, Option<usize>) {
15611577
self.base.size_hint()
15621578
}
1579+
#[inline]
1580+
fn fold<B, F>(self, init: B, f: F) -> B
1581+
where
1582+
Self: Sized,
1583+
F: FnMut(B, Self::Item) -> B,
1584+
{
1585+
self.base.fold(init, f)
1586+
}
15631587
}
15641588
#[stable(feature = "rust1", since = "1.0.0")]
15651589
impl<K> ExactSizeIterator for Drain<'_, K> {
@@ -1639,6 +1663,15 @@ where
16391663
let (_, upper) = self.iter.size_hint();
16401664
(0, upper)
16411665
}
1666+
1667+
#[inline]
1668+
fn fold<B, F>(self, init: B, mut f: F) -> B
1669+
where
1670+
Self: Sized,
1671+
F: FnMut(B, Self::Item) -> B,
1672+
{
1673+
self.iter.fold(init, |acc, elt| if self.other.contains(elt) { f(acc, elt) } else { acc })
1674+
}
16421675
}
16431676

16441677
#[stable(feature = "std_debug", since = "1.16.0")]
@@ -1691,6 +1724,15 @@ where
16911724
let (_, upper) = self.iter.size_hint();
16921725
(0, upper)
16931726
}
1727+
1728+
#[inline]
1729+
fn fold<B, F>(self, init: B, mut f: F) -> B
1730+
where
1731+
Self: Sized,
1732+
F: FnMut(B, Self::Item) -> B,
1733+
{
1734+
self.iter.fold(init, |acc, elt| if self.other.contains(elt) { acc } else { f(acc, elt) })
1735+
}
16941736
}
16951737

16961738
#[stable(feature = "fused", since = "1.26.0")]
@@ -1736,6 +1778,14 @@ where
17361778
fn size_hint(&self) -> (usize, Option<usize>) {
17371779
self.iter.size_hint()
17381780
}
1781+
#[inline]
1782+
fn fold<B, F>(self, init: B, f: F) -> B
1783+
where
1784+
Self: Sized,
1785+
F: FnMut(B, Self::Item) -> B,
1786+
{
1787+
self.iter.fold(init, f)
1788+
}
17391789
}
17401790

17411791
#[stable(feature = "fused", since = "1.26.0")]
@@ -1800,6 +1850,14 @@ where
18001850
fn size_hint(&self) -> (usize, Option<usize>) {
18011851
self.iter.size_hint()
18021852
}
1853+
#[inline]
1854+
fn fold<B, F>(self, init: B, f: F) -> B
1855+
where
1856+
Self: Sized,
1857+
F: FnMut(B, Self::Item) -> B,
1858+
{
1859+
self.iter.fold(init, f)
1860+
}
18031861
}
18041862

18051863
#[allow(dead_code)]

0 commit comments

Comments
 (0)