mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 16:54:01 +00:00
rewrite iterator Default
tests as doctests
This commit is contained in:
parent
05c7330ca0
commit
2b32b315f9
@ -1470,6 +1470,13 @@ impl<T> FusedIterator for IntoIter<T> {}
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T> Default for IntoIter<T> {
|
||||
/// Creates an empty `binary_heap::IntoIter`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::binary_heap;
|
||||
/// let iter: binary_heap::IntoIter<u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
IntoIter { iter: Default::default() }
|
||||
}
|
||||
|
@ -364,6 +364,13 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'_, K, V> {
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<'a, K: 'a, V: 'a> Default for Iter<'a, K, V> {
|
||||
/// Creates an empty `btree_map::Iter`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::btree_map;
|
||||
/// let iter: btree_map::Iter<'_, u8, u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Iter { range: Default::default(), length: 0 }
|
||||
}
|
||||
@ -395,6 +402,13 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<'a, K: 'a, V: 'a> Default for IterMut<'a, K, V> {
|
||||
/// Creates an empty `btree_map::IterMut`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::btree_map;
|
||||
/// let iter: btree_map::IterMut<'_, u8, u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
IterMut { range: Default::default(), length: 0, _marker: PhantomData {} }
|
||||
}
|
||||
@ -437,6 +451,13 @@ impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for IntoIter<K, V, A> {
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<K, V> Default for IntoIter<K, V> {
|
||||
/// Creates an empty `btree_map::IntoIter`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::btree_map;
|
||||
/// let iter: btree_map::IntoIter<u8, u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
IntoIter { range: Default::default(), length: 0, alloc: Global }
|
||||
}
|
||||
@ -1791,6 +1812,13 @@ impl<K, V> Clone for Keys<'_, K, V> {
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<K, V> Default for Keys<'_, K, V> {
|
||||
/// Creates an empty `btree_map::Keys`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::btree_map;
|
||||
/// let iter: btree_map::Keys<'_, u8, u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Keys { inner: Default::default() }
|
||||
}
|
||||
@ -1839,6 +1867,13 @@ impl<K, V> Clone for Values<'_, K, V> {
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<K, V> Default for Values<'_, K, V> {
|
||||
/// Creates an empty `btree_map::Values`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::btree_map;
|
||||
/// let iter: btree_map::Values<'_, u8, u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Values { inner: Default::default() }
|
||||
}
|
||||
@ -1982,6 +2017,13 @@ impl<'a, K, V> Iterator for Range<'a, K, V> {
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<K, V> Default for Range<'_, K, V> {
|
||||
/// Creates an empty `btree_map::Range`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::btree_map;
|
||||
/// let iter: btree_map::Range<'_, u8, u8> = Default::default();
|
||||
/// assert_eq!(iter.count(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Range { inner: Default::default() }
|
||||
}
|
||||
@ -2065,6 +2107,13 @@ impl<K, V, A: Allocator + Clone> FusedIterator for IntoKeys<K, V, A> {}
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<K, V> Default for IntoKeys<K, V> {
|
||||
/// Creates an empty `btree_map::IntoKeys`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::btree_map;
|
||||
/// let iter: btree_map::IntoKeys<u8, u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
IntoKeys { inner: Default::default() }
|
||||
}
|
||||
@ -2106,6 +2155,13 @@ impl<K, V, A: Allocator + Clone> FusedIterator for IntoValues<K, V, A> {}
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<K, V> Default for IntoValues<K, V> {
|
||||
/// Creates an empty `btree_map::IntoValues`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::btree_map;
|
||||
/// let iter: btree_map::IntoValues<u8, u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
IntoValues { inner: Default::default() }
|
||||
}
|
||||
|
@ -563,22 +563,6 @@ fn test_iter_min_max() {
|
||||
a.check();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iters_default() {
|
||||
let iter: Keys<'_, u8, u8> = Keys::default();
|
||||
assert_eq!(iter.len(), 0);
|
||||
let iter: Values<'_, u8, u8> = Values::default();
|
||||
assert_eq!(iter.len(), 0);
|
||||
let iter: Range<'_, u8, u8> = Range::default();
|
||||
assert_eq!(iter.count(), 0);
|
||||
let iter: IntoIter<u8, u8> = IntoIter::default();
|
||||
assert_eq!(iter.len(), 0);
|
||||
let iter: IntoKeys<u8, u8> = IntoKeys::default();
|
||||
assert_eq!(iter.len(), 0);
|
||||
let iter: IntoValues<u8, u8> = IntoValues::default();
|
||||
assert_eq!(iter.len(), 0);
|
||||
}
|
||||
|
||||
fn range_keys(map: &BTreeMap<i32, i32>, range: impl RangeBounds<i32>) -> Vec<i32> {
|
||||
Vec::from_iter(map.range(range).map(|(&k, &v)| {
|
||||
assert_eq!(k, v);
|
||||
|
@ -1547,6 +1547,13 @@ impl<T, A: Allocator + Clone> Iterator for IntoIter<T, A> {
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T> Default for Iter<'_, T> {
|
||||
/// Creates an empty `btree_set::Iter`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::btree_set;
|
||||
/// let iter: btree_set::Iter<'_, u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Iter { iter: Default::default() }
|
||||
}
|
||||
@ -1570,6 +1577,13 @@ impl<T, A: Allocator + Clone> FusedIterator for IntoIter<T, A> {}
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T> Default for IntoIter<T> {
|
||||
/// Creates an empty `btree_set::IntoIter`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::btree_set;
|
||||
/// let iter: btree_set::IntoIter<u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
IntoIter { iter: Default::default() }
|
||||
}
|
||||
@ -1615,6 +1629,13 @@ impl<T> FusedIterator for Range<'_, T> {}
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T> Default for Range<'_, T> {
|
||||
/// Creates an empty `btree_set::Range`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::btree_set;
|
||||
/// let iter: btree_set::Range<'_, u8> = Default::default();
|
||||
/// assert_eq!(iter.count(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Range { iter: Default::default() }
|
||||
}
|
||||
|
@ -1077,6 +1077,13 @@ impl<T> FusedIterator for Iter<'_, T> {}
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T> Default for Iter<'_, T> {
|
||||
/// Creates an empty `linked_list::Iter`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::linked_list;
|
||||
/// let iter: linked_list::Iter<'_, u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Iter { head: None, tail: None, len: 0, marker: Default::default() }
|
||||
}
|
||||
@ -1824,6 +1831,13 @@ impl<T> FusedIterator for IntoIter<T> {}
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T> Default for IntoIter<T> {
|
||||
/// Creates an empty `linked_list::IntoIter`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::linked_list;
|
||||
/// let iter: linked_list::IntoIter<u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
LinkedList::new().into_iter()
|
||||
}
|
||||
|
@ -172,12 +172,6 @@ fn test_iterator() {
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default() {
|
||||
let iter: IntoIter<u8> = Default::default();
|
||||
assert_eq!(iter.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_clone() {
|
||||
let mut n = LinkedList::new();
|
||||
|
@ -349,6 +349,14 @@ unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> {}
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T> Default for IntoIter<T> {
|
||||
/// Creates an empty `vec::IntoIter`.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::vec;
|
||||
/// let iter: vec::IntoIter<u8> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// assert_eq!(iter.as_slice(), &[]);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
super::Vec::new().into_iter()
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
use core::alloc::{Allocator, Layout};
|
||||
use core::assert_eq;
|
||||
use core::iter::{ExactSizeIterator, IntoIterator};
|
||||
use core::iter::IntoIterator;
|
||||
use core::ptr::NonNull;
|
||||
use std::alloc::System;
|
||||
use std::assert_matches::assert_matches;
|
||||
@ -1036,13 +1035,6 @@ fn test_into_iter_clone() {
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_into_iter_default() {
|
||||
let iter: IntoIter<u8> = Default::default();
|
||||
assert_eq!(iter.len(), 0);
|
||||
assert_eq!(iter.as_slice(), &[]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_into_iter_leak() {
|
||||
static mut DROPS: i32 = 0;
|
||||
|
@ -288,6 +288,21 @@ where
|
||||
A: Iterator + Default,
|
||||
B: Iterator + Default,
|
||||
{
|
||||
/// Creates a `Chain` from the default values for `A` and `B`.
|
||||
///
|
||||
/// ```
|
||||
/// # use core::iter::Chain;
|
||||
/// # use core::slice;
|
||||
/// # use std::collections::{btree_set, BTreeSet};
|
||||
/// # use std::mem;
|
||||
/// struct Foo<'a>(Chain<slice::Iter<'a, u8>, btree_set::Iter<'a, u8>>);
|
||||
///
|
||||
/// let set = BTreeSet::<u8>::new();
|
||||
/// let slice: &[u8] = &[];
|
||||
/// let mut foo = Foo(slice.iter().chain(set.iter()));
|
||||
///
|
||||
/// // take requires `Default`
|
||||
/// let _: Chain<_, _> = mem::take(&mut foo.0);
|
||||
fn default() -> Self {
|
||||
Chain::new(Default::default(), Default::default())
|
||||
}
|
||||
|
@ -160,6 +160,13 @@ where
|
||||
I: Default + Iterator<Item = &'a T>,
|
||||
T: Clone,
|
||||
{
|
||||
/// Creates a `Cloned` iterator from the default value of `I`
|
||||
/// ```
|
||||
/// # use core::slice;
|
||||
/// # use core::iter::Cloned;
|
||||
/// let iter: Cloned<slice::Iter<'_, u8>> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Self::new(Default::default())
|
||||
}
|
||||
|
@ -247,6 +247,13 @@ where
|
||||
I: Default + Iterator<Item = &'a T>,
|
||||
T: Copy,
|
||||
{
|
||||
/// Creates a `Copied` iterator from the default value of `I`
|
||||
/// ```
|
||||
/// # use core::slice;
|
||||
/// # use core::iter::Copied;
|
||||
/// let iter: Copied<slice::Iter<'_, u8>> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Self::new(Default::default())
|
||||
}
|
||||
|
@ -270,6 +270,13 @@ impl<I> Default for Enumerate<I>
|
||||
where
|
||||
I: Iterator + Default,
|
||||
{
|
||||
/// Creates an `Enumerate` iterator from the default value of `I`
|
||||
/// ```
|
||||
/// # use core::slice;
|
||||
/// # use std::iter::Enumerate;
|
||||
/// let iter: Enumerate<slice::Iter<'_, u8>> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Enumerate::new(Default::default())
|
||||
}
|
||||
|
@ -308,6 +308,14 @@ where
|
||||
I: Iterator + Default,
|
||||
<I as Iterator>::Item: IntoIterator,
|
||||
{
|
||||
/// Creates a `Flatten` iterator from the default value of `I`.
|
||||
///
|
||||
/// ```
|
||||
/// # use core::slice;
|
||||
/// # use std::iter::Flatten;
|
||||
/// let iter: Flatten<slice::Iter<'_, [u8; 4]>> = Default::default();
|
||||
/// assert_eq!(iter.count(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Flatten::new(Default::default())
|
||||
}
|
||||
|
@ -183,6 +183,14 @@ where
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<I: Default> Default for Fuse<I> {
|
||||
/// Creates a `Fuse` iterator from the default value of `I`.
|
||||
///
|
||||
/// ```
|
||||
/// # use core::slice;
|
||||
/// # use std::iter::Fuse;
|
||||
/// let iter: Fuse<slice::Iter<'_, u8>> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Fuse { iter: Default::default() }
|
||||
}
|
||||
|
@ -141,6 +141,13 @@ impl<I> Default for Rev<I>
|
||||
where
|
||||
I: Default + Iterator,
|
||||
{
|
||||
/// Creates a `Rev` iterator from the default value of `I`
|
||||
/// ```
|
||||
/// # use core::slice;
|
||||
/// # use core::iter::Rev;
|
||||
/// let iter: Rev<slice::Iter<'_, u8>> = Default::default();
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
Rev::new(Default::default())
|
||||
}
|
||||
|
@ -396,6 +396,13 @@ macro_rules! iterator {
|
||||
|
||||
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T> Default for $name<'_, T> {
|
||||
/// Creates an empty slice iterator.
|
||||
///
|
||||
/// ```
|
||||
#[doc = concat!("# use core::slice::", stringify!($name), ";")]
|
||||
#[doc = concat!("let iter: ", stringify!($name<'_, u8>), " = Default::default();")]
|
||||
/// assert_eq!(iter.len(), 0);
|
||||
/// ```
|
||||
fn default() -> Self {
|
||||
(& $( $mut_ )? []).into_iter()
|
||||
}
|
||||
|
@ -1,10 +1,8 @@
|
||||
use core::cell::Cell;
|
||||
use core::cmp::Ordering;
|
||||
use core::iter::ExactSizeIterator;
|
||||
use core::mem::MaybeUninit;
|
||||
use core::result::Result::{Err, Ok};
|
||||
use core::slice::Iter;
|
||||
use core::{assert_eq, slice};
|
||||
use core::slice;
|
||||
|
||||
#[test]
|
||||
fn test_position() {
|
||||
@ -226,12 +224,6 @@ fn test_iterator_count() {
|
||||
assert_eq!(iter2.count(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_default() {
|
||||
let iter: Iter<'_, u8> = Iter::default();
|
||||
assert_eq!(iter.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_chunks_count() {
|
||||
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
||||
|
Loading…
Reference in New Issue
Block a user