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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use crate::allocator::AllocatorProvider;
use crate::dynamic::common::limit::LEN_VALUE_MASK_USIZE;
use crate::dynamic::types::{DynamicVec, Variant, VariantMut};
use crate::vec::types::EcoVec;
impl<'a, const N: usize, A> DynamicVec<'a, N, A>
where
A: AllocatorProvider,
{
#[inline]
pub fn len(&self) -> usize {
if self.is_pure_inline() {
unsafe { (*self.0.inline).len() }
} else {
// Referenced and Spilled are both repr(C) with `len: usize`
// at offset size_of::<usize>(). The mask strips Referenced
// tag bits and is a no-op for Spilled (len ≤ isize::MAX).
unsafe { (*self.0.referenced).len & LEN_VALUE_MASK_USIZE }
}
}
#[inline]
pub fn as_slice(&self) -> &[u8] {
if self.is_pure_inline() {
unsafe { (*self.0.inline).as_slice() }
} else {
// Referenced and Spilled are both repr(C) with ptr at offset 0
// and len at offset size_of::<usize>(). Reading through the
// Referenced arm is sound for Spilled because the fields have
// the same type and offset. The mask strips Referenced tag
// bits and is a no-op for Spilled (len ≤ isize::MAX).
let r = unsafe { &*self.0.referenced };
let len = r.len & LEN_VALUE_MASK_USIZE;
unsafe { core::slice::from_raw_parts(r.ptr, len) }
}
}
#[inline]
pub fn make_mut(&mut self, size_hint: usize) -> &mut [u8]
where
A: Clone,
{
// Materialise Referenced inline — reads data directly from the
// union member (one tag check, no separate helper function).
// Borrows &A instead of cloning upfront; from_slice_in_owned
// defers the clone until the inline/spill decision is made.
if self.is_referenced() {
let r = unsafe { &*self.0.referenced };
let len = r.len & LEN_VALUE_MASK_USIZE;
let ptr = r.ptr;
// Safety: from_referenced guarantees ptr/len are valid.
let slice = unsafe { core::slice::from_raw_parts(ptr, len) };
*self = Self::from_slice_in_owned(slice, &r.allocator, size_hint);
}
match self.variant_mut() {
VariantMut::Inline(inline) => inline.as_mut_slice(),
VariantMut::Spilled(spilled) => {
// Ensure uniqueness via the u8-specialised memcpy path
// instead of the generic element-by-element clone.
// The subsequent make_mut() sees is_unique()=true and
// returns the mutable slice without re-cloning.
spilled.make_unique_bytes();
spilled.make_mut()
}
VariantMut::Referenced(_) => unreachable!(),
}
}
#[inline]
pub fn push(&mut self, byte: u8)
where
A: Clone,
{
if self.is_referenced() {
self.make_mut(1);
// Fall through — now Inline or Spilled.
}
match self.variant_mut() {
VariantMut::Inline(inline) => {
if inline.push(byte).is_err() {
let alloc = inline.allocator.clone();
// NLL: inline borrow is dead after clone.
self.spill_push(byte, alloc);
}
}
VariantMut::Spilled(spilled) => {
spilled.push(byte);
}
// make_mut() above guarantees the variant is no longer Referenced.
VariantMut::Referenced(_) => unreachable!(),
}
}
#[inline]
pub fn extend_from_slice(&mut self, bytes: &[u8])
where
A: Clone,
{
if self.is_referenced() {
self.make_mut(bytes.len());
// Fall through — now Inline or Spilled.
}
match self.variant_mut() {
VariantMut::Inline(inline) => {
if inline.extend_from_slice(bytes).is_err() {
let needed = inline.len() + bytes.len();
let alloc = inline.allocator.clone();
// NLL: inline borrow is dead after clone.
self.spill_extend(bytes, needed, alloc);
}
}
VariantMut::Spilled(spilled) => {
spilled.extend_from_byte_slice(bytes);
}
// make_mut() above guarantees the variant is no longer Referenced.
VariantMut::Referenced(_) => unreachable!(),
}
}
#[inline]
pub fn clear(&mut self)
where
A: Clone,
{
if self.is_referenced() {
// Skip materialisation — copying the borrowed data just to
// discard it is wasteful. Replace with a fresh empty inline.
let alloc = unsafe { (*self.0.referenced).allocator.clone() };
*self = Self::new_in(alloc);
return;
}
match self.variant_mut() {
VariantMut::Inline(inline) => inline.clear(),
VariantMut::Spilled(spilled) => spilled.clear(),
VariantMut::Referenced(_) => unreachable!(),
}
}
#[inline]
pub fn truncate(&mut self, target: usize)
where
A: Clone,
{
match self.variant_mut() {
VariantMut::Referenced(r) => {
let cur_len = r.len & LEN_VALUE_MASK_USIZE;
if target < cur_len {
use crate::dynamic::common::limit::LEN_TAGS_USIZE;
r.len = target | LEN_TAGS_USIZE;
}
}
VariantMut::Inline(inline) => inline.truncate(target),
VariantMut::Spilled(spilled) => spilled.truncate(target),
}
}
#[inline]
#[allow(dead_code)]
pub(crate) fn get_allocator(&self) -> &A
where
A: Clone,
{
// ZST allocators (e.g. Global) have no data — skip the dispatch.
if core::mem::size_of::<A>() == 0 {
unsafe { &*core::ptr::NonNull::<A>::dangling().as_ptr() }
} else {
match self.variant() {
Variant::Referenced(r) => &r.allocator,
Variant::Inline(inline) => inline.get_allocator(),
Variant::Spilled(spilled) => spilled.get_allocator(),
}
}
}
/// Spill from inline to heap and push one byte.
/// Allocator is passed directly from the match arm — no re-dispatch.
#[cold]
#[inline(never)]
fn spill_push(&mut self, byte: u8, alloc: A)
where
A: Clone,
{
let mut eco = EcoVec::with_capacity_in(N * 2, alloc);
// Safety: with_capacity_in(N * 2) guarantees capacity ≥ N + 1.
// The current inline data is at most N bytes, plus 1 byte to push.
// The fresh vec is unique (refcount = 1).
// Read directly from the inline union member — this function is only
// called from the Inline match arm, so skip the as_slice() dispatch.
unsafe {
eco.extend_from_byte_slice_unchecked((*self.0.inline).as_slice());
eco.push_unchecked(byte);
// Write EcoVec directly into *self — skip from_eco intermediate.
self.emplace_eco(eco);
}
}
/// Spill from inline to heap and extend with a byte slice.
/// Allocator and needed capacity passed directly — no re-dispatch.
#[cold]
#[inline(never)]
fn spill_extend(&mut self, bytes: &[u8], needed: usize, alloc: A)
where
A: Clone,
{
let mut eco = EcoVec::with_capacity_in(needed.next_power_of_two(), alloc);
// Safety: capacity ≥ needed = self.len() + bytes.len().
// The fresh vec is unique (refcount = 1).
// Read directly from the inline union member — this function is only
// called from the Inline match arm, so skip the as_slice() dispatch.
unsafe {
eco.extend_from_byte_slice_unchecked((*self.0.inline).as_slice());
eco.extend_from_byte_slice_unchecked(bytes);
// Write EcoVec directly into *self — skip from_eco intermediate.
self.emplace_eco(eco);
}
}
/// Write an EcoVec directly into `*self`, transitioning to the Spilled
/// variant without an intermediate `MaybeUninit` or `from_eco` call.
///
/// # Safety
/// - The current variant must be Inline or Referenced (Drop for these
/// is a no-op — no heap resources to clean up).
/// - The caller must have already copied any needed data from *self.
#[inline(always)]
unsafe fn emplace_eco(&mut self, eco: EcoVec<u8, A>) {
use crate::dynamic::common::limit::TAG_OVERLAPS_LEN;
use core::mem::ManuallyDrop;
use core::ptr::addr_of_mut;
let self_ptr = self as *mut Self;
// On 64-bit LE, the high byte of EcoVec::len is at the tagged_len
// position and is guaranteed 0 (len ≤ isize::MAX). On other
// layouts we must explicitly clear the tag byte.
if !TAG_OVERLAPS_LEN {
// Safety:
// The tag byte lives at offset `N` within the inline union member,
// which is in bounds of `*self`. Writing it does not touch any
// initialized field the caller still needs (caller copied data out).
unsafe {
let tag_ptr = (addr_of_mut!((*self_ptr).0.inline) as *mut u8).add(N);
tag_ptr.write(0);
}
}
// Safety:
// The current variant is Inline/Referenced (Drop is a no-op for these),
// so overwriting the union with the Spilled member leaks nothing, and
// `spilled_ptr` points to the correctly-aligned, in-bounds union slot.
unsafe {
let spilled_ptr = addr_of_mut!((*self_ptr).0.spilled);
spilled_ptr.write(ManuallyDrop::new(eco));
}
}
}