diff --git a/library/alloc/src/vec/source_iter_marker.rs b/library/alloc/src/vec/source_iter_marker.rs index 6e78534cf5b10..1c7417375e512 100644 --- a/library/alloc/src/vec/source_iter_marker.rs +++ b/library/alloc/src/vec/source_iter_marker.rs @@ -21,12 +21,7 @@ where // a) no ZSTs as there would be no allocation to reuse and pointer arithmetic would panic // b) size match as required by Alloc contract // c) alignments match as required by Alloc contract - if mem::size_of::() == 0 - || mem::size_of::() - != mem::size_of::<<::Source as AsIntoIter>::Item>() - || mem::align_of::() - != mem::align_of::<<::Source as AsIntoIter>::Item>() - { + if ::MATCHES { // fallback to more generic implementations return SpecFromIterNested::from_iter(iterator); } @@ -154,3 +149,18 @@ where len } } + +trait LayoutMatcher { + type IN; + const MATCHES: bool; +} + +impl LayoutMatcher for I +where + I: Iterator + SourceIter, +{ + type IN = <::Source as AsIntoIter>::Item; + const MATCHES: bool = mem::size_of::() == 0 + || mem::size_of::() != mem::size_of::() + || mem::align_of::() != mem::align_of::(); +}