Skip to content

Commit 6318cbb

Browse files
committed
HashMap/HashSet: forward fold implementations of iterators
1 parent e281163 commit 6318cbb

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

Diff for: library/std/src/collections/hash/map.rs

+72
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,14 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
22322232
fn size_hint(&self) -> (usize, Option<usize>) {
22332233
self.base.size_hint()
22342234
}
2235+
#[inline]
2236+
fn fold<B, F>(self, init: B, f: F) -> B
2237+
where
2238+
Self: Sized,
2239+
F: FnMut(B, Self::Item) -> B,
2240+
{
2241+
self.base.fold(init, f)
2242+
}
22352243
}
22362244
#[stable(feature = "rust1", since = "1.0.0")]
22372245
impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
@@ -2256,6 +2264,14 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
22562264
fn size_hint(&self) -> (usize, Option<usize>) {
22572265
self.base.size_hint()
22582266
}
2267+
#[inline]
2268+
fn fold<B, F>(self, init: B, f: F) -> B
2269+
where
2270+
Self: Sized,
2271+
F: FnMut(B, Self::Item) -> B,
2272+
{
2273+
self.base.fold(init, f)
2274+
}
22592275
}
22602276
#[stable(feature = "rust1", since = "1.0.0")]
22612277
impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
@@ -2290,6 +2306,14 @@ impl<K, V> Iterator for IntoIter<K, V> {
22902306
fn size_hint(&self) -> (usize, Option<usize>) {
22912307
self.base.size_hint()
22922308
}
2309+
#[inline]
2310+
fn fold<B, F>(self, init: B, f: F) -> B
2311+
where
2312+
Self: Sized,
2313+
F: FnMut(B, Self::Item) -> B,
2314+
{
2315+
self.base.fold(init, f)
2316+
}
22932317
}
22942318
#[stable(feature = "rust1", since = "1.0.0")]
22952319
impl<K, V> ExactSizeIterator for IntoIter<K, V> {
@@ -2320,6 +2344,14 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
23202344
fn size_hint(&self) -> (usize, Option<usize>) {
23212345
self.inner.size_hint()
23222346
}
2347+
#[inline]
2348+
fn fold<B, F>(self, init: B, mut f: F) -> B
2349+
where
2350+
Self: Sized,
2351+
F: FnMut(B, Self::Item) -> B,
2352+
{
2353+
self.inner.fold(init, |acc, (k, _)| f(acc, k))
2354+
}
23232355
}
23242356
#[stable(feature = "rust1", since = "1.0.0")]
23252357
impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
@@ -2343,6 +2375,14 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
23432375
fn size_hint(&self) -> (usize, Option<usize>) {
23442376
self.inner.size_hint()
23452377
}
2378+
#[inline]
2379+
fn fold<B, F>(self, init: B, mut f: F) -> B
2380+
where
2381+
Self: Sized,
2382+
F: FnMut(B, Self::Item) -> B,
2383+
{
2384+
self.inner.fold(init, |acc, (_, v)| f(acc, v))
2385+
}
23462386
}
23472387
#[stable(feature = "rust1", since = "1.0.0")]
23482388
impl<K, V> ExactSizeIterator for Values<'_, K, V> {
@@ -2366,6 +2406,14 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
23662406
fn size_hint(&self) -> (usize, Option<usize>) {
23672407
self.inner.size_hint()
23682408
}
2409+
#[inline]
2410+
fn fold<B, F>(self, init: B, mut f: F) -> B
2411+
where
2412+
Self: Sized,
2413+
F: FnMut(B, Self::Item) -> B,
2414+
{
2415+
self.inner.fold(init, |acc, (_, v)| f(acc, v))
2416+
}
23692417
}
23702418
#[stable(feature = "map_values_mut", since = "1.10.0")]
23712419
impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
@@ -2396,6 +2444,14 @@ impl<K, V> Iterator for IntoKeys<K, V> {
23962444
fn size_hint(&self) -> (usize, Option<usize>) {
23972445
self.inner.size_hint()
23982446
}
2447+
#[inline]
2448+
fn fold<B, F>(self, init: B, mut f: F) -> B
2449+
where
2450+
Self: Sized,
2451+
F: FnMut(B, Self::Item) -> B,
2452+
{
2453+
self.inner.fold(init, |acc, (k, _)| f(acc, k))
2454+
}
23992455
}
24002456
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
24012457
impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
@@ -2426,6 +2482,14 @@ impl<K, V> Iterator for IntoValues<K, V> {
24262482
fn size_hint(&self) -> (usize, Option<usize>) {
24272483
self.inner.size_hint()
24282484
}
2485+
#[inline]
2486+
fn fold<B, F>(self, init: B, mut f: F) -> B
2487+
where
2488+
Self: Sized,
2489+
F: FnMut(B, Self::Item) -> B,
2490+
{
2491+
self.inner.fold(init, |acc, (_, v)| f(acc, v))
2492+
}
24292493
}
24302494
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
24312495
impl<K, V> ExactSizeIterator for IntoValues<K, V> {
@@ -2456,6 +2520,14 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> {
24562520
fn size_hint(&self) -> (usize, Option<usize>) {
24572521
self.base.size_hint()
24582522
}
2523+
#[inline]
2524+
fn fold<B, F>(self, init: B, f: F) -> B
2525+
where
2526+
Self: Sized,
2527+
F: FnMut(B, Self::Item) -> B,
2528+
{
2529+
self.base.fold(init, f)
2530+
}
24592531
}
24602532
#[stable(feature = "drain", since = "1.6.0")]
24612533
impl<K, V> ExactSizeIterator for Drain<'_, K, V> {

Diff for: 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)