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
use std::{
fmt,
iter::{FusedIterator, TrustedLen},
mem,
ptr::{self, NonNull},
slice,
};
use crate::SmallVec;
pub struct Drain<'a, T, const N: usize> {
pub(crate) tail_start: usize,
pub(crate) tail_len: usize,
pub(crate) iter: slice::Iter<'a, T>,
pub(crate) vec: NonNull<SmallVec<T, N>>,
}
impl<T, const N: usize> Drain<'_, T, N> {}
impl<T, const N: usize> AsRef<[T]> for Drain<'_, T, N> {
fn as_ref(&self) -> &[T] {
self.as_slice()
}
}
impl<T, const N: usize> Drain<'_, T, N> {
pub fn as_slice(&self) -> &[T] {
self.iter.as_slice()
}
}
impl<'a, T, const N: usize> Drain<'a, T, N> {
pub fn new(
tail_start: usize,
tail_len: usize,
iter: slice::Iter<'a, T>,
vec: NonNull<SmallVec<T, N>>,
) -> Self {
Self {
tail_start,
tail_len,
iter,
vec,
}
}
}
impl<T, const N: usize> Drop for Drain<'_, T, N> {
fn drop(&mut self) {
struct DropGuard<'r, 'a, T, const N: usize>(&'r mut Drain<'a, T, N>);
impl<'r, 'a, T, const N: usize> Drop for DropGuard<'r, 'a, T, N> {
fn drop(&mut self) {
self.0.for_each(drop);
if self.0.tail_len > 0 {
unsafe {
let source_vec = self.0.vec.as_mut();
let start = source_vec.len();
let tail = self.0.tail_start;
if tail != start {
let src = source_vec.as_ptr().add(tail);
let dst = source_vec.as_mut_ptr().add(start);
ptr::copy(src, dst, self.0.tail_len);
}
source_vec.set_len(start + self.0.tail_len);
}
}
}
}
while let Some(item) = self.next() {
let guard = DropGuard(self);
drop(item);
mem::forget(guard);
}
let _ = DropGuard(self);
}
}
unsafe impl<T: Sync, const N: usize> Sync for Drain<'_, T, N> {}
unsafe impl<T: Send, const N: usize> Send for Drain<'_, T, N> {}
impl<T, const N: usize> fmt::Debug for Drain<'_, T, N>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Drain").field(&self.iter.as_slice()).finish()
}
}
impl<T, const N: usize> Iterator for Drain<'_, T, N> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.map(|elt| unsafe { ptr::read(elt as *const _) })
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<T, const N: usize> DoubleEndedIterator for Drain<'_, T, N> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.iter
.next_back()
.map(|elt| unsafe { ptr::read(elt as *const _) })
}
}
impl<T, const N: usize> ExactSizeIterator for Drain<'_, T, N> {
fn is_empty(&self) -> bool {
self.iter.is_empty()
}
}
impl<T, const N: usize> FusedIterator for Drain<'_, T, N> {}
unsafe impl<T, const N: usize> TrustedLen for Drain<'_, T, N> {}