Skip to content
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

Enable draining iterator/stream elements into a () #94166

Closed
wants to merge 2 commits 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
6 changes: 3 additions & 3 deletions library/core/src/iter/traits/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,11 @@ pub trait Extend<A> {
}

#[stable(feature = "extend_for_unit", since = "1.28.0")]
impl Extend<()> for () {
fn extend<T: IntoIterator<Item = ()>>(&mut self, iter: T) {
impl<T> Extend<T> for () {
fn extend<A: IntoIterator<Item = T>>(&mut self, iter: A) {
iter.into_iter().for_each(drop)
}
fn extend_one(&mut self, _item: ()) {}
fn extend_one(&mut self, _item: T) {}
}

#[stable(feature = "extend_for_tuple", since = "1.56.0")]
Expand Down
13 changes: 7 additions & 6 deletions library/core/src/unit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::iter::FromIterator;

/// Collapses all unit items from an iterator into one.
/// Drains all items from an iterator.
///
/// This is more useful when combined with higher-level abstractions, like
/// collecting to a `Result<(), E>` where you only care about errors:
/// This is useful to run an iterator to completion when you don't
/// care about the result, or to collect into a `Result<(), E>` when
/// you only care about errors:
///
/// ```
/// use std::io::*;
Expand All @@ -14,8 +15,8 @@ use crate::iter::FromIterator;
/// assert!(res.is_ok());
/// ```
#[stable(feature = "unit_from_iter", since = "1.23.0")]
impl FromIterator<()> for () {
fn from_iter<I: IntoIterator<Item = ()>>(iter: I) -> Self {
iter.into_iter().for_each(|()| {})
impl<T> FromIterator<T> for () {
fn from_iter<A: IntoIterator<Item = T>>(iter: A) -> Self {
iter.into_iter().for_each(|_| {})
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
iter.into_iter().for_each(|_| {})
iter.into_iter().for_each(drop)

Maybe this should also use .for_each(drop) to clarify the intent better? It doesn't matter much for (), but for generic T that seems better.

}
}