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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#[cfg(feature = "specialization")]
use super::specialization::SpecFromSlice;
use {
super::{
SmallVec,
allocator::{
Allocator,
boxed::Box,
global::Global,
like::Like,
vec::Vec
},
locatedlength::LocatedLength,
rawsmallvec::RawSmallVec
},
core::{
mem::ManuallyDrop,
ptr::{
NonNull,
copy_nonoverlapping
}
}
};
impl<Item: Clone, const INLINE: usize> From<&[Item]> for SmallVec<Item, INLINE, Global> {
#[inline]
fn from(slice: &[Item]) -> Self {
if slice.len() > Self::inline_size() {
// Standard Rust vectors are already specialized.
<Vec<Item, Global> as Like>::Type::from(slice).into()
} else {
// SAFETY: The precondition is checked in the initial comparison
// above.
unsafe {
#[cfg(feature = "specialization")]
{
<Self as SpecFromSlice<Item>>::spec_from(slice)
}
#[cfg(not(feature = "specialization"))]
{
Self::from_slice_fallback(slice)
}
}
}
}
}
impl<Item: Clone, const INLINE: usize> From<&mut [Item]> for SmallVec<Item, INLINE, Global> {
#[inline]
fn from(slice: &mut [Item]) -> Self {
Self::from(slice as &[Item])
}
}
impl<Item: Clone, const M: usize, const INLINE: usize> From<&[Item; M]>
for SmallVec<Item, INLINE, Global>
{
#[inline]
fn from(slice: &[Item; M]) -> Self {
Self::from(slice as &[Item])
}
}
impl<Item: Clone, const M: usize, const INLINE: usize> From<&mut [Item; M]>
for SmallVec<Item, INLINE, Global>
{
#[inline]
fn from(slice: &mut [Item; M]) -> Self {
Self::from(slice as &[Item])
}
}
impl<Item, const INLINE: usize, const M: usize> From<[Item; M]> for SmallVec<Item, INLINE, Global> {
fn from(array: [Item; M]) -> Self {
if M > INLINE {
// If M > INLINE, we'd have to heap allocate anyway,
// so delegate for Vec for the allocation.
Self::from(<Vec<Item, Global> as Like>::Type::from(array))
} else {
// M <= INLINE
let mut this = Self::new();
debug_assert!(M <= this.capacity());
let array = ManuallyDrop::new(array);
// SAFETY: M <= this.capacity()
unsafe {
copy_nonoverlapping(array.as_ptr(), this.as_mut_ptr(), M);
this.set_len(M);
}
this
}
}
}
impl<Item, const INLINE: usize, const M: usize, Heap: Allocator>
TryFrom<SmallVec<Item, INLINE, Heap>> for [Item; M]
{
type Error = SmallVec<Item, INLINE, Heap>;
#[inline]
fn try_from(
mut this: SmallVec<Item, INLINE, Heap>
) -> Result<[Item; M], SmallVec<Item, INLINE, Heap>> {
if this.len() != M {
Err(this)
} else {
// SAFETY: we release ownership of the elements we hold
unsafe {
this.set_len(0);
}
let ptr = this.as_ptr() as *const [Item; M];
// SAFETY: these elements are initialized since the length was `M`
unsafe { Ok(ptr.read()) }
}
}
}
impl<Item, const INLINE: usize> From<<Vec<Item, Global> as Like>::Type>
for SmallVec<Item, INLINE, Global>
{
fn from(array: <Vec<Item, Global> as Like>::Type) -> Self {
if array.capacity() == 0 {
return Self::new();
}
if Self::IS_ZST {
// "Move" elements to stack buffer. They're ZST so we don't actually
// have to do anything. Just make sure they're not
// dropped. We don't wrap the vector in ManuallyDrop so
// that when it's dropped, the memory is deallocated, if
// it needs to be.
let mut vec = array;
let length = vec.len();
// SAFETY: `0` is less than the vector's capacity.
// old_len..new_len is an empty range. So there are no uninitialized
// elements
unsafe { vec.set_len(0) };
Self {
length: LocatedLength::new(length, false),
raw: RawSmallVec::new(),
allocator: Global
}
} else {
let mut vec = ManuallyDrop::new(array);
let length = vec.len();
// A heap-allocated `SmallVec` must always observe the invariant
// that `cap > INLINE`.
if vec.capacity() <= INLINE {
vec.reserve(INLINE + 1 - length);
}
let cap = vec.capacity();
// SAFETY: vec.capacity is not `0` (checked above), so the pointer
// can not dangle and thus specifically cannot be null.
let ptr = unsafe { NonNull::new_unchecked(vec.as_mut_ptr()) };
Self {
length: LocatedLength::new(length, true),
raw: RawSmallVec::heap(ptr, cap),
allocator: Global
}
}
}
}
impl<Item, const INLINE: usize, Heap: Allocator> From<SmallVec<Item, INLINE, Heap>>
for <Vec<Item, Global> as Like>::Type
{
fn from(this: SmallVec<Item, INLINE, Heap>) -> Self {
let (length, on_heap) = this.length.parts();
if !on_heap {
let mut vec = <Vec<Item, Global> as Like>::Type::with_capacity(length);
let this = ManuallyDrop::new(this);
// SAFETY: we create a new vector with sufficient capacity, copy our
// elements into it to transfer ownership and then set
// the length we don't drop the elements we previously
// held
unsafe {
copy_nonoverlapping(this.raw.as_ptr_inline(), vec.as_mut_ptr(), length);
vec.set_len(length);
}
vec
} else {
let this = ManuallyDrop::new(this);
// SAFETY:
// - `ptr` was created with the SmallVec's allocator
// - `ptr` was created with the appropriate alignment for `Item`
// - the allocation pointed to by ptr is exactly cap * sizeof(Item)
// - `length` is less than or equal to `cap`
// - the first `length` entries are proper `Item`-values
// - the allocation is not larger than `isize::MAX`
unsafe {
let (ptr, cap) = this.raw.heap;
<Vec<Item, Global> as Like>::Type::from_raw_parts(ptr.as_ptr(), length, cap)
}
}
}
}
impl<Item, const INLINE: usize, Heap: Allocator> From<SmallVec<Item, INLINE, Heap>>
for <Box<[Item], Global> as Like>::Type
{
fn from(this: SmallVec<Item, INLINE, Heap>) -> Self {
<Vec<Item, Global> as Like>::Type::from(this).into_boxed_slice()
}
}
impl<T, const N: usize> From<<Box<[T], Global> as Like>::Type> for SmallVec<T, N> {
fn from(boxed: <Box<[T], Global> as Like>::Type) -> Self {
boxed.into_vec().into()
}
}