-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
impl TrustedLen for Cycle #68352
impl TrustedLen for Cycle #68352
Conversation
r? @dtolnay (rust_highfive has picked a reviewer for you, use r? to override) |
@@ -504,6 +504,9 @@ where | |||
#[stable(feature = "fused", since = "1.26.0")] | |||
impl<I> FusedIterator for Cycle<I> where I: Clone + Iterator {} | |||
|
|||
#[unstable(feature = "trusted_len", issue = "37572")] |
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.
There is no such thing as an unstable trait impl so this would need to be changed to #[stable].
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.
TrustedLen
isn't a stable trait. Unstable is correct here, I think.
This PR is adding: unsafe impl<I> TrustedLen for Cycle<I> where I: Clone + TrustedLen where I don't think this impl is correct because use std::iter::TrustedLen;
struct S(usize);
impl Clone for S {
fn clone(&self) -> Self {
S(0)
}
}
impl Iterator for S {
type Item = ();
fn next(&mut self) -> Option<Self::Item> {
if self.0 > 0 {
self.0 -= 1;
Some(())
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.0, Some(self.0))
}
}
unsafe impl TrustedLen for S {}
fn main() {
for x in S(4).cycle() {
println!("{:?}", x);
}
} FYI @rust-lang/libs |
Yeah, that's a fair point. While it's strictly speaking fine with built-in implementations, there is nothing to guarantee |
That was something I noticed in #66531 (comment), so sure, why not.
size_hint
method forCycle<I>
can return either(0, Some(0))
or(usize::MAX, None)
forTrustedLen
implementations ((0, None)
is impossible as(0, x) if x != Some(0)
returned by inner iterator is impossible byTrustedLen
invariant).