]> git.lizzy.rs Git - rust.git/blob - library/alloc/src/vec/spec_extend.rs
c6f4f22a01f6e2170cb382a9ebfb633951c8ad39
[rust.git] / library / alloc / src / vec / spec_extend.rs
1 use crate::alloc::Allocator;
2 use core::iter::TrustedLen;
3 use core::ptr::{self};
4 use core::slice::{self};
5
6 use super::{IntoIter, SetLenOnDrop, Vec};
7
8 // Specialization trait used for Vec::extend
9 pub(super) trait SpecExtend<T, I> {
10     fn spec_extend(&mut self, iter: I);
11 }
12
13 impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
14 where
15     I: Iterator<Item = T>,
16 {
17     default fn spec_extend(&mut self, iter: I) {
18         self.extend_desugared(iter)
19     }
20 }
21
22 impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
23 where
24     I: TrustedLen<Item = T>,
25 {
26     default fn spec_extend(&mut self, iterator: I) {
27         // This is the case for a TrustedLen iterator.
28         let (low, high) = iterator.size_hint();
29         if let Some(additional) = high {
30             debug_assert_eq!(
31                 low,
32                 additional,
33                 "TrustedLen iterator's size hint is not exact: {:?}",
34                 (low, high)
35             );
36             self.reserve(additional);
37             unsafe {
38                 let mut ptr = self.as_mut_ptr().add(self.len());
39                 let mut local_len = SetLenOnDrop::new(&mut self.len);
40                 iterator.for_each(move |element| {
41                     ptr::write(ptr, element);
42                     ptr = ptr.offset(1);
43                     // NB can't overflow since we would have had to alloc the address space
44                     local_len.increment_len(1);
45                 });
46             }
47         } else {
48             // Per TrustedLen contract a `None` upper bound means that the iterator length
49             // truly exceeds usize::MAX, which would eventually lead to a capacity overflow anyway.
50             // Since the other branch already panics eagerly (via `reserve()`) we do the same here.
51             // This avoids additional codegen for a fallback code path which would eventually
52             // panic anyway.
53             panic!("capacity overflow");
54         }
55     }
56 }
57
58 impl<T, A: Allocator> SpecExtend<T, IntoIter<T>> for Vec<T, A> {
59     fn spec_extend(&mut self, mut iterator: IntoIter<T>) {
60         unsafe {
61             self.append_elements(iterator.as_slice() as _);
62         }
63         iterator.ptr = iterator.end;
64     }
65 }
66
67 impl<'a, T: 'a, I, A: Allocator + 'a> SpecExtend<&'a T, I> for Vec<T, A>
68 where
69     I: Iterator<Item = &'a T>,
70     T: Clone,
71 {
72     default fn spec_extend(&mut self, iterator: I) {
73         self.spec_extend(iterator.cloned())
74     }
75 }
76
77 impl<'a, T: 'a, A: Allocator + 'a> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A>
78 where
79     T: Copy,
80 {
81     fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) {
82         let slice = iterator.as_slice();
83         unsafe { self.append_elements(slice) };
84     }
85 }