From 6b99f0553faadb3c7d1df04e8c02a252c38c216c Mon Sep 17 00:00:00 2001 From: The 8472 Date: Thu, 25 Nov 2021 23:04:54 +0100 Subject: [PATCH] extract const guard to trait associated const for optimization experiments --- library/alloc/src/vec/source_iter_marker.rs | 22 +++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) 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::(); +}