Add Default impls for iterators and adapters (original) (raw)
Proposal
Problem statement
Currently many iterators don't implement Default which means one can't derive(Default) structs containing them, mem::take() or Cell::take them.
Motivation, use-cases
let iter = mem::replace(&mut self.iter, (&mut []).iter());could be replaced with
let iter = mem::take(&mut self.iter);Solution sketches
Iterator sources should implement Default when it is unambiguous that the default should be an empty iterator. Examples:
slice::Iter{Mut}vec::IntoIteriter::Empty(already implements it)btree_map::Iter- etc.
Adapters should implement it when the inner iterator does and when they don't have any closure that would have to be materialized
Skip<I> where I: DefaultFlatten<I> where I: DefaultChain<A, B> where A: Default, B: Default
Examples where it should not be implemented
array::IntoItersince one could expect[T; N]::default().into_iter()whereT: Defaultiter::Oncesince one could expectiter::once(Default::default())adapters::Mapsince it would require summoning an FnMut ex nihilo
Links and related work
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.