1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::{ptr, slice};
use crate::Drain;
#[derive(Debug)]
pub struct Splice<'a, I: Iterator + 'a, const N: usize> {
pub(crate) drain: Drain<'a, I::Item, N>,
pub(crate) replace_with: I,
}
impl<'a, I: Iterator + 'a, const N: usize> Splice<'a, I, N> {
pub fn new(drain: Drain<'a, I::Item, N>, replace_with: I) -> Self {
Self {
drain,
replace_with,
}
}
}
impl<I: Iterator, const N: usize> Iterator for Splice<'_, I, N> {
type Item = I::Item;
fn next(&mut self) -> Option<Self::Item> {
self.drain.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.drain.size_hint()
}
}
impl<I: Iterator, const N: usize> DoubleEndedIterator for Splice<'_, I, N> {
fn next_back(&mut self) -> Option<Self::Item> {
self.drain.next_back()
}
}
impl<I: Iterator, const N: usize> ExactSizeIterator for Splice<'_, I, N> {}
impl<I: Iterator, const N: usize> Drop for Splice<'_, I, N> {
fn drop(&mut self) {
self.drain.by_ref().for_each(drop);
unsafe {
if self.drain.tail_len == 0 {
self.drain.vec.as_mut().extend(self.replace_with.by_ref());
return;
}
let fill = self.drain.fill(&mut self.replace_with);
if !fill {
return;
}
let (lower_bound, _upper_bound) = self.replace_with.size_hint();
if lower_bound > 0 {
self.drain.move_tail(lower_bound);
let fill = self.drain.fill(&mut self.replace_with);
if !fill {
return;
}
}
let mut collected = self
.replace_with
.by_ref()
.collect::<Vec<I::Item>>()
.into_iter();
if collected.len() > 0 {
self.drain.move_tail(collected.len());
let filled = self.drain.fill(&mut collected);
debug_assert!(filled);
debug_assert_eq!(collected.len(), 0);
}
}
}
}
impl<T, const N: usize> Drain<'_, T, N> {
pub(crate) unsafe fn fill<I: Iterator<Item = T>>(&mut self, replace_with: &mut I) -> bool {
let vec = unsafe { self.vec.as_mut() };
let range_start = vec.len;
let range_end = self.tail_start;
let data = unsafe { vec.as_mut_ptr().add(range_start) };
let range_len = range_end - range_start;
let range_slice = unsafe { slice::from_raw_parts_mut(data, range_len) };
for place in range_slice {
if let Some(new_item) = replace_with.next() {
unsafe { ptr::write(place, new_item) };
vec.len += 1;
} else {
return false;
}
}
true
}
pub(crate) unsafe fn move_tail(&mut self, additional: usize) {
let small_vec = unsafe { self.vec.as_mut() };
let len = self.tail_start + self.tail_len;
let src = unsafe { small_vec.as_ptr().add(self.tail_start) };
let src_value = unsafe { ptr::read(src) };
let a = small_vec.spilled();
small_vec.reserve_exact(additional + len);
let new_tail_start = self.tail_start + additional;
let b = small_vec.spilled();
if a == b {
unsafe {
let src = small_vec.as_ptr().add(self.tail_start);
let dst = small_vec.as_mut_ptr().add(new_tail_start);
ptr::copy(src, dst, self.tail_len);
}
} else {
unsafe {
let src = &src_value as *const T;
let dst = small_vec.as_mut_ptr().add(new_tail_start);
ptr::copy(src, dst, self.tail_len);
}
}
self.tail_start = new_tail_start;
}
}