-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Endless Iterators #90093
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
Endless Iterators #90093
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 | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,10 @@ | ||||||||||||||||||||||||||||||||||||
use crate::{iter::FusedIterator, ops::Try}; | ||||||||||||||||||||||||||||||||||||
use crate::{ | ||||||||||||||||||||||||||||||||||||
iter::{ | ||||||||||||||||||||||||||||||||||||
traits::{EndlessIterator, EndlessOrExact}, | ||||||||||||||||||||||||||||||||||||
FusedIterator, | ||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||
ops::Try, | ||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||
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. nit: multiple separate |
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/// An iterator that repeats endlessly. | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
|
@@ -106,3 +112,6 @@ where | |||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
#[stable(feature = "fused", since = "1.26.0")] | ||||||||||||||||||||||||||||||||||||
impl<I> FusedIterator for Cycle<I> where I: Clone + Iterator {} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
#[unstable(issue = "none", feature = "none")] | ||||||||||||||||||||||||||||||||||||
unsafe impl<I> EndlessIterator for Cycle<I> where I: Clone + EndlessOrExact {} | ||||||||||||||||||||||||||||||||||||
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.
If this is intended to be unsafe then we need to introduce 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. If trying to reflect a "not more than an allocatable amount" property, that may be better off with a type representing the logic of rust/library/alloc/src/raw_vec.rs Lines 539 to 555 in 1067e2c
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use crate::fmt; | ||
use crate::iter::traits::{EndlessIterator, EndlessOrExact}; | ||
use crate::iter::{DoubleEndedIterator, Fuse, FusedIterator, Iterator, Map, TrustedLen}; | ||
use crate::ops::Try; | ||
|
||
|
@@ -138,6 +139,26 @@ where | |
{ | ||
} | ||
|
||
#[unstable(issue = "none", feature = "endless")] | ||
unsafe impl<I, U, F> EndlessIterator for FlatMap<I, U, F> | ||
where | ||
I: EndlessIterator, | ||
U: IntoIterator, | ||
U::IntoIter: EndlessOrExact, | ||
F: FnMut(I::Item) -> U, | ||
{ | ||
} | ||
|
||
#[unstable(issue = "none", feature = "endless")] | ||
unsafe impl<I, U, F> EndlessIterator for FlatMap<I, U, F> | ||
where | ||
I: EndlessOrExact, | ||
U: IntoIterator, | ||
U::IntoIter: EndlessIterator, | ||
F: FnMut(I::Item) -> U, | ||
{ | ||
} | ||
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. Some of the bounds here seem redundant. If the inner iterators are |
||
|
||
/// An iterator that flattens one level of nesting in an iterator of things | ||
/// that can be turned into iterators. | ||
/// | ||
|
@@ -262,6 +283,22 @@ where | |
{ | ||
} | ||
|
||
#[unstable(issue = "none", feature = "endless")] | ||
unsafe impl<I> EndlessIterator for Flatten<I> | ||
where | ||
I: EndlessIterator, | ||
I::Item: EndlessOrExact, | ||
{ | ||
} | ||
|
||
#[unstable(issue = "none", feature = "endless")] | ||
unsafe impl<I> EndlessIterator for Flatten<I> | ||
where | ||
I: EndlessOrExact, | ||
I::Item: EndlessIterator, | ||
{ | ||
} | ||
|
||
/// Real logic of both `Flatten` and `FlatMap` which simply delegate to | ||
/// this type. | ||
#[derive(Clone, Debug)] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use crate::cmp; | ||
use crate::fmt::{self, Debug}; | ||
use crate::iter::traits::EndlessIterator; | ||
use crate::iter::{DoubleEndedIterator, ExactSizeIterator, FusedIterator, Iterator}; | ||
use crate::iter::{InPlaceIterable, SourceIter, TrustedLen}; | ||
|
||
|
@@ -435,6 +436,12 @@ where | |
// (without calling next()) would be needed to properly drop the remainder of the source. | ||
unsafe impl<A: InPlaceIterable, B: Iterator> InPlaceIterable for Zip<A, B> where A::Item: Copy {} | ||
|
||
#[unstable(issue = "none", feature = "endless")] | ||
unsafe impl<A: EndlessIterator, B: Iterator> EndlessIterator for Zip<A, B> {} | ||
|
||
#[unstable(issue = "none", feature = "endless")] | ||
unsafe impl<A: Iterator, B: EndlessIterator> EndlessIterator for Zip<A, B> {} | ||
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. If one is endless and the other is finite then the resulting |
||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<A: Debug, B: Debug> Debug for Zip<A, B> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,3 +72,26 @@ pub unsafe trait InPlaceIterable: Iterator {} | |
#[unstable(feature = "trusted_step", issue = "85731")] | ||
#[rustc_specialization_trait] | ||
pub unsafe trait TrustedStep: Step {} | ||
|
||
/// An iterator that is either empty or endless. | ||
/// | ||
/// The iterator reports a size hint that is either `0, Some(0)` or `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. Does it also require the size hint to be correct at at all times? Because Also note that generally 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. You had be worried for a second here -- I think you mean If 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 meant |
||
/// Since this is used to implement `ExactSizeIterator` for some adapters, iterators that | ||
/// are only empty should not implement this. | ||
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 don't quite understand this one. Implementing 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. This can be used to implement |
||
/// | ||
/// # Safety | ||
/// | ||
/// The implementation must either be endless or empty. | ||
#[unstable(issue = "none", feature = "endless")] | ||
#[marker] | ||
pub unsafe trait EndlessIterator: Iterator {} | ||
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. Do you intend to add specializations or implement other unsafe traits based on this one? Otherwise I can't see any unsafe use of that trait in the current PR. Also, the name is confusing if it can be either empty or endless. I had to revise my reasoning several times after remembering that "endless" here can also mean empty. |
||
|
||
#[unstable(issue = "none", feature = "endless")] | ||
#[marker] | ||
pub trait EndlessOrExact: Iterator {} | ||
|
||
#[unstable(issue = "none", feature = "endless")] | ||
impl<T: EndlessIterator> EndlessOrExact for T {} | ||
|
||
#[unstable(issue = "none", feature = "endless")] | ||
impl<T: ExactSizeIterator> EndlessOrExact for T {} |
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.
Similar to the
Zip
issue: If A is non-empty but finite and B is empty then then Chain is non-empty and finite.