-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Implement TrustedLen for Take<Repeat> and Take<RangeFrom> #47944
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -970,9 +970,11 @@ impl<'a, I: FusedIterator + ?Sized> FusedIterator for &'a mut I {} | |
/// The iterator reports a size hint where it is either exact | ||
/// (lower bound is equal to upper bound), or the upper bound is [`None`]. | ||
/// The upper bound must only be [`None`] if the actual iterator length is | ||
/// larger than [`usize::MAX`]. | ||
/// larger than [`usize::MAX`]. In that case, the lower bound must be | ||
/// [`usize::MAX`], resulting in a [`.size_hint`] of `(usize::MAX, None)`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 to this. Probably worth mentioning in release notes just in case, though? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would I go about doing so? I don't know if it's really needed because TrustedLen is unstable anyways. But better safe than sorry. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think for now it just means someone with tagging permissions (thus not me, at least) putting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't have an impact to users of stable Rust, so then it's normally not in any release notes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, makes sense. I guess nightly finds out by watching the TWiR PRs section. |
||
/// | ||
/// The iterator must produce exactly the number of elements it reported. | ||
/// The iterator must produce exactly the number of elements it reported | ||
/// or diverge before reaching the end. | ||
/// | ||
/// # Safety | ||
/// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// compile-flags: -O | ||
// ignore-tidy-linelength | ||
|
||
#![crate_type = "lib"] | ||
|
||
use std::iter; | ||
|
||
// CHECK-LABEL: @repeat_take_collect | ||
#[no_mangle] | ||
pub fn repeat_take_collect() -> Vec<u8> { | ||
// CHECK: call void @llvm.memset.p0i8 | ||
iter::repeat(42).take(100000).collect() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🥇 |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh, this gets into the discussion about the "length of a x.. range" again.. I suppose it fits, it can panic before reaching the trusted length just as much as
any_iterator.map(f)
can.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it panics, it diverges, which should be fine. Worst case is that e.g. a Vector is currently collecting the elements. This will leave it with uninitialized values, but it should be dropped anyway / it shouldn't be accessable anymore due to the panic.
Edit:
Repeat
can panic as well ifClone
panics.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linking previous discussion about why
RangeFrom
needs to have an infinite size hint, which was also aboutTake<RangeFrom<_>>
: #42315 (comment)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oberien Yes, checking again to make sure, Vec's special case using
TrustedLen
trusts the length for the reallocation but it also behaves correctly on a possible panic inIterator::next()
.