Skip to content

Add TrustedRandomAccess specialization for Vec::extend() #83770

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
May 26, 2021
Merged
Show file tree
Hide file tree
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
84 changes: 59 additions & 25 deletions library/alloc/src/vec/spec_extend.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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>,
{
Expand All @@ -19,7 +62,7 @@ where
}
}

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>,
{
Expand Down Expand Up @@ -57,31 +100,22 @@ where
}
}

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>
impl<T, I, A: Allocator> SpecExtendInner<T, I> for Vec<T, A>
where
I: Iterator<Item = &'a T>,
T: Clone,
I: TrustedLen<Item = T> + TrustedRandomAccess,
{
default fn spec_extend(&mut self, iterator: I) {
self.spec_extend(iterator.cloned())
}
}
default fn spec_extend(&mut self, mut iterator: I) {
let size = iterator.size();
self.reserve(size);

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) };
// 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 {
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);
}
}
}
}
4 changes: 4 additions & 0 deletions library/core/src/iter/adapters/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ impl<A: Debug + TrustedRandomAccess, B: Debug + TrustedRandomAccess> ZipFmt<A, B

/// 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.
Expand Down