-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add Iterator::join to combine Iterator and Join #75738
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 | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,40 @@ | ||||||||||||||||||||||||||||||||||
//! Allocation extensions for [`Iterator`]. | ||||||||||||||||||||||||||||||||||
//! | ||||||||||||||||||||||||||||||||||
//! *[See also the Iterator trait][Iterator].* | ||||||||||||||||||||||||||||||||||
#![unstable(feature = "iterator_join", issue = "75638")] | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
use crate::slice::Join; | ||||||||||||||||||||||||||||||||||
use crate::vec::Vec; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/// Iterator extension traits that requires allocation. | ||||||||||||||||||||||||||||||||||
#[unstable(feature = "iterator_join", issue = "75638")] | ||||||||||||||||||||||||||||||||||
pub trait IteratorExt: 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.
Suggested change
I'd caution against calling this trait I'm really worried about the floodgates of future itertools breakage that this trait opens up. It's bad enough that the Itertools readme has to beg people not to contribute methods that could reside on
An |
||||||||||||||||||||||||||||||||||
/// Flattens an iterator into a single value with the given separator in | ||||||||||||||||||||||||||||||||||
/// between. | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// Combines `collect` with `join` to convert a sequence into a value | ||||||||||||||||||||||||||||||||||
/// separated with the specified separator. | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// Allows `.join(sep)` instead of `.collect::<Vec<_>>().join(sep)`. | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||
/// #![feature(iterator_join)] | ||||||||||||||||||||||||||||||||||
/// use alloc::iter::IteratorExt; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(["hello", "world"].iter().copied().join(" "), "hello world"); | ||||||||||||||||||||||||||||||||||
/// assert_eq!([[1, 2], [3, 4]].iter().copied().join(&0), [1, 2, 0, 3, 4]); | ||||||||||||||||||||||||||||||||||
/// assert_eq!([[1, 2], [3, 4]].iter().copied().join(&[0, 0][..]), [1, 2, 0, 0, 3, 4]); | ||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||
#[inline] | ||||||||||||||||||||||||||||||||||
#[unstable(feature = "iterator_join", issue = "75638")] | ||||||||||||||||||||||||||||||||||
#[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"] | ||||||||||||||||||||||||||||||||||
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 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. True |
||||||||||||||||||||||||||||||||||
fn join<Separator>(self, sep: Separator) -> <[Self::Item] as Join<Separator>>::Output | ||||||||||||||||||||||||||||||||||
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. Sigh. Here we go again... :-( Alternatively:
Suggested change
😉 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 recall that 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. Ugh. It's not solved. Stabilizing 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 about we do a breaking change in |
||||||||||||||||||||||||||||||||||
where | ||||||||||||||||||||||||||||||||||
[Self::Item]: Join<Separator>, | ||||||||||||||||||||||||||||||||||
Self: Sized, | ||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||
Join::join(self.collect::<Vec<Self::Item>>().as_slice(), sep) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
Comment on lines
+31
to
+38
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 looks like we're going to get
Suggested change
This method can be defined on 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. No, the reason why I don't want intersperse is that intersperse does not support joining with different types, it's the reason for this why I opened up this pull request, otherwise I will use intersperse instead. |
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
impl<T: Iterator + ?Sized> IteratorExt 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.
The
impl
seems to be missingThere 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.
Do we need to
impl IteratorExt
for all items thatimpl Iterator
? I thought this already does that? Do we need to do likeimpl IteratorExt for dyn Iterator
orimpl<T: Iterator> IteratorExt 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.
Without any
impl
, it's just specifying the supertrait relationship as a requirement to implementors. I would recommend a blanketimpl<T: Iterator + ?Sized>
, which will include thedyn
case.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.
I added
impl<T: ExactSizeIterator + ?Sized> IteratorExt for T {}