Skip to content

Commit 085d885

Browse files
authored
Rollup merge of rust-lang#81599 - sdroege:fuse-trusted-len, r=m-ou-se
Implement `TrustedLen` for `Fuse<I: TrustedLen>` This looks like it was simply forgotten.
2 parents a06716b + 12b605a commit 085d885

File tree

1 file changed

+14
-1
lines changed
  • library/core/src/iter/adapters

1 file changed

+14
-1
lines changed

library/core/src/iter/adapters/fuse.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::intrinsics;
22
use crate::iter::adapters::{zip::try_get_unchecked, InPlaceIterable, SourceIter};
3-
use crate::iter::{DoubleEndedIterator, ExactSizeIterator, FusedIterator, TrustedRandomAccess};
3+
use crate::iter::{
4+
DoubleEndedIterator, ExactSizeIterator, FusedIterator, TrustedLen, TrustedRandomAccess,
5+
};
46
use crate::ops::Try;
57

68
/// An iterator that yields `None` forever after the underlying iterator
@@ -182,8 +184,19 @@ where
182184
}
183185
}
184186

187+
#[unstable(feature = "trusted_len", issue = "37572")]
188+
// SAFETY: `TrustedLen` requires that an accurate length is reported via `size_hint()`. As `Fuse`
189+
// is just forwarding this to the wrapped iterator `I` this property is preserved and it is safe to
190+
// implement `TrustedLen` here.
191+
unsafe impl<I> TrustedLen for Fuse<I> where I: TrustedLen {}
192+
185193
#[doc(hidden)]
186194
#[unstable(feature = "trusted_random_access", issue = "none")]
195+
// SAFETY: `TrustedRandomAccess` requires that `size_hint()` must be exact and cheap to call, and
196+
// `Iterator::__iterator_get_unchecked()` must be implemented accordingly.
197+
//
198+
// This is safe to implement as `Fuse` is just forwarding these to the wrapped iterator `I`, which
199+
// preserves these properties.
187200
unsafe impl<I> TrustedRandomAccess for Fuse<I>
188201
where
189202
I: TrustedRandomAccess,

0 commit comments

Comments
 (0)