From 248c8b96a0a96190df2cefc784d0586a11b4b5a4 Mon Sep 17 00:00:00 2001 From: GrayJack Date: Mon, 25 Nov 2024 21:06:01 -0300 Subject: [PATCH] feat: Specialize `Iterator::count` when for `Drain`, `IntoIter` and `Iter` --- src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 511f272..06cd1f6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1073,6 +1073,11 @@ impl Iterator for Drain<'_, T, N> { let len = self.inner.len(); (len, Some(len)) } + + fn count(self) -> usize + where Self: Sized { + self.len() + } } impl ExactSizeIterator for Drain<'_, T, N> { @@ -1136,6 +1141,11 @@ impl Iterator for IntoIter { let len = self.0.len(); (len, Some(len)) } + + fn count(self) -> usize + where Self: Sized { + self.len() + } } impl DoubleEndedIterator for IntoIter { @@ -1158,6 +1168,7 @@ impl DoubleEndedIterator for IntoIter { impl FusedIterator for IntoIter {} impl ExactSizeIterator for IntoIter { + #[inline] fn len(&self) -> usize { self.0.len() } @@ -1224,6 +1235,11 @@ impl Iterator for Iter<'_, T, N> { let len = self.borrow.len() - self.passed_count; (len, Some(len)) } + + fn count(self) -> usize + where Self: Sized { + self.len() + } } impl DoubleEndedIterator for Iter<'_, T, N> { @@ -1244,6 +1260,7 @@ impl DoubleEndedIterator for Iter<'_, T, N> { impl FusedIterator for Iter<'_, T, N> {} impl ExactSizeIterator for Iter<'_, T, N> { + #[inline] fn len(&self) -> usize { self.borrow.len() - self.passed_count }