Skip to content

Commit 5938eba

Browse files
committed
Auto merge of #38149 - bluss:is-empty, r=alexcrichton
Forward more ExactSizeIterator methods and `is_empty` edits - Forward ExactSizeIterator methods in more places, like `&mut I` and `Box<I>` iterator impls. - Improve `VecDeque::is_empty` itself (see commit 4) - All the collections iterators now have `len` or `is_empty` forwarded if doing so is a benefit. In the remaining cases, they already use a simple size hint (using something like a stored `usize` value), which is sufficient for the default implementation of len and is_empty.
2 parents 02ea82d + d53f82c commit 5938eba

File tree

9 files changed

+77
-9
lines changed

9 files changed

+77
-9
lines changed

src/liballoc/boxed.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,14 @@ impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {
535535
}
536536
}
537537
#[stable(feature = "rust1", since = "1.0.0")]
538-
impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {}
538+
impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {
539+
fn len(&self) -> usize {
540+
(**self).len()
541+
}
542+
fn is_empty(&self) -> bool {
543+
(**self).is_empty()
544+
}
545+
}
539546

540547
#[unstable(feature = "fused", issue = "35602")]
541548
impl<I: FusedIterator + ?Sized> FusedIterator for Box<I> {}

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#![feature(core_intrinsics)]
8080
#![feature(custom_attribute)]
8181
#![feature(dropck_parametricity)]
82+
#![cfg_attr(not(test), feature(exact_size_is_empty))]
8283
#![feature(fundamental)]
8384
#![feature(lang_items)]
8485
#![feature(needs_allocator)]

src/libcollections/binary_heap.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,11 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
986986
}
987987

988988
#[stable(feature = "rust1", since = "1.0.0")]
989-
impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
989+
impl<'a, T> ExactSizeIterator for Iter<'a, T> {
990+
fn is_empty(&self) -> bool {
991+
self.iter.is_empty()
992+
}
993+
}
990994

991995
#[unstable(feature = "fused", issue = "35602")]
992996
impl<'a, T> FusedIterator for Iter<'a, T> {}
@@ -1022,7 +1026,11 @@ impl<T> DoubleEndedIterator for IntoIter<T> {
10221026
}
10231027

10241028
#[stable(feature = "rust1", since = "1.0.0")]
1025-
impl<T> ExactSizeIterator for IntoIter<T> {}
1029+
impl<T> ExactSizeIterator for IntoIter<T> {
1030+
fn is_empty(&self) -> bool {
1031+
self.iter.is_empty()
1032+
}
1033+
}
10261034

10271035
#[unstable(feature = "fused", issue = "35602")]
10281036
impl<T> FusedIterator for IntoIter<T> {}
@@ -1057,7 +1065,11 @@ impl<'a, T: 'a> DoubleEndedIterator for Drain<'a, T> {
10571065
}
10581066

10591067
#[stable(feature = "drain", since = "1.6.0")]
1060-
impl<'a, T: 'a> ExactSizeIterator for Drain<'a, T> {}
1068+
impl<'a, T: 'a> ExactSizeIterator for Drain<'a, T> {
1069+
fn is_empty(&self) -> bool {
1070+
self.iter.is_empty()
1071+
}
1072+
}
10611073

10621074
#[unstable(feature = "fused", issue = "35602")]
10631075
impl<'a, T: 'a> FusedIterator for Drain<'a, T> {}

src/libcollections/vec_deque.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ impl<T> VecDeque<T> {
810810
/// ```
811811
#[stable(feature = "rust1", since = "1.0.0")]
812812
pub fn is_empty(&self) -> bool {
813-
self.len() == 0
813+
self.tail == self.head
814814
}
815815

816816
/// Create a draining iterator that removes the specified range in the
@@ -1916,7 +1916,11 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
19161916
}
19171917

19181918
#[stable(feature = "rust1", since = "1.0.0")]
1919-
impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
1919+
impl<'a, T> ExactSizeIterator for Iter<'a, T> {
1920+
fn is_empty(&self) -> bool {
1921+
self.head == self.tail
1922+
}
1923+
}
19201924

19211925
#[unstable(feature = "fused", issue = "35602")]
19221926
impl<'a, T> FusedIterator for Iter<'a, T> {}
@@ -1980,7 +1984,11 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
19801984
}
19811985

19821986
#[stable(feature = "rust1", since = "1.0.0")]
1983-
impl<'a, T> ExactSizeIterator for IterMut<'a, T> {}
1987+
impl<'a, T> ExactSizeIterator for IterMut<'a, T> {
1988+
fn is_empty(&self) -> bool {
1989+
self.head == self.tail
1990+
}
1991+
}
19841992

19851993
#[unstable(feature = "fused", issue = "35602")]
19861994
impl<'a, T> FusedIterator for IterMut<'a, T> {}
@@ -2017,7 +2025,11 @@ impl<T> DoubleEndedIterator for IntoIter<T> {
20172025
}
20182026

20192027
#[stable(feature = "rust1", since = "1.0.0")]
2020-
impl<T> ExactSizeIterator for IntoIter<T> {}
2028+
impl<T> ExactSizeIterator for IntoIter<T> {
2029+
fn is_empty(&self) -> bool {
2030+
self.inner.is_empty()
2031+
}
2032+
}
20212033

20222034
#[unstable(feature = "fused", issue = "35602")]
20232035
impl<T> FusedIterator for IntoIter<T> {}

src/libcollectionstest/vec_deque.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1007,3 +1007,24 @@ fn assert_covariance() {
10071007
d
10081008
}
10091009
}
1010+
1011+
#[test]
1012+
fn test_is_empty() {
1013+
let mut v = VecDeque::<i32>::new();
1014+
assert!(v.is_empty());
1015+
assert!(v.iter().is_empty());
1016+
assert!(v.iter_mut().is_empty());
1017+
v.extend(&[2, 3, 4]);
1018+
assert!(!v.is_empty());
1019+
assert!(!v.iter().is_empty());
1020+
assert!(!v.iter_mut().is_empty());
1021+
while let Some(_) = v.pop_front() {
1022+
assert_eq!(v.is_empty(), v.len() == 0);
1023+
assert_eq!(v.iter().is_empty(), v.iter().len() == 0);
1024+
assert_eq!(v.iter_mut().is_empty(), v.iter_mut().len() == 0);
1025+
}
1026+
assert!(v.is_empty());
1027+
assert!(v.iter().is_empty());
1028+
assert!(v.iter_mut().is_empty());
1029+
assert!(v.into_iter().is_empty());
1030+
}

src/libcore/iter/traits.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,14 @@ pub trait ExactSizeIterator: Iterator {
552552
}
553553

554554
#[stable(feature = "rust1", since = "1.0.0")]
555-
impl<'a, I: ExactSizeIterator + ?Sized> ExactSizeIterator for &'a mut I {}
555+
impl<'a, I: ExactSizeIterator + ?Sized> ExactSizeIterator for &'a mut I {
556+
fn len(&self) -> usize {
557+
(**self).len()
558+
}
559+
fn is_empty(&self) -> bool {
560+
(**self).is_empty()
561+
}
562+
}
556563

557564
/// Trait to represent types that can be created by summing up an iterator.
558565
///

src/libcore/str/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,11 @@ impl<'a> ExactSizeIterator for Bytes<'a> {
618618
fn len(&self) -> usize {
619619
self.0.len()
620620
}
621+
622+
#[inline]
623+
fn is_empty(&self) -> bool {
624+
self.0.is_empty()
625+
}
621626
}
622627

623628
#[unstable(feature = "fused", issue = "35602")]

src/libstd/env.rs

+2
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ impl Iterator for Args {
630630
#[stable(feature = "env", since = "1.0.0")]
631631
impl ExactSizeIterator for Args {
632632
fn len(&self) -> usize { self.inner.len() }
633+
fn is_empty(&self) -> bool { self.inner.is_empty() }
633634
}
634635

635636
#[stable(feature = "env_iterators", since = "1.11.0")]
@@ -649,6 +650,7 @@ impl Iterator for ArgsOs {
649650
#[stable(feature = "env", since = "1.0.0")]
650651
impl ExactSizeIterator for ArgsOs {
651652
fn len(&self) -> usize { self.inner.len() }
653+
fn is_empty(&self) -> bool { self.inner.is_empty() }
652654
}
653655

654656
#[stable(feature = "env_iterators", since = "1.11.0")]

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@
250250
#![feature(core_float)]
251251
#![feature(core_intrinsics)]
252252
#![feature(dropck_parametricity)]
253+
#![feature(exact_size_is_empty)]
253254
#![feature(float_extras)]
254255
#![feature(float_from_str_radix)]
255256
#![feature(fn_traits)]

0 commit comments

Comments
 (0)