-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Avoid unused clones in Cloned<I>
and Copied<I>
#139745
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
Conversation
I was trying to implement |
The easiest way is to use try { it.try_find(..)?.cloned() } |
Thanks. That's indeed simpler. Unfortunately, it's not the full missing piece to the puzzle. With that, it's this: impl<'a, I, T: 'a> Iterator for Cloned<I>
where
I: Iterator<Item = &'a T>,
T: Clone,
{
fn try_find<R>(
&mut self,
f: impl FnMut(&Self::Item) -> R,
) -> ChangeOutputType<R, Option<Self::Item>>
where
R: Try<Output = bool, Residual: Residual<Option<Self::Item>>>,
{
try { self.it.try_find(move |x| f(&x))?.cloned() }
}
} It yields this error:
This is the same error I was encountering with my draft version. I think it is equivalent or close to using try blocks: match self.it.try_find(move |x| f(&x)).branch() {
ControlFlow::Continue(x) => Try::from_output(x),
ControlFlow::Break(r) => FromResidual::from_residual(r),
} Upon further reflection, I think |
Avoid cloning in `Cloned<I>` or copying in `Copied<I>` when elements are only needed by reference or not at all. There is already some precedent for this, given that `__iterator_get_unchecked` is implemented, which can skip elements. The reduced clones are technically observable by a user impl of `Clone`.
8a6d677
to
ed5f31a
Compare
Ah, yes, that's true... I don't think this needs libs-api approval, I'd consider these changes as implementation details. So: |
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#139745 (Avoid unused clones in `Cloned<I>` and `Copied<I>`) - rust-lang#139757 (opt-dist: use executable-extension for host llvm-profdata) - rust-lang#139778 (Add test for issue 34834) - rust-lang#139783 (Use `compiletest-ignore-dir` for bootstrap self-tests) - rust-lang#139797 (Allow (but don't require) `#[unsafe(naked)]` so that `compiler-builtins` can upgrade to it) - rust-lang#139799 (Specify `--print info=file` syntax in `--help`) - rust-lang#139811 (Use `newtype_index!`-generated types more idiomatically) - rust-lang#139813 (Miri subtree update) r? `@ghost` `@rustbot` modify labels: rollup
…, r=joboet Avoid unused clones in `Cloned<I>` and `Copied<I>` Avoid cloning in `Cloned<I>` or copying in `Copied<I>` when elements are only needed by reference or not at all. There is already some precedent for this, given that `__iterator_get_unchecked` is implemented, which can skip elements. The reduced clones are technically observable by a user impl of `Clone`. r? libs-api
Rollup of 12 pull requests Successful merges: - rust-lang#138374 (Enable contracts for const functions) - rust-lang#138380 (ci: add runners for vanilla LLVM 20) - rust-lang#138393 (Allow const patterns of matches to contain pattern types) - rust-lang#139393 (rustdoc-json: Output target feature information) - rust-lang#139517 (std: sys: process: uefi: Use NULL stdin by default) - rust-lang#139554 (std: add Output::exit_ok) - rust-lang#139745 (Avoid unused clones in `Cloned<I>` and `Copied<I>`) - rust-lang#139757 (opt-dist: use executable-extension for host llvm-profdata) - rust-lang#139778 (Add test for issue 34834) - rust-lang#139783 (Use `compiletest-ignore-dir` for bootstrap self-tests) - rust-lang#139789 (do not unnecessarily leak auto traits in item bounds) - rust-lang#139791 (drop global where-bounds before merging candidates) r? `@ghost` `@rustbot` modify labels: rollup
…, r=joboet Avoid unused clones in `Cloned<I>` and `Copied<I>` Avoid cloning in `Cloned<I>` or copying in `Copied<I>` when elements are only needed by reference or not at all. There is already some precedent for this, given that `__iterator_get_unchecked` is implemented, which can skip elements. The reduced clones are technically observable by a user impl of `Clone`. r? libs-api
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#139745 (Avoid unused clones in `Cloned<I>` and `Copied<I>`) - rust-lang#139757 (opt-dist: use executable-extension for host llvm-profdata) - rust-lang#139778 (Add test for issue 34834) - rust-lang#139783 (Use `compiletest-ignore-dir` for bootstrap self-tests) - rust-lang#139797 (Allow (but don't require) `#[unsafe(naked)]` so that `compiler-builtins` can upgrade to it) - rust-lang#139799 (Specify `--print info=file` syntax in `--help`) - rust-lang#139811 (Use `newtype_index!`-generated types more idiomatically) - rust-lang#139813 (Miri subtree update) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#139745 - thaliaarchi:iter-unused-clone-copy, r=joboet Avoid unused clones in `Cloned<I>` and `Copied<I>` Avoid cloning in `Cloned<I>` or copying in `Copied<I>` when elements are only needed by reference or not at all. There is already some precedent for this, given that `__iterator_get_unchecked` is implemented, which can skip elements. The reduced clones are technically observable by a user impl of `Clone`. r? libs-api
…, r=joboet Avoid unused clones in `Cloned<I>` and `Copied<I>` Avoid cloning in `Cloned<I>` or copying in `Copied<I>` when elements are only needed by reference or not at all. There is already some precedent for this, given that `__iterator_get_unchecked` is implemented, which can skip elements. The reduced clones are technically observable by a user impl of `Clone`. r? libs-api
Avoid cloning in
Cloned<I>
or copying inCopied<I>
when elements are only needed by reference or not at all. There is already some precedent for this, given that__iterator_get_unchecked
is implemented, which can skip elements. The reduced clones are technically observable by a user impl ofClone
.r? libs-api