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
Generalize the various chunk methods to produce any collection that implements Default + Extend #2498
base: master
Are you sure you want to change the base?
Conversation
If a |
Also, since there is no |
Only the former is implementable today. The latter would require a (stablized) trait for collection lengths. Since the caller already has to cope with undersized collections (at least for the final item with I don't think there's any need to gate this on |
Regarding Perhaps something like "Depending on collection type, collections of less than |
Yes I think we would document that N items are consumed and if your collection's Extend impl can result in replacement rather than addition (like *Set/Map can) then the yielded collection will have fewer than N items. If you do want collections of N items no matter what you could implement chunks yourself with scan. It's a bit yucky but it's doable. e.g. my_stream.map(Some).chain(None).scan(HashMap::new(), |map, item| {
match item {
// End of the stream, yield whatever partial collection is left.
None => Some(Some(mem::take(map))),
Some(item) => {
map.extend(item);
if map.len() >= DESIRED_N {
return Some(Some(mem::take(map)));
}
Some(None)
}
}
}).filter_map(|x| x) |
…mplements Default + Extend. Fixes rust-lang#2492
Fixes #2492