-
Notifications
You must be signed in to change notification settings - Fork 23
Description
Proposal
Problem statement
Sometimes it's useful to be able to create empty iterators without needing to first obtain an empty container to then iterate over, some of the existing iterator types implement Default
for this #77 but there are still quite a few missing.
Motivating examples or use cases
I'm currently writing a parser for parsing templates for command lines that are already split into a slice of arguments (written using the Default
impls I'd like to add):
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Token {
Char(char),
ArgSeparator,
}
#[derive(Clone, Debug, Default)]
struct Tokens<'a> {
current: std::str::Chars<'a>,
rest: std::slice::Iter<'a, &'a str>,
}
impl<'a> Tokens<'a> {
fn new(args: &'a [&'a str]) -> Self {
Self { rest: args, ..Self::default() }
}
}
impl Iterator for Tokens<'_> {
type Item = Token;
fn next(&mut self) -> Option<Self::Item> {
match self.current.next() {
Some(c) => Some(Token::Char(c)),
None => {
self.current = self.rest.next()?.chars();
Some(Token::ArgSeparator)
}
}
}
}
// code that uses Tokens...
Solution sketch
implement Default
for all iterators where the iterator is constructed from some type that has an obvious default value that's empty. So std::str::Chars
should be Default
since ""
is obviously empty. std::array::IntoIter
should not be Default
since the array type's default wouldn't be empty unless it has zero length. std::env::Args
should not be Default
since an obvious default value is the program's arguments which isn't usually empty.
The full list of iterator types: https://doc.rust-lang.org/nightly/std/iter/trait.Iterator.html#implementors
Iterator
implementations I found:
iterator implements Default
already
For list, see #656 (comment)
iterator should implement Default
core::ascii::EscapeDefault
core::char::EscapeDebug
core::char::EscapeDefault
core::char::EscapeUnicode
core::char::ToLowercase
core::char::ToUppercase
core::char::decode::DecodeUtf16
core::error::Source
core::ffi::c_str::Bytes
core::iter::adapters::array_chunks::ArrayChunks
core::iter::adapters::by_ref_sized::ByRefSized
core::iter::adapters::cycle::Cycle
core::iter::adapters::filter::Filter
core::iter::adapters::filter_map::FilterMap
core::iter::adapters::flatten::FlatMap
core::iter::adapters::inspect::Inspect
core::iter::adapters::intersperse::Intersperse
core::iter::adapters::intersperse::IntersperseWith
core::iter::adapters::map::Map
core::iter::adapters::map_while::MapWhile
core::iter::adapters::map_windows::MapWindows
core::iter::adapters::peekable::Peekable
core::iter::adapters::scan::Scan
core::iter::adapters::skip::Skip
core::iter::adapters::skip_while::SkipWhile
core::iter::adapters::step_by::StepBy
core::iter::adapters::take::Take
core::iter::adapters::take_while::TakeWhile
core::iter::adapters::zip::Zip
core::iter::sources::once::Once
core::iter::sources::once_with::OnceWith
core::option::IntoIter
core::option::Iter
core::option::IterMut
core::range::iter::IterRange
core::range::iter::IterRangeFrom
core::range::iter::IterRangeInclusive
core::result::IntoIter
core::result::Iter
core::result::IterMut
core::slice::ascii::EscapeAscii
core::slice::iter::ArrayWindows
core::slice::iter::ChunkBy
core::slice::iter::ChunkByMut
core::slice::iter::Chunks
core::slice::iter::ChunksExact
core::slice::iter::ChunksExactMut
core::slice::iter::ChunksMut
core::slice::iter::RChunks
core::slice::iter::RChunksExact
core::slice::iter::RChunksExactMut
core::slice::iter::RChunksMut
core::slice::iter::RSplit
core::slice::iter::RSplitMut
core::slice::iter::RSplitN
core::slice::iter::RSplitNMut
core::slice::iter::Split
core::slice::iter::SplitInclusive
core::slice::iter::SplitInclusiveMut
core::slice::iter::SplitMut
core::slice::iter::SplitN
core::slice::iter::SplitNMut
core::slice::iter::Windows
core::str::iter::Bytes
core::str::iter::CharIndices
core::str::iter::Chars
core::str::iter::EncodeUtf16
core::str::iter::EscapeDebug
core::str::iter::EscapeDefault
core::str::iter::EscapeUnicode
core::str::iter::Lines
core::str::iter::LinesAny
core::str::iter::MatchIndices
core::str::iter::Matches
core::str::iter::RMatchIndices
core::str::iter::RMatches
core::str::iter::RSplit
core::str::iter::RSplitN
core::str::iter::RSplitTerminator
core::str::iter::Split
core::str::iter::SplitAsciiWhitespace
core::str::iter::SplitInclusive
core::str::iter::SplitN
core::str::iter::SplitTerminator
core::str::iter::SplitWhitespace
core::str::lossy::Utf8Chunks
alloc::collections::binary_heap::IntoIterSorted
alloc::collections::btree::set::Difference
alloc::collections::btree::set::Intersection
alloc::collections::btree::set::SymmetricDifference
alloc::collections::btree::set::Union
alloc::collections::linked_list::ExtractIf
alloc::collections::vec_deque::into_iter::IntoIter
alloc::string::IntoChars
proc_macro::token_stream::IntoIter
std::collections::hash::set::Difference
std::collections::hash::set::Intersection
std::collections::hash::set::SymmetricDifference
std::collections::hash::set::Union
std::os::unix::net::ancillary::Messages
std::os::windows::ffi::EncodeWide
std::path::Ancestors
std::path::Components
std::path::Iter
std::process::CommandArgs
std::process::CommandEnvs
iterator could implement Default
but it might make the rest of the iterator slower (e.g. adding a conditional in Drop
)
alloc::collections::binary_heap::Drain
alloc::collections::binary_heap::DrainSorted
alloc::collections::btree::map::ExtractIf
alloc::collections::btree::set::ExtractIf
alloc::collections::vec_deque::drain::Drain
alloc::string::Drain
alloc::vec::drain::Drain
alloc::vec::extract_if::ExtractIf
alloc::vec::splice::Splice
std::collections::hash::map::Drain
std::collections::hash::map::ExtractIf
std::collections::hash::set::Drain
std::collections::hash::set::ExtractIf
std::os::unix::net::ancillary::ScmCredentials
std::os::unix::net::ancillary::ScmRights
iterator shouldn't implement Default
core::iter::sources::from_coroutine::FromCoroutine
core::iter::sources::from_fn::FromFn
core::iter::sources::repeat::Repeat
core::iter::sources::repeat_n::RepeatN
core::iter::sources::repeat_with::RepeatWith
core::iter::sources::successors::Successors
std::env::Args
std::env::ArgsOs
std::env::SplitPaths
std::env::Vars
std::env::VarsOs
std::fs::ReadDir
std::io::Bytes
std::io::Lines
std::io::Split
std::net::tcp::Incoming
std::net::tcp::IntoIncoming
std::os::unix::net::listener::Incoming
iterator could implement Default
but we maybe shouldn't
core::ops::range::RangeFrom
core::ops::range::RangeInclusive
std::sync::mpmc::IntoIter
std::sync::mpmc::Iter
std::sync::mpmc::TryIter
std::sync::mpsc::IntoIter
std::sync::mpsc::Iter
std::sync::mpsc::TryIter
Alternatives
Manually construct iterators from a manually-obtained empty source type and have to implement Default
manually.
Links and related work
We did some of this before in #77 but didn't end up handling all the iterator types.
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.