Skip to content

Make core::iter::Fuse fuse all iterators #102006

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 10 additions & 2 deletions library/core/src/iter/adapters/fuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::ops::Try;
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Fuse<I> {
// NOTE: for `I: FusedIterator`, we never bother setting `None`, but
// NOTE: for `I: TrustedFusedIterator`, we never bother setting `None`, but
// we still have to be prepared for that state due to variance.
// See rust-lang/rust#85863
iter: Option<I>,
Expand All @@ -26,6 +26,14 @@ impl<I> Fuse<I> {
}
}

// Safety: only implemented by core::iter::Fuse to specialize on nested Fuse<Fuse<...>>
#[rustc_unsafe_specialization_marker]
unsafe trait TrustedFusedIterator: Iterator {}

// Fuse properly fuses iterators when it's specialized on this trait
// only if that specialization is correct is this impl correct
unsafe impl<I> TrustedFusedIterator for Fuse<I> where I: Iterator {}

#[stable(feature = "fused", since = "1.26.0")]
impl<I> FusedIterator for Fuse<I> where I: Iterator {}

Expand Down Expand Up @@ -330,7 +338,7 @@ where
#[doc(hidden)]
impl<I> FuseImpl<I> for Fuse<I>
where
I: FusedIterator,
I: TrustedFusedIterator,
{
#[inline]
fn next(&mut self) -> Option<<I as Iterator>::Item> {
Expand Down
4 changes: 0 additions & 4 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1543,10 +1543,6 @@ pub trait Iterator {
/// [`Some(T)`] again. `fuse()` adapts an iterator, ensuring that after a
/// [`None`] is given, it will always return [`None`] forever.
///
/// Note that the [`Fuse`] wrapper is a no-op on iterators that implement
/// the [`FusedIterator`] trait. `fuse()` may therefore behave incorrectly
/// if the [`FusedIterator`] trait is improperly implemented.
///
/// [`Some(T)`]: Some
/// [`FusedIterator`]: crate::iter::FusedIterator
///
Expand Down
6 changes: 2 additions & 4 deletions library/core/src/iter/traits/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ use crate::iter::Step;
/// An iterator that always continues to yield `None` when exhausted.
///
/// Calling next on a fused iterator that has returned `None` once is guaranteed
/// to return [`None`] again. This trait should be implemented by all iterators
/// that behave this way because it allows optimizing [`Iterator::fuse()`].
/// to return [`None`] again.
///
/// Note: In general, you should not use `FusedIterator` in generic bounds if
/// you need a fused iterator. Instead, you should just call [`Iterator::fuse()`]
/// on the iterator. If the iterator is already fused, the additional [`Fuse`]
/// on the iterator. If the iterator is already a [`Fuse`], the additional [`Fuse`]
/// wrapper will be a no-op with no performance penalty.
///
/// [`Fuse`]: crate::iter::Fuse
#[stable(feature = "fused", since = "1.26.0")]
#[rustc_unsafe_specialization_marker]
pub trait FusedIterator: Iterator {}

#[stable(feature = "fused", since = "1.26.0")]
Expand Down