Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When possible without changing semantics, implement Iterator::last in terms of DoubleEndedIterator::next_back for types in liballoc and libcore. #62316

Merged
merged 1 commit into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,14 @@ impl<I: Iterator + ?Sized> Iterator for Box<I> {
(**self).nth(n)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I: Iterator + Sized> Iterator for Box<I> {
fn last(self) -> Option<I::Item> where I: Sized {
(*self).last()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {
fn next_back(&mut self) -> Option<I::Item> {
Expand Down
5 changes: 5 additions & 0 deletions src/liballoc/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,11 @@ impl<'a, T> Iterator for Iter<'a, T> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}

#[inline]
fn last(self) -> Option<&'a T> {
self.iter.last()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
28 changes: 28 additions & 0 deletions src/liballoc/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,10 @@ impl<'a, K: 'a, V: 'a> Iterator for Iter<'a, K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.length, Some(self.length))
}

fn last(mut self) -> Option<(&'a K, &'a V)> {
self.next_back()
}
}

#[stable(feature = "fused", since = "1.26.0")]
Expand Down Expand Up @@ -1253,6 +1257,10 @@ impl<'a, K: 'a, V: 'a> Iterator for IterMut<'a, K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.length, Some(self.length))
}

fn last(mut self) -> Option<(&'a K, &'a mut V)> {
self.next_back()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -1421,6 +1429,10 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}

fn last(mut self) -> Option<&'a K> {
self.next_back()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -1458,6 +1470,10 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}

fn last(mut self) -> Option<&'a V> {
self.next_back()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -1495,6 +1511,10 @@ impl<'a, K, V> Iterator for Range<'a, K, V> {
unsafe { Some(self.next_unchecked()) }
}
}

fn last(mut self) -> Option<(&'a K, &'a V)> {
self.next_back()
}
}

#[stable(feature = "map_values_mut", since = "1.10.0")]
Expand All @@ -1508,6 +1528,10 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}

fn last(mut self) -> Option<&'a mut V> {
self.next_back()
}
}

#[stable(feature = "map_values_mut", since = "1.10.0")]
Expand Down Expand Up @@ -1626,6 +1650,10 @@ impl<'a, K, V> Iterator for RangeMut<'a, K, V> {
unsafe { Some(self.next_unchecked()) }
}
}

fn last(mut self) -> Option<(&'a K, &'a mut V)> {
self.next_back()
}
}

impl<'a, K, V> RangeMut<'a, K, V> {
Expand Down
7 changes: 7 additions & 0 deletions src/liballoc/collections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,9 @@ impl<'a, T> Iterator for Iter<'a, T> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
fn last(mut self) -> Option<&'a T> {
self.next_back()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
Expand Down Expand Up @@ -1073,6 +1076,10 @@ impl<'a, T> Iterator for Range<'a, T> {
fn next(&mut self) -> Option<&'a T> {
self.iter.next().map(|(k, _)| k)
}

fn last(mut self) -> Option<&'a T> {
self.next_back()
}
}

#[stable(feature = "btree_range", since = "1.17.0")]
Expand Down
10 changes: 10 additions & 0 deletions src/liballoc/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,11 @@ impl<'a, T> Iterator for Iter<'a, T> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}

#[inline]
fn last(mut self) -> Option<&'a T> {
self.next_back()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -881,6 +886,11 @@ impl<'a, T> Iterator for IterMut<'a, T> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}

#[inline]
fn last(mut self) -> Option<&'a mut T> {
self.next_back()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
10 changes: 10 additions & 0 deletions src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,11 @@ impl<'a, T> Iterator for Iter<'a, T> {
self.tail = self.head - iter.len();
final_res
}

#[inline]
fn last(mut self) -> Option<&'a T> {
self.next_back()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -2319,6 +2324,11 @@ impl<'a, T> Iterator for IterMut<'a, T> {
accum = front.iter_mut().fold(accum, &mut f);
back.iter_mut().fold(accum, &mut f)
}

#[inline]
fn last(mut self) -> Option<&'a mut T> {
self.next_back()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
5 changes: 5 additions & 0 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2385,6 +2385,11 @@ impl Iterator for Drain<'_> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}

#[inline]
fn last(mut self) -> Option<char> {
self.next_back()
}
}

#[stable(feature = "drain", since = "1.6.0")]
Expand Down
1 change: 1 addition & 0 deletions src/libcore/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl Iterator for EscapeDefault {
type Item = u8;
fn next(&mut self) -> Option<u8> { self.range.next().map(|i| self.data[i]) }
fn size_hint(&self) -> (usize, Option<usize>) { self.range.size_hint() }
fn last(mut self) -> Option<u8> { self.next_back() }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl DoubleEndedIterator for EscapeDefault {
Expand Down
15 changes: 15 additions & 0 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,11 @@ impl<'a> Iterator for Lines<'a> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}

#[inline]
fn last(mut self) -> Option<&'a str> {
self.next_back()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -4241,6 +4246,11 @@ impl<'a> Iterator for SplitWhitespace<'a> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}

#[inline]
fn last(mut self) -> Option<&'a str> {
self.next_back()
}
}

#[stable(feature = "split_whitespace", since = "1.1.0")]
Expand All @@ -4267,6 +4277,11 @@ impl<'a> Iterator for SplitAsciiWhitespace<'a> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}

#[inline]
fn last(mut self) -> Option<&'a str> {
self.next_back()
}
}

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