Skip to content

Commit 10acbf8

Browse files
authored
Merge pull request #3 from the8472/vec-spec-assoc-const
extract const guard to trait associated const for optimization experiment
2 parents 568fe20 + 6b99f05 commit 10acbf8

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

library/alloc/src/vec/source_iter_marker.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ where
2121
// a) no ZSTs as there would be no allocation to reuse and pointer arithmetic would panic
2222
// b) size match as required by Alloc contract
2323
// c) alignments match as required by Alloc contract
24-
if mem::size_of::<T>() == 0
25-
|| mem::size_of::<T>()
26-
!= mem::size_of::<<<I as SourceIter>::Source as AsIntoIter>::Item>()
27-
|| mem::align_of::<T>()
28-
!= mem::align_of::<<<I as SourceIter>::Source as AsIntoIter>::Item>()
29-
{
24+
if <I as LayoutMatcher>::MATCHES {
3025
// fallback to more generic implementations
3126
return SpecFromIterNested::from_iter(iterator);
3227
}
@@ -154,3 +149,18 @@ where
154149
len
155150
}
156151
}
152+
153+
trait LayoutMatcher {
154+
type IN;
155+
const MATCHES: bool;
156+
}
157+
158+
impl<I, OUT> LayoutMatcher for I
159+
where
160+
I: Iterator<Item = OUT> + SourceIter<Source: AsIntoIter>,
161+
{
162+
type IN = <<I as SourceIter>::Source as AsIntoIter>::Item;
163+
const MATCHES: bool = mem::size_of::<OUT>() == 0
164+
|| mem::size_of::<OUT>() != mem::size_of::<Self::IN>()
165+
|| mem::align_of::<OUT>() != mem::align_of::<Self::IN>();
166+
}

0 commit comments

Comments
 (0)