forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let
try_collect
take advantage of try_fold
overrides
Without this change it's going through `&mut impl Iterator`, which handles `?Sized` and thus currently can't forward generics. Here's the test added, to see that it fails before this PR (once a new enough nightly is out): https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=462f2896f2fed2c238ee63ca1a7e7c56
- Loading branch information
Showing
5 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::ops::Try; | ||
|
||
/// Like `Iterator::by_ref`, but requiring `Sized` so it can forward generics. | ||
/// | ||
/// Ideally this will no longer be required, eventually, but as can be seen in | ||
/// the benchmarks (as of Feb 2022 at least) `by_ref` can have performance cost. | ||
pub(crate) struct ByRefSized<'a, I>(pub &'a mut I); | ||
|
||
impl<I: Iterator> Iterator for ByRefSized<'_, I> { | ||
type Item = I::Item; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.0.next() | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.0.size_hint() | ||
} | ||
|
||
fn advance_by(&mut self, n: usize) -> Result<(), usize> { | ||
self.0.advance_by(n) | ||
} | ||
|
||
fn nth(&mut self, n: usize) -> Option<Self::Item> { | ||
self.0.nth(n) | ||
} | ||
|
||
fn fold<B, F>(self, init: B, f: F) -> B | ||
where | ||
F: FnMut(B, Self::Item) -> B, | ||
{ | ||
self.0.fold(init, f) | ||
} | ||
|
||
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R | ||
where | ||
F: FnMut(B, Self::Item) -> R, | ||
R: Try<Output = B>, | ||
{ | ||
self.0.try_fold(init, f) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters