Rollup merge of #149381 - yotamofek:pr/library/btree-iter-trustedlen, r=Amanieu

Add `impl TrustedLen` on `BTree{Map,Set}` iterators

https://github.com/rust-lang/rust/pull/149125#issuecomment-3556865052

r? `@Amanieu` , or re-assign
This commit is contained in:
Matthias Krüger 2025-11-27 20:07:15 +01:00 committed by GitHub
commit 66c0e1c06a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 2 deletions

View file

@ -3,7 +3,7 @@ use core::cmp::Ordering;
use core::error::Error;
use core::fmt::{self, Debug};
use core::hash::{Hash, Hasher};
use core::iter::FusedIterator;
use core::iter::{FusedIterator, TrustedLen};
use core::marker::PhantomData;
use core::mem::{self, ManuallyDrop};
use core::ops::{Bound, Index, RangeBounds};
@ -1624,6 +1624,9 @@ impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<K, V> TrustedLen for Iter<'_, K, V> {}
#[stable(feature = "rust1", since = "1.0.0")]
impl<K, V> Clone for Iter<'_, K, V> {
fn clone(&self) -> Self {
@ -1696,6 +1699,9 @@ impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<K, V> TrustedLen for IterMut<'_, K, V> {}
#[stable(feature = "fused", since = "1.26.0")]
impl<K, V> FusedIterator for IterMut<'_, K, V> {}
@ -1817,6 +1823,9 @@ impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoIter<K, V, A> {
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<K, V, A: Allocator + Clone> TrustedLen for IntoIter<K, V, A> {}
#[stable(feature = "fused", since = "1.26.0")]
impl<K, V, A: Allocator + Clone> FusedIterator for IntoIter<K, V, A> {}
@ -1865,6 +1874,9 @@ impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<K, V> TrustedLen for Keys<'_, K, V> {}
#[stable(feature = "fused", since = "1.26.0")]
impl<K, V> FusedIterator for Keys<'_, K, V> {}
@ -1920,6 +1932,9 @@ impl<K, V> ExactSizeIterator for Values<'_, K, V> {
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<K, V> TrustedLen for Values<'_, K, V> {}
#[stable(feature = "fused", since = "1.26.0")]
impl<K, V> FusedIterator for Values<'_, K, V> {}
@ -2160,6 +2175,9 @@ impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<K, V> TrustedLen for ValuesMut<'_, K, V> {}
#[stable(feature = "fused", since = "1.26.0")]
impl<K, V> FusedIterator for ValuesMut<'_, K, V> {}
@ -2222,6 +2240,9 @@ impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoKeys<K, V, A> {
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<K, V, A: Allocator + Clone> TrustedLen for IntoKeys<K, V, A> {}
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
impl<K, V, A: Allocator + Clone> FusedIterator for IntoKeys<K, V, A> {}
@ -2273,6 +2294,9 @@ impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoValues<K, V, A> {
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<K, V, A: Allocator + Clone> TrustedLen for IntoValues<K, V, A> {}
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
impl<K, V, A: Allocator + Clone> FusedIterator for IntoValues<K, V, A> {}

View file

@ -3,7 +3,7 @@ use core::cmp::Ordering::{self, Equal, Greater, Less};
use core::cmp::{max, min};
use core::fmt::{self, Debug};
use core::hash::{Hash, Hasher};
use core::iter::{FusedIterator, Peekable};
use core::iter::{FusedIterator, Peekable, TrustedLen};
use core::mem::ManuallyDrop;
use core::ops::{BitAnd, BitOr, BitXor, Bound, RangeBounds, Sub};
@ -1753,6 +1753,7 @@ impl<T> Clone for Iter<'_, T> {
Iter { iter: self.iter.clone() }
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;
@ -1783,12 +1784,14 @@ impl<'a, T> Iterator for Iter<'a, T> {
self.next_back()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
fn next_back(&mut self) -> Option<&'a T> {
self.iter.next_back()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ExactSizeIterator for Iter<'_, T> {
fn len(&self) -> usize {
@ -1796,6 +1799,9 @@ impl<T> ExactSizeIterator for Iter<'_, T> {
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<T> TrustedLen for Iter<'_, T> {}
#[stable(feature = "fused", since = "1.26.0")]
impl<T> FusedIterator for Iter<'_, T> {}
@ -1832,6 +1838,7 @@ impl<T, A: Allocator + Clone> DoubleEndedIterator for IntoIter<T, A> {
self.iter.next_back().map(|(k, _)| k)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T, A: Allocator + Clone> ExactSizeIterator for IntoIter<T, A> {
fn len(&self) -> usize {
@ -1839,6 +1846,9 @@ impl<T, A: Allocator + Clone> ExactSizeIterator for IntoIter<T, A> {
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<T, A: Allocator + Clone> TrustedLen for IntoIter<T, A> {}
#[stable(feature = "fused", since = "1.26.0")]
impl<T, A: Allocator + Clone> FusedIterator for IntoIter<T, A> {}