Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions library/core/src/iter/sources/repeat_n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ impl<A> Drop for RepeatN<A> {
}
}

/// Creates an empty iterator, like [`repeat_n(value, 0)`][`repeat_n`]
/// but without needing any such value at hand.
#[stable(feature = "iter_repeat_n_default", since = "CURRENT_RUSTC_VERSION")]
impl<A> Default for RepeatN<A> {
fn default() -> Self {
RepeatN { count: 0, element: MaybeUninit::uninit() }
}
}

#[stable(feature = "iter_repeat_n", since = "1.82.0")]
impl<A: Clone> Iterator for RepeatN<A> {
type Item = A;
Expand Down
16 changes: 16 additions & 0 deletions library/coretests/tests/iter/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,19 @@ fn test_repeat_n_soundness() {
let _z = y;
assert_eq!(0, *x);
}

#[test]
fn test_repeat_n_default() {
#[derive(Clone)]
pub struct PanicOnDrop;

impl Drop for PanicOnDrop {
fn drop(&mut self) {
unreachable!()
}
}

// The default is an empty iterator, so there's never any item to drop.
let iter = RepeatN::<PanicOnDrop>::default();
assert_eq!(iter.count(), 0);
}
Loading