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
use crate::{RawDrain, Storage};
pub struct Splice<'a, T, S, I>
where
S: ?Sized + Storage<T>,
I: Iterator<Item = T>,
{
raw: RawDrain<'a, T, S>,
replace_with: I,
}
impl<'a, T, S: ?Sized + Storage<T>, I: Iterator<Item = T>> Splice<'a, T, S, I> {
pub(crate) fn new(raw: RawDrain<'a, T, S>, replace_with: I) -> Self { Self { raw, replace_with } }
}
impl<T, S: ?Sized + Storage<T>, I: Iterator<Item = T>> Drop for Splice<'_, T, S, I> {
fn drop(&mut self) {
self.for_each(drop);
let Self { raw, replace_with } = self;
#[cfg(not(feature = "alloc"))]
{
const CAPACITY: usize = 16;
let mut buffer = crate::uninit_array!(CAPACITY);
let mut buffer = crate::SliceVec::new(&mut buffer);
replace_with.for_each(|item| unsafe {
buffer.push_unchecked(item);
if !RawDrain::<T, S>::IS_ZS && buffer.is_full() {
unsafe {
raw.assert_space(buffer.len());
raw.consume_write_slice_front(&buffer);
buffer.set_len_unchecked(0);
}
}
});
unsafe {
raw.assert_space(buffer.len());
raw.consume_write_slice_front(&buffer);
core::mem::forget(buffer);
}
}
#[cfg(feature = "alloc")]
{
let mut temp: std::vec::Vec<_> = replace_with.collect();
unsafe {
raw.assert_space(temp.len());
raw.consume_write_slice_front(&temp);
temp.set_len(0);
}
}
}
}
impl<'a, T, S: ?Sized + Storage<T>, I: Iterator<Item = T>> Iterator for Splice<'a, T, S, I> {
type Item = I::Item;
fn next(&mut self) -> Option<Self::Item> {
if self.raw.is_complete() {
return None
}
unsafe {
let front = self.raw.front();
Some(if let Some(item) = self.replace_with.next() {
let item = core::mem::replace(front, item);
self.raw.skip_front();
item
} else {
self.raw.take_front()
})
}
}
}