#
alloc/vec/
spec_extend.rs

    1use core::iter::TrustedLen;
    2use core::slice::{self};
    3
    4use super::{IntoIter, Vec};
    5use crate::alloc::Allocator;
    6
    7// Specialization trait used for Vec::extend
    8pub(super) trait SpecExtend<T, I> {
    9    #[track_caller]
    10    fn spec_extend(&mut self, iter: I);
    11}
    12
    13impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
    14where
    15    I: Iterator<Item = T>,
    16{
    17    #[track_caller]
    18    default fn spec_extend(&mut self, iter: I) {
    19        self.extend_desugared(iter)
    20    }
    21}
    22
    23impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
    24where
    25    I: TrustedLen<Item = T>,
    26{
    27    #[track_caller]
    28    default fn spec_extend(&mut self, iterator: I) {
    29        self.extend_trusted(iterator)
    30    }
    31}
    32
    33impl<T, A: Allocator> SpecExtend<T, IntoIter<T>> for Vec<T, A> {
    34    #[track_caller]
    35    fn spec_extend(&mut self, mut iterator: IntoIter<T>) {
    36        unsafe {
    37            self.append_elements(iterator.as_slice() as _);
    38        }
    39        iterator.forget_remaining_elements();
    40    }
    41}
    42
    43impl<'a, T: 'a, I, A: Allocator> SpecExtend<&'a T, I> for Vec<T, A>
    44where
    45    I: Iterator<Item = &'a T>,
    46    T: Clone,
    47{
    48    #[track_caller]
    49    default fn spec_extend(&mut self, iterator: I) {
    50        self.spec_extend(iterator.cloned())
    51    }
    52}
    53
    54impl<'a, T: 'a, A: Allocator> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A>
    55where
    56    T: Copy,
    57{
    58    #[track_caller]
    59    fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) {
    60        let slice = iterator.as_slice();
    61        unsafe { self.append_elements(slice) };
    62    }
    63}