add TrustedRandomAccess specialization to vec::extend
This should do roughly the same as the TrustedLen specialization but result in less IR by using __iterator_get_unchecked instead of iterator.for_each.
This commit is contained in:
parent
d474075a8f
commit
020287516b
@ -1,5 +1,5 @@
|
||||
use crate::alloc::Allocator;
|
||||
use core::iter::TrustedLen;
|
||||
use core::iter::{TrustedLen, TrustedRandomAccess};
|
||||
use core::ptr::{self};
|
||||
use core::slice::{self};
|
||||
|
||||
@ -11,6 +11,49 @@ pub(super) trait SpecExtend<T, I> {
|
||||
}
|
||||
|
||||
impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
|
||||
where
|
||||
I: Iterator<Item = T>,
|
||||
{
|
||||
default fn spec_extend(&mut self, iter: I) {
|
||||
SpecExtendInner::spec_extend(self, iter);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, A: Allocator> SpecExtend<T, IntoIter<T>> for Vec<T, A> {
|
||||
fn spec_extend(&mut self, mut iterator: IntoIter<T>) {
|
||||
unsafe {
|
||||
self.append_elements(iterator.as_slice() as _);
|
||||
}
|
||||
iterator.ptr = iterator.end;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: 'a, I, A: Allocator + 'a> SpecExtend<&'a T, I> for Vec<T, A>
|
||||
where
|
||||
I: Iterator<Item = &'a T>,
|
||||
T: Clone,
|
||||
{
|
||||
default fn spec_extend(&mut self, iterator: I) {
|
||||
SpecExtend::spec_extend(self, iterator.cloned())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: 'a, A: Allocator + 'a> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A>
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) {
|
||||
let slice = iterator.as_slice();
|
||||
unsafe { self.append_elements(slice) };
|
||||
}
|
||||
}
|
||||
|
||||
// Helper trait to disambiguate overlapping specializations
|
||||
trait SpecExtendInner<T, I> {
|
||||
fn spec_extend(&mut self, iter: I);
|
||||
}
|
||||
|
||||
impl<T, I, A: Allocator> SpecExtendInner<T, I> for Vec<T, A>
|
||||
where
|
||||
I: Iterator<Item = T>,
|
||||
{
|
||||
@ -19,7 +62,7 @@ impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
|
||||
impl<T, I, A: Allocator> SpecExtendInner<T, I> for Vec<T, A>
|
||||
where
|
||||
I: TrustedLen<Item = T>,
|
||||
{
|
||||
@ -57,31 +100,22 @@ impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, A: Allocator> SpecExtend<T, IntoIter<T>> for Vec<T, A> {
|
||||
fn spec_extend(&mut self, mut iterator: IntoIter<T>) {
|
||||
impl<T, I, A: Allocator> SpecExtendInner<T, I> for Vec<T, A>
|
||||
where
|
||||
I: TrustedLen<Item = T> + TrustedRandomAccess,
|
||||
{
|
||||
default fn spec_extend(&mut self, mut iterator: I) {
|
||||
let size = iterator.size();
|
||||
self.reserve(size);
|
||||
|
||||
// SAFETY: reserve ensured that there is sufficient capacity for the additional items.
|
||||
// The loop upholds the TRA requirements by accessing each element only once.
|
||||
unsafe {
|
||||
self.append_elements(iterator.as_slice() as _);
|
||||
let sink = self.as_mut_ptr().add(self.len());
|
||||
for i in 0..size {
|
||||
ptr::write(sink.add(i), iterator.__iterator_get_unchecked(i));
|
||||
self.set_len(self.len() + 1);
|
||||
}
|
||||
}
|
||||
iterator.ptr = iterator.end;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: 'a, I, A: Allocator + 'a> SpecExtend<&'a T, I> for Vec<T, A>
|
||||
where
|
||||
I: Iterator<Item = &'a T>,
|
||||
T: Clone,
|
||||
{
|
||||
default fn spec_extend(&mut self, iterator: I) {
|
||||
self.spec_extend(iterator.cloned())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: 'a, A: Allocator + 'a> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A>
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) {
|
||||
let slice = iterator.as_slice();
|
||||
unsafe { self.append_elements(slice) };
|
||||
}
|
||||
}
|
||||
|
@ -416,6 +416,10 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
|
||||
/// An iterator whose items are random-accessible efficiently
|
||||
///
|
||||
/// Iterators that implement this trait should also implement TrustedLen which
|
||||
/// allows specialization to disambiguate overlaps with a `TrustedLen + TrustedRandomAccess`
|
||||
/// bound.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The iterator's `size_hint` must be exact and cheap to call.
|
||||
|
Loading…
Reference in New Issue
Block a user