Skip to content

Commit 3e86cf3

Browse files
committed
Add implementations of last in terms of next_back on a bunch of DoubleEndedIterators.
r?Manishearth
1 parent 8aaae42 commit 3e86cf3

File tree

14 files changed

+159
-0
lines changed

14 files changed

+159
-0
lines changed

src/liballoc/collections/binary_heap.rs

+15
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,11 @@ impl<'a, T> Iterator for Iter<'a, T> {
968968
fn size_hint(&self) -> (usize, Option<usize>) {
969969
self.iter.size_hint()
970970
}
971+
972+
#[inline]
973+
fn last(mut self) -> Option<&'a T> {
974+
self.next_back()
975+
}
971976
}
972977

973978
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1023,6 +1028,11 @@ impl<T> Iterator for IntoIter<T> {
10231028
fn size_hint(&self) -> (usize, Option<usize>) {
10241029
self.iter.size_hint()
10251030
}
1031+
1032+
#[inline]
1033+
fn last(mut self) -> Option<T> {
1034+
self.next_back()
1035+
}
10261036
}
10271037

10281038
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1069,6 +1079,11 @@ impl<T> Iterator for Drain<'_, T> {
10691079
fn size_hint(&self) -> (usize, Option<usize>) {
10701080
self.iter.size_hint()
10711081
}
1082+
1083+
#[inline]
1084+
fn last(mut self) -> Option<T> {
1085+
self.next_back()
1086+
}
10721087
}
10731088

10741089
#[stable(feature = "drain", since = "1.6.0")]

src/liballoc/collections/btree/map.rs

+40
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,11 @@ impl<'a, K: 'a, V: 'a> Iterator for Iter<'a, K, V> {
11931193
fn size_hint(&self) -> (usize, Option<usize>) {
11941194
(self.length, Some(self.length))
11951195
}
1196+
1197+
#[inline]
1198+
fn last(mut self) -> Option<(&'a K, &'a V)> {
1199+
self.next_back()
1200+
}
11961201
}
11971202

11981203
#[stable(feature = "fused", since = "1.26.0")]
@@ -1253,6 +1258,11 @@ impl<'a, K: 'a, V: 'a> Iterator for IterMut<'a, K, V> {
12531258
fn size_hint(&self) -> (usize, Option<usize>) {
12541259
(self.length, Some(self.length))
12551260
}
1261+
1262+
#[inline]
1263+
fn last(mut self) -> Option<(&'a K, &'a mut V)> {
1264+
self.next_back()
1265+
}
12561266
}
12571267

12581268
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1359,6 +1369,11 @@ impl<K, V> Iterator for IntoIter<K, V> {
13591369
fn size_hint(&self) -> (usize, Option<usize>) {
13601370
(self.length, Some(self.length))
13611371
}
1372+
1373+
#[inline]
1374+
fn last(mut self) -> Option<(K, V)> {
1375+
self.next_back()
1376+
}
13621377
}
13631378

13641379
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1421,6 +1436,11 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
14211436
fn size_hint(&self) -> (usize, Option<usize>) {
14221437
self.inner.size_hint()
14231438
}
1439+
1440+
#[inline]
1441+
fn last(mut self) -> Option<&'a K> {
1442+
self.next_back()
1443+
}
14241444
}
14251445

14261446
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1458,6 +1478,11 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
14581478
fn size_hint(&self) -> (usize, Option<usize>) {
14591479
self.inner.size_hint()
14601480
}
1481+
1482+
#[inline]
1483+
fn last(mut self) -> Option<&'a V> {
1484+
self.next_back()
1485+
}
14611486
}
14621487

14631488
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1495,6 +1520,11 @@ impl<'a, K, V> Iterator for Range<'a, K, V> {
14951520
unsafe { Some(self.next_unchecked()) }
14961521
}
14971522
}
1523+
1524+
#[inline]
1525+
fn last(mut self) -> Option<(&'a K, &'a V)> {
1526+
self.next_back()
1527+
}
14981528
}
14991529

15001530
#[stable(feature = "map_values_mut", since = "1.10.0")]
@@ -1508,6 +1538,11 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
15081538
fn size_hint(&self) -> (usize, Option<usize>) {
15091539
self.inner.size_hint()
15101540
}
1541+
1542+
#[inline]
1543+
fn last(mut self) -> Option<&'a mut V> {
1544+
self.next_back()
1545+
}
15111546
}
15121547

15131548
#[stable(feature = "map_values_mut", since = "1.10.0")]
@@ -1626,6 +1661,11 @@ impl<'a, K, V> Iterator for RangeMut<'a, K, V> {
16261661
unsafe { Some(self.next_unchecked()) }
16271662
}
16281663
}
1664+
1665+
#[inline]
1666+
fn last(mut self) -> Option<(&'a K, &'a mut V)> {
1667+
self.next_back()
1668+
}
16291669
}
16301670

16311671
impl<'a, K, V> RangeMut<'a, K, V> {

src/liballoc/collections/btree/set.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,11 @@ impl<'a, T> Iterator for Iter<'a, T> {
10191019
fn size_hint(&self) -> (usize, Option<usize>) {
10201020
self.iter.size_hint()
10211021
}
1022+
1023+
#[inline]
1024+
fn last(mut self) -> Option<&'a T> {
1025+
self.next_back()
1026+
}
10221027
}
10231028
#[stable(feature = "rust1", since = "1.0.0")]
10241029
impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
@@ -1044,6 +1049,11 @@ impl<T> Iterator for IntoIter<T> {
10441049
fn size_hint(&self) -> (usize, Option<usize>) {
10451050
self.iter.size_hint()
10461051
}
1052+
1053+
#[inline]
1054+
fn last(mut self) -> Option<T> {
1055+
self.next_back()
1056+
}
10471057
}
10481058
#[stable(feature = "rust1", since = "1.0.0")]
10491059
impl<T> DoubleEndedIterator for IntoIter<T> {
@@ -1073,6 +1083,11 @@ impl<'a, T> Iterator for Range<'a, T> {
10731083
fn next(&mut self) -> Option<&'a T> {
10741084
self.iter.next().map(|(k, _)| k)
10751085
}
1086+
1087+
#[inline]
1088+
fn last(mut self) -> Option<&'a T> {
1089+
self.next_back()
1090+
}
10761091
}
10771092

10781093
#[stable(feature = "btree_range", since = "1.17.0")]

src/liballoc/string.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,10 @@ impl Iterator for Drain<'_> {
23672367
fn size_hint(&self) -> (usize, Option<usize>) {
23682368
self.iter.size_hint()
23692369
}
2370+
#[inline]
2371+
fn last(mut self) -> Option<char> {
2372+
self.next_back()
2373+
}
23702374
}
23712375

23722376
#[stable(feature = "drain", since = "1.6.0")]

src/liballoc/vec.rs

+14
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,11 @@ impl<T> Iterator for IntoIter<T> {
23852385
fn count(self) -> usize {
23862386
self.len()
23872387
}
2388+
2389+
#[inline]
2390+
fn last(mut self) -> Option<T> {
2391+
self.next_back()
2392+
}
23882393
}
23892394

23902395
#[stable(feature = "rust1", since = "1.0.0")]
@@ -2504,6 +2509,11 @@ impl<T> Iterator for Drain<'_, T> {
25042509
fn size_hint(&self) -> (usize, Option<usize>) {
25052510
self.iter.size_hint()
25062511
}
2512+
2513+
#[inline]
2514+
fn last(mut self) -> Option<T> {
2515+
self.next_back()
2516+
}
25072517
}
25082518

25092519
#[stable(feature = "drain", since = "1.6.0")]
@@ -2573,6 +2583,10 @@ impl<I: Iterator> Iterator for Splice<'_, I> {
25732583
fn size_hint(&self) -> (usize, Option<usize>) {
25742584
self.drain.size_hint()
25752585
}
2586+
2587+
fn last(mut self) -> Option<Self::Item> {
2588+
self.next_back()
2589+
}
25762590
}
25772591

25782592
#[stable(feature = "vec_splice", since = "1.21.0")]

src/libcore/ascii.rs

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ impl Iterator for EscapeDefault {
117117
type Item = u8;
118118
fn next(&mut self) -> Option<u8> { self.range.next().map(|i| self.data[i]) }
119119
fn size_hint(&self) -> (usize, Option<usize>) { self.range.size_hint() }
120+
#[inline]
121+
fn last(mut self) -> Option<u8> { self.next_back() }
120122
}
121123
#[stable(feature = "rust1", since = "1.0.0")]
122124
impl DoubleEndedIterator for EscapeDefault {

src/libcore/iter/adapters/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ impl<I> Iterator for Rev<I> where I: DoubleEndedIterator {
7373
{
7474
self.iter.position(predicate)
7575
}
76+
77+
#[inline]
78+
fn last(mut self) -> Option<Self::Item> {
79+
self.next_back()
80+
}
7681
}
7782

7883
#[stable(feature = "rust1", since = "1.0.0")]

src/libcore/slice/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -3539,6 +3539,11 @@ impl<'a, T, P> Iterator for Split<'a, T, P> where P: FnMut(&T) -> bool {
35393539
(1, Some(self.v.len() + 1))
35403540
}
35413541
}
3542+
3543+
#[inline]
3544+
fn last(mut self) -> Option<Self::Item> {
3545+
self.next_back()
3546+
}
35423547
}
35433548

35443549
#[stable(feature = "rust1", since = "1.0.0")]
@@ -3637,6 +3642,11 @@ impl<'a, T, P> Iterator for SplitMut<'a, T, P> where P: FnMut(&T) -> bool {
36373642
(1, Some(self.v.len() + 1))
36383643
}
36393644
}
3645+
3646+
#[inline]
3647+
fn last(mut self) -> Option<Self::Item> {
3648+
self.next_back()
3649+
}
36403650
}
36413651

36423652
#[stable(feature = "rust1", since = "1.0.0")]
@@ -3702,6 +3712,11 @@ impl<'a, T, P> Iterator for RSplit<'a, T, P> where P: FnMut(&T) -> bool {
37023712
fn size_hint(&self) -> (usize, Option<usize>) {
37033713
self.inner.size_hint()
37043714
}
3715+
3716+
#[inline]
3717+
fn last(mut self) -> Option<Self::Item> {
3718+
self.next_back()
3719+
}
37053720
}
37063721

37073722
#[stable(feature = "slice_rsplit", since = "1.27.0")]
@@ -3766,6 +3781,11 @@ impl<'a, T, P> Iterator for RSplitMut<'a, T, P> where P: FnMut(&T) -> bool {
37663781
fn size_hint(&self) -> (usize, Option<usize>) {
37673782
self.inner.size_hint()
37683783
}
3784+
3785+
#[inline]
3786+
fn last(mut self) -> Option<Self::Item> {
3787+
self.next_back()
3788+
}
37693789
}
37703790

37713791
#[stable(feature = "slice_rsplit", since = "1.27.0")]

src/libcore/str/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,11 @@ impl<'a> Iterator for Lines<'a> {
13311331
fn size_hint(&self) -> (usize, Option<usize>) {
13321332
self.0.size_hint()
13331333
}
1334+
1335+
#[inline]
1336+
fn last(mut self) -> Option<Self::Item> {
1337+
self.next_back()
1338+
}
13341339
}
13351340

13361341
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1377,6 +1382,11 @@ impl<'a> Iterator for LinesAny<'a> {
13771382
fn size_hint(&self) -> (usize, Option<usize>) {
13781383
self.0.size_hint()
13791384
}
1385+
1386+
#[inline]
1387+
fn last(mut self) -> Option<Self::Item> {
1388+
self.next_back()
1389+
}
13801390
}
13811391

13821392
#[stable(feature = "rust1", since = "1.0.0")]
@@ -4215,6 +4225,11 @@ impl<'a> Iterator for SplitWhitespace<'a> {
42154225
fn size_hint(&self) -> (usize, Option<usize>) {
42164226
self.inner.size_hint()
42174227
}
4228+
4229+
#[inline]
4230+
fn last(mut self) -> Option<Self::Item> {
4231+
self.next_back()
4232+
}
42184233
}
42194234

42204235
#[stable(feature = "split_whitespace", since = "1.1.0")]
@@ -4241,6 +4256,11 @@ impl<'a> Iterator for SplitAsciiWhitespace<'a> {
42414256
fn size_hint(&self) -> (usize, Option<usize>) {
42424257
self.inner.size_hint()
42434258
}
4259+
4260+
#[inline]
4261+
fn last(mut self) -> Option<Self::Item> {
4262+
self.next_back()
4263+
}
42444264
}
42454265

42464266
#[stable(feature = "split_ascii_whitespace", since = "1.34.0")]

src/libstd/env.rs

+6
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,10 @@ impl Iterator for Args {
740740
self.inner.next().map(|s| s.into_string().unwrap())
741741
}
742742
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
743+
#[inline]
744+
fn last(mut self) -> Option<String> {
745+
self.next_back()
746+
}
743747
}
744748

745749
#[stable(feature = "env", since = "1.0.0")]
@@ -775,6 +779,8 @@ impl Iterator for ArgsOs {
775779
type Item = OsString;
776780
fn next(&mut self) -> Option<OsString> { self.inner.next() }
777781
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
782+
#[inline]
783+
fn last(mut self) -> Option<OsString> { self.next_back() }
778784
}
779785

780786
#[stable(feature = "env", since = "1.0.0")]

src/libstd/path.rs

+10
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,11 @@ impl<'a> Iterator for Iter<'a> {
886886
fn next(&mut self) -> Option<&'a OsStr> {
887887
self.inner.next().map(Component::as_os_str)
888888
}
889+
890+
#[inline]
891+
fn last(mut self) -> Option<&'a OsStr> {
892+
self.next_back()
893+
}
889894
}
890895

891896
#[stable(feature = "rust1", since = "1.0.0")]
@@ -949,6 +954,11 @@ impl<'a> Iterator for Components<'a> {
949954
}
950955
None
951956
}
957+
958+
#[inline]
959+
fn last(mut self) -> Option<Self::Item> {
960+
self.next_back()
961+
}
952962
}
953963

954964
#[stable(feature = "rust1", since = "1.0.0")]

src/libstd/sys/unix/args.rs

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ impl Iterator for Args {
3535
type Item = OsString;
3636
fn next(&mut self) -> Option<OsString> { self.iter.next() }
3737
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
38+
#[inline]
39+
fn last(mut self) -> Option<OsString> { self.next_back() }
3840
}
3941

4042
impl ExactSizeIterator for Args {

src/libstd/sys/wasm/args.rs

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ impl Iterator for Args {
3737
fn size_hint(&self) -> (usize, Option<usize>) {
3838
self.iter.size_hint()
3939
}
40+
#[inline]
41+
fn last(mut self) -> Option<OsString> {
42+
self.next_back()
43+
}
4044
}
4145

4246
impl ExactSizeIterator for Args {

0 commit comments

Comments
 (0)