Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/libcore/iter/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ impl<A: Clone> Iterator for Repeat<A> {

#[inline]
fn next(&mut self) -> Option<A> { Some(self.element.clone()) }

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) { (usize::MAX, None) }

#[inline]
fn nth(&mut self, _: usize) -> Option<A> { self.next() }
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
1 change: 1 addition & 0 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,7 @@ fn test_repeat() {
assert_eq!(it.next(), Some(42));
assert_eq!(it.next(), Some(42));
assert_eq!(it.next(), Some(42));
assert_eq!(it.nth(usize::MAX), Some(42));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider doing this with something that's need_drop, since LLVM already can remove the loop for trivial things like i32: https://play.rust-lang.org/?gist=546bdfadcb744e3aadabf698267b204f&version=nightly

Copy link
Copy Markdown
Contributor Author

@varkor varkor Jan 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure the loop is being removed here? https://play.rust-lang.org/?gist=c93f5f86a61ea6af7db1b3c2ab22b66f&version=nightly is terminated for taking too long to execute (which was the intention for this test case as well), which isn't the case after the specialisation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try https://play.rust-lang.org/?gist=c93f5f86a61ea6af7db1b3c2ab22b66f&version=stable&mode=release, and it doesn't time out. (Now, I'm sure at least one of the builds runs the test in debug, so it'd still catch it, but it's also a Copy type, so copy elision is definitely allowed -- a dead memcpy is certainly removable -- compared to a more complex Clone impl that's the subject of discussion in the issue.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it! I'll adjust the test, thanks!

}

#[test]
Expand Down