Skip to content

Commit 451e86c

Browse files
committed
simplify TrustedLen impls
Implement on FlattenCompat and delegate from Flatten and FlatMap.
1 parent a7aa205 commit 451e86c

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

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

+28-34
Original file line numberDiff line numberDiff line change
@@ -136,26 +136,12 @@ where
136136
}
137137

138138
#[unstable(feature = "trusted_len", issue = "37572")]
139-
unsafe impl<T, I, F, const N: usize> TrustedLen for FlatMap<I, [T; N], F>
139+
unsafe impl<I, U, F> TrustedLen for FlatMap<I, U, F>
140140
where
141-
I: TrustedLen,
142-
F: FnMut(I::Item) -> [T; N],
143-
{
144-
}
145-
146-
#[unstable(feature = "trusted_len", issue = "37572")]
147-
unsafe impl<'a, T, I, F, const N: usize> TrustedLen for FlatMap<I, &'a [T; N], F>
148-
where
149-
I: TrustedLen,
150-
F: FnMut(I::Item) -> &'a [T; N],
151-
{
152-
}
153-
154-
#[unstable(feature = "trusted_len", issue = "37572")]
155-
unsafe impl<'a, T, I, F, const N: usize> TrustedLen for FlatMap<I, &'a mut [T; N], F>
156-
where
157-
I: TrustedLen,
158-
F: FnMut(I::Item) -> &'a mut [T; N],
141+
I: Iterator,
142+
U: IntoIterator,
143+
F: FnMut(I::Item) -> U,
144+
FlattenCompat<Map<I, F>, <U as IntoIterator>::IntoIter>: TrustedLen,
159145
{
160146
}
161147

@@ -298,8 +284,8 @@ where
298284
#[unstable(feature = "trusted_len", issue = "37572")]
299285
unsafe impl<I> TrustedLen for Flatten<I>
300286
where
301-
I: TrustedLen,
302-
<I as Iterator>::Item: TrustedConstSize,
287+
I: Iterator<Item: IntoIterator>,
288+
FlattenCompat<I, <I::Item as IntoIterator>::IntoIter>: TrustedLen,
303289
{
304290
}
305291

@@ -660,6 +646,27 @@ where
660646
}
661647
}
662648

649+
unsafe impl<const N: usize, I, T> TrustedLen
650+
for FlattenCompat<I, <[T; N] as IntoIterator>::IntoIter>
651+
where
652+
I: TrustedLen<Item = [T; N]>,
653+
{
654+
}
655+
656+
unsafe impl<'a, const N: usize, I, T> TrustedLen
657+
for FlattenCompat<I, <&'a [T; N] as IntoIterator>::IntoIter>
658+
where
659+
I: TrustedLen<Item = &'a [T; N]>,
660+
{
661+
}
662+
663+
unsafe impl<'a, const N: usize, I, T> TrustedLen
664+
for FlattenCompat<I, <&'a mut [T; N] as IntoIterator>::IntoIter>
665+
where
666+
I: TrustedLen<Item = &'a mut [T; N]>,
667+
{
668+
}
669+
663670
trait ConstSizeIntoIterator: IntoIterator {
664671
// FIXME(#31844): convert to an associated const once specialization supports that
665672
fn size() -> Option<usize>;
@@ -696,19 +703,6 @@ impl<T, const N: usize> ConstSizeIntoIterator for &mut [T; N] {
696703
}
697704
}
698705

699-
#[doc(hidden)]
700-
#[unstable(feature = "std_internals", issue = "none")]
701-
// FIXME(#20400): Instead of this helper trait there should be multiple impl TrustedLen for Flatten<>
702-
// blocks with different bounds on Iterator::Item but the compiler erroneously considers them overlapping
703-
pub unsafe trait TrustedConstSize: IntoIterator {}
704-
705-
#[unstable(feature = "std_internals", issue = "none")]
706-
unsafe impl<T, const N: usize> TrustedConstSize for [T; N] {}
707-
#[unstable(feature = "std_internals", issue = "none")]
708-
unsafe impl<T, const N: usize> TrustedConstSize for &'_ [T; N] {}
709-
#[unstable(feature = "std_internals", issue = "none")]
710-
unsafe impl<T, const N: usize> TrustedConstSize for &'_ mut [T; N] {}
711-
712706
#[inline]
713707
fn and_then_or_clear<T, U>(opt: &mut Option<T>, f: impl FnOnce(&mut T) -> Option<U>) -> Option<U> {
714708
let x = f(opt.as_mut()?);

0 commit comments

Comments
 (0)