alloc/collections/vec_deque/
splice.rs1use core::alloc::Allocator;
2
3use crate::alloc::Global;
4use crate::collections::vec_deque::Drain;
5use crate::vec::Vec;
6
7#[unstable(feature = "deque_extend_front", issue = "146975")]
23#[derive(Debug)]
24pub struct Splice<
25 'a,
26 I: Iterator + 'a,
27 #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global,
28> {
29 pub(super) drain: Drain<'a, I::Item, A>,
30 pub(super) replace_with: I,
31}
32
33#[unstable(feature = "deque_extend_front", issue = "146975")]
34impl<I: Iterator, A: Allocator> Iterator for Splice<'_, I, A> {
35 type Item = I::Item;
36
37 fn next(&mut self) -> Option<Self::Item> {
38 self.drain.next()
39 }
40
41 fn size_hint(&self) -> (usize, Option<usize>) {
42 self.drain.size_hint()
43 }
44}
45
46#[unstable(feature = "deque_extend_front", issue = "146975")]
47impl<I: Iterator, A: Allocator> DoubleEndedIterator for Splice<'_, I, A> {
48 fn next_back(&mut self) -> Option<Self::Item> {
49 self.drain.next_back()
50 }
51}
52
53#[unstable(feature = "deque_extend_front", issue = "146975")]
54impl<I: Iterator, A: Allocator> ExactSizeIterator for Splice<'_, I, A> {}
55
56#[unstable(feature = "deque_extend_front", issue = "146975")]
58impl<I: Iterator, A: Allocator> Drop for Splice<'_, I, A> {
59 fn drop(&mut self) {
60 self.drain.by_ref().for_each(drop);
63
64 unsafe {
68 let tail_len = self.drain.tail_len; if tail_len == 0 {
71 self.drain.deque.as_mut().extend(self.replace_with.by_ref());
72 return;
73 }
74
75 if !self.drain.fill(&mut self.replace_with) {
77 return;
78 }
79
80 let (lower_bound, _upper_bound) = self.replace_with.size_hint();
83 if lower_bound > 0 {
84 self.drain.move_tail(lower_bound);
85 if !self.drain.fill(&mut self.replace_with) {
86 return;
87 }
88 }
89
90 let mut collected = self.replace_with.by_ref().collect::<Vec<I::Item>>().into_iter();
93 if collected.len() > 0 {
95 self.drain.move_tail(collected.len());
96 let filled = self.drain.fill(&mut collected);
97 debug_assert!(filled);
98 debug_assert_eq!(collected.len(), 0);
99 }
100 }
101 }
103}
104
105impl<T, A: Allocator> Drain<'_, T, A> {
107 unsafe fn fill<I: Iterator<Item = T>>(&mut self, replace_with: &mut I) -> bool {
117 let deque = unsafe { self.deque.as_mut() };
118 let range_start = deque.len;
119 let range_end = range_start + self.drain_len;
120
121 for idx in range_start..range_end {
122 if let Some(new_item) = replace_with.next() {
123 let index = deque.to_physical_idx(idx);
124 unsafe { deque.buffer_write(index, new_item) };
125 deque.len += 1;
126 self.drain_len -= 1;
127 } else {
128 return false;
129 }
130 }
131 true
132 }
133
134 unsafe fn move_tail(&mut self, additional: usize) {
140 let deque = unsafe { self.deque.as_mut() };
141 let tail_start = deque.len + self.drain_len;
142 deque.buf.reserve(tail_start + self.tail_len, additional);
143
144 let new_tail_start = tail_start + additional;
145 unsafe {
146 deque.wrap_copy(tail_start, new_tail_start, self.tail_len);
147 }
148 self.drain_len += additional;
149 }
150}