Skip to content

extract const guard to trait associated const for optimization experiment #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions library/alloc/src/vec/source_iter_marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<T>() == 0
|| mem::size_of::<T>()
!= mem::size_of::<<<I as SourceIter>::Source as AsIntoIter>::Item>()
|| mem::align_of::<T>()
!= mem::align_of::<<<I as SourceIter>::Source as AsIntoIter>::Item>()
{
if <I as LayoutMatcher>::MATCHES {
// fallback to more generic implementations
return SpecFromIterNested::from_iter(iterator);
}
Expand Down Expand Up @@ -154,3 +149,18 @@ where
len
}
}

trait LayoutMatcher {
type IN;
const MATCHES: bool;
}

impl<I, OUT> LayoutMatcher for I
where
I: Iterator<Item = OUT> + SourceIter<Source: AsIntoIter>,
{
type IN = <<I as SourceIter>::Source as AsIntoIter>::Item;
const MATCHES: bool = mem::size_of::<OUT>() == 0
|| mem::size_of::<OUT>() != mem::size_of::<Self::IN>()
|| mem::align_of::<OUT>() != mem::align_of::<Self::IN>();
}