1 | use crate::alloc::Allocator; |
2 | use core::iter::TrustedLen; |
3 | use core::slice::{self}; |
4 | |
5 | use super::{IntoIter, Vec}; |
6 | |
7 | // Specialization trait used for Vec::extend |
8 | pub(super) trait SpecExtend<T, I> { |
9 | fn spec_extend(&mut self, iter: I); |
10 | } |
11 | |
12 | impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A> |
13 | where |
14 | I: Iterator<Item = T>, |
15 | { |
16 | default fn spec_extend(&mut self, iter: I) { |
17 | self.extend_desugared(iterator:iter) |
18 | } |
19 | } |
20 | |
21 | impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A> |
22 | where |
23 | I: TrustedLen<Item = T>, |
24 | { |
25 | default fn spec_extend(&mut self, iterator: I) { |
26 | self.extend_trusted(iterator) |
27 | } |
28 | } |
29 | |
30 | impl<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 | |
39 | impl<'a, T: 'a, I, A: Allocator> SpecExtend<&'a T, I> for Vec<T, A> |
40 | where |
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 | |
49 | impl<'a, T: 'a, A: Allocator> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A> |
50 | where |
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 | |