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