From d8ceb2ddf82455a8bdc82d9733e86d8c86195fb5 Mon Sep 17 00:00:00 2001 From: pmendes Date: Thu, 18 Apr 2024 14:26:28 +0100 Subject: [PATCH] Add ExactSizeIterator implementation to Chain --- library/core/src/iter/adapters/chain.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/library/core/src/iter/adapters/chain.rs b/library/core/src/iter/adapters/chain.rs index bcaac2f42cf04..aae80d6db1214 100644 --- a/library/core/src/iter/adapters/chain.rs +++ b/library/core/src/iter/adapters/chain.rs @@ -273,6 +273,18 @@ where { } +#[stable(feature = "impl_exact_size_iterator_for_chain", since = "CURRENT_RUSTC_VERSION")] +impl ExactSizeIterator for Chain +where + A: ExactSizeIterator, + B: ExactSizeIterator, +{ + fn len(&self) -> usize { + self.a.as_ref().map(|i| i.len()).unwrap_or_default() + + self.b.as_ref().map(|i| i.len()).unwrap_or_default() + } +} + #[stable(feature = "default_iters", since = "1.70.0")] impl Default for Chain { /// Creates a `Chain` from the default values for `A` and `B`.