-
Notifications
You must be signed in to change notification settings - Fork 19
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 Default
impls for iterators and adapters
#77
Comments
That sounds to me like this should perhaps be an RFC, since it substantially effects ecosystem crates too. (For example, https://docs.rs/ndarray/latest/ndarray/iter/struct.ExactChunksIterMut.html and https://docs.rs/itertools/latest/itertools/structs/struct.TupleWindows.html don't implement But we'll see what libs-api says. |
Eh. The "should" is more a recommendation to improve ergonomics, not as a requirement. Just as one "should" implement a |
This one's insta-stable (right?) so it will need an FCP but my preliminary reaction is that this seems like a good idea. Seconded |
I think the general rule of thumb should be that if an iterator type has a reasonable invariant of yielding at least a single element, its |
Imo that's just one possible interpretation what |
Yes, I agree that it's better to leave it unimplemented if there's any ambiguity. I just meant that IMHO it's better to not impl |
Implement Default for some alloc/core iterators Add `Default` impls to the following collection iterators: * slice::{Iter, IterMut} * binary_heap::IntoIter * btree::map::{Iter, IterMut, Keys, Values, Range, IntoIter, IntoKeys, IntoValues} * btree::set::{Iter, IntoIter, Range} * linked_list::IntoIter * vec::IntoIter and these adapters: * adapters::{Chain, Cloned, Copied, Rev, Enumerate, Flatten, Fuse, Rev} For iterators which are generic over allocators it only implements it for the global allocator because we can't conjure an allocator from nothing or would have to turn the allocator field into an `Option` just for this change. These changes will be insta-stable. ACP: rust-lang/libs-team#77
Implement Default for some alloc/core iterators Add `Default` impls to the following collection iterators: * slice::{Iter, IterMut} * binary_heap::IntoIter * btree::map::{Iter, IterMut, Keys, Values, Range, IntoIter, IntoKeys, IntoValues} * btree::set::{Iter, IntoIter, Range} * linked_list::IntoIter * vec::IntoIter and these adapters: * adapters::{Chain, Cloned, Copied, Rev, Enumerate, Flatten, Fuse, Rev} For iterators which are generic over allocators it only implements it for the global allocator because we can't conjure an allocator from nothing or would have to turn the allocator field into an `Option` just for this change. These changes will be insta-stable. ACP: rust-lang/libs-team#77
Implement Default for some alloc/core iterators Add `Default` impls to the following collection iterators: * slice::{Iter, IterMut} * binary_heap::IntoIter * btree::map::{Iter, IterMut, Keys, Values, Range, IntoIter, IntoKeys, IntoValues} * btree::set::{Iter, IntoIter, Range} * linked_list::IntoIter * vec::IntoIter and these adapters: * adapters::{Chain, Cloned, Copied, Rev, Enumerate, Flatten, Fuse, Rev} For iterators which are generic over allocators it only implements it for the global allocator because we can't conjure an allocator from nothing or would have to turn the allocator field into an `Option` just for this change. These changes will be insta-stable. ACP: rust-lang/libs-team#77
Has been implemented |
Implement Default for some alloc/core iterators Add `Default` impls to the following collection iterators: * slice::{Iter, IterMut} * binary_heap::IntoIter * btree::map::{Iter, IterMut, Keys, Values, Range, IntoIter, IntoKeys, IntoValues} * btree::set::{Iter, IntoIter, Range} * linked_list::IntoIter * vec::IntoIter and these adapters: * adapters::{Chain, Cloned, Copied, Rev, Enumerate, Flatten, Fuse, Rev} For iterators which are generic over allocators it only implements it for the global allocator because we can't conjure an allocator from nothing or would have to turn the allocator field into an `Option` just for this change. These changes will be insta-stable. ACP: rust-lang/libs-team#77
Implement Default for some alloc/core iterators Add `Default` impls to the following collection iterators: * slice::{Iter, IterMut} * binary_heap::IntoIter * btree::map::{Iter, IterMut, Keys, Values, Range, IntoIter, IntoKeys, IntoValues} * btree::set::{Iter, IntoIter, Range} * linked_list::IntoIter * vec::IntoIter and these adapters: * adapters::{Chain, Cloned, Copied, Rev, Enumerate, Flatten, Fuse, Rev} For iterators which are generic over allocators it only implements it for the global allocator because we can't conjure an allocator from nothing or would have to turn the allocator field into an `Option` just for this change. These changes will be insta-stable. ACP: rust-lang/libs-team#77
Implement Default for some alloc/core iterators Add `Default` impls to the following collection iterators: * slice::{Iter, IterMut} * binary_heap::IntoIter * btree::map::{Iter, IterMut, Keys, Values, Range, IntoIter, IntoKeys, IntoValues} * btree::set::{Iter, IntoIter, Range} * linked_list::IntoIter * vec::IntoIter and these adapters: * adapters::{Chain, Cloned, Copied, Rev, Enumerate, Flatten, Fuse, Rev} For iterators which are generic over allocators it only implements it for the global allocator because we can't conjure an allocator from nothing or would have to turn the allocator field into an `Option` just for this change. These changes will be insta-stable. ACP: rust-lang/libs-team#77
Proposal
Problem statement
Currently many iterators don't implement
Default
which means one can'tderive(Default)
structs containing them,mem::take()
orCell::take
them.Motivation, use-cases
https://github.com/rust-lang/rust/blob/dc2d232c7485c60dd856f8b9aee83426492d4661/library/alloc/src/vec/drain.rs#L129
could be replaced with
Solution sketches
Iterator sources should implement
Default
when it is unambiguous that the default should be an empty iterator. Examples:slice::Iter{Mut}
vec::IntoIter
iter::Empty
(already implements it)btree_map::Iter
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: Default
Flatten<I> where I: Default
Chain<A, B> where A: Default, B: Default
Examples where it should not be implemented
array::IntoIter
since one could expect[T; N]::default().into_iter()
whereT: Default
iter::Once
since one could expectiter::once(Default::default())
adapters::Map
since it would require summoning an FnMut ex nihiloLinks and related work
Default
foriter::Empty
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.
The text was updated successfully, but these errors were encountered: