-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathdynamic.rs
More file actions
448 lines (400 loc) · 13.5 KB
/
dynamic.rs
File metadata and controls
448 lines (400 loc) · 13.5 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use core::mem::{self, ManuallyDrop};
use core::ptr;
use std::ops::RangeBounds;
use super::EcoVec;
/// A byte vector that can hold up to 15 bytes inline and then spills to an
/// `EcoVec<u8>`.
pub(crate) struct DynamicVec(Repr);
/// The internal representation.
///
/// On 64-bit little endian, this assumes that no valid EcoVec exists in which
/// the highest-order bit of the InlineVec's `tagged_len` would be set. This is
/// true because EcoVec is repr(C) and its second field `len` is bounded by
/// `isize::MAX`. On 32-bit, it's no problem and for 64-bit big endian, we
/// have an increased limit to prevent the overlap.
#[repr(C)]
union Repr {
inline: InlineVec,
spilled: ManuallyDrop<EcoVec<u8>>,
}
/// This is never stored in memory, it's just an abstraction for safe access.
#[derive(Debug)]
enum Variant<'a> {
Inline(&'a InlineVec),
Spilled(&'a EcoVec<u8>),
}
/// This is never stored in memory, it's just an abstraction for safe access.
#[derive(Debug)]
enum VariantMut<'a> {
Inline(&'a mut InlineVec),
Spilled(&'a mut EcoVec<u8>),
}
/// The maximum amount of inline storage. Typically, this is 15 bytes.
///
/// However, in the rare exotic system, we still want things to be safe.
/// Therefore, the following special cases:
/// - For big endian, we increase the limit such that the tagged length of the
/// inline variants doesn't overlap with the EcoVec. For little endian, it's
/// fine since the highest order bit is never set for a valid EcoVec.
/// - In case somehow EcoVec is very big (128-bit pointers woah), increase the
/// limit too.
pub(crate) const LIMIT: usize = {
let mut limit = 15;
if limit < mem::size_of::<EcoVec<u8>>() - 1 {
limit = mem::size_of::<EcoVec<u8>>() - 1;
}
if cfg!(target_endian = "big") {
limit += mem::size_of::<usize>();
}
limit
};
/// This bit is used to check whether we are inline or not. On 64-bit little
/// endian, it coincides with the highest-order bit of an EcoVec's length, which
/// can't be set because the EcoVec's length never exceeds `isize::MAX`.
const LEN_TAG: u8 = 0b1000_0000;
/// This is used to mask off the tag to get the inline variant's length.
const LEN_MASK: u8 = 0b0111_1111;
impl DynamicVec {
#[inline]
pub const fn new() -> Self {
Self::from_inline(InlineVec::new())
}
#[inline]
pub const fn from_inline(inline: InlineVec) -> Self {
Self(Repr { inline })
}
#[inline]
pub const fn from_eco(vec: EcoVec<u8>) -> Self {
// Safety:
// Explicitly set `tagged_len` to 0 to mark this as a spilled variant.
// Just initializing with `Repr { spilled: ... }` would leave
// `tagged_len` uninitialized, leading to undefined behaviour on access.
let mut repr = Repr {
inline: InlineVec { buf: [0; LIMIT], tagged_len: 0 },
};
repr.spilled = ManuallyDrop::new(vec);
Self(repr)
}
#[inline]
pub fn from_slice(bytes: &[u8]) -> Self {
match InlineVec::from_slice(bytes) {
Ok(inline) => Self::from_inline(inline),
_ => Self::from_eco(EcoVec::from(bytes)),
}
}
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
if capacity <= LIMIT {
Self::new()
} else {
Self::from_eco(EcoVec::with_capacity(capacity))
}
}
#[inline]
pub fn len(&self) -> usize {
match self.variant() {
Variant::Inline(inline) => inline.len(),
Variant::Spilled(spilled) => spilled.len(),
}
}
#[inline]
pub fn as_slice(&self) -> &[u8] {
match self.variant() {
Variant::Inline(inline) => inline.as_slice(),
Variant::Spilled(spilled) => spilled.as_slice(),
}
}
#[inline]
pub fn make_mut(&mut self) -> &mut [u8] {
match self.variant_mut() {
VariantMut::Inline(inline) => inline.as_mut_slice(),
VariantMut::Spilled(spilled) => spilled.make_mut(),
}
}
#[inline]
pub fn push(&mut self, byte: u8) {
match self.variant_mut() {
VariantMut::Inline(inline) => {
if inline.push(byte).is_err() {
let mut eco = EcoVec::with_capacity(LIMIT * 2);
eco.extend_from_byte_slice(self.as_slice());
eco.push(byte);
*self = Self::from_eco(eco);
}
}
VariantMut::Spilled(spilled) => {
spilled.push(byte);
}
}
}
#[inline]
pub fn extend_from_slice(&mut self, bytes: &[u8]) {
match self.variant_mut() {
VariantMut::Inline(inline) => {
if inline.extend_from_slice(bytes).is_err() {
let needed = inline.len() + bytes.len();
let mut eco = EcoVec::with_capacity(needed.next_power_of_two());
eco.extend_from_byte_slice(self.as_slice());
eco.extend_from_byte_slice(bytes);
*self = Self::from_eco(eco);
}
}
VariantMut::Spilled(spilled) => {
spilled.extend_from_byte_slice(bytes);
}
}
}
#[inline]
pub fn insert_slice(&mut self, index: usize, bytes: &[u8]) {
match self.variant_mut() {
VariantMut::Inline(inline) => {
if inline.insert_slice(index, bytes).is_err() {
let needed = inline.len() + bytes.len();
let mut eco = EcoVec::with_capacity(needed.next_power_of_two());
let (a, b) = inline.as_slice().split_at(index);
eco.extend_from_byte_slice(a);
eco.extend_from_byte_slice(bytes);
eco.extend_from_byte_slice(b);
*self = Self::from_eco(eco);
}
}
VariantMut::Spilled(spilled) => {
spilled.splice(index..index, bytes.iter().copied());
}
}
}
#[inline]
pub fn clear(&mut self) {
match self.variant_mut() {
VariantMut::Inline(inline) => inline.clear(),
VariantMut::Spilled(spilled) => spilled.clear(),
}
}
#[inline]
pub fn truncate(&mut self, target: usize) {
match self.variant_mut() {
VariantMut::Inline(inline) => inline.truncate(target),
VariantMut::Spilled(spilled) => spilled.truncate(target),
}
}
#[inline]
pub fn remove_range<R>(&mut self, range: R)
where
R: RangeBounds<usize>,
{
match self.variant_mut() {
VariantMut::Inline(inline) => inline.remove_range(range),
VariantMut::Spilled(spilled) => {
spilled.drain(range);
}
}
}
}
impl DynamicVec {
// If this returns true, guarantees that `self.0.inline` is initialized.
// Otherwise, guarantees that `self.0.spilled` is initialized.
#[inline]
fn is_inline(&self) -> bool {
// Safety:
// We always initialize tagged_len, even for the `EcoVec` variant. For
// the inline variant the highest-order bit is always `1`. For the
// spilled variant, it is initialized with `0` and cannot deviate from
// that because the EcoVec's `len` field is bounded by `isize::MAX`. (At
// least on 64-bit little endian; on 32-bit or big-endian the EcoVec
// and tagged_len fields don't even overlap, meaning tagged_len stays at
// its initial value.)
unsafe { self.0.inline.tagged_len & LEN_TAG != 0 }
}
#[inline]
fn variant(&self) -> Variant<'_> {
unsafe {
// Safety:
// We access the respective variant only if the check passes.
if self.is_inline() {
Variant::Inline(&self.0.inline)
} else {
Variant::Spilled(&self.0.spilled)
}
}
}
#[inline]
fn variant_mut(&mut self) -> VariantMut<'_> {
unsafe {
// Safety:
// We access the respective variant only if the check passes.
if self.is_inline() {
VariantMut::Inline(&mut self.0.inline)
} else {
VariantMut::Spilled(&mut self.0.spilled)
}
}
}
}
impl Clone for DynamicVec {
#[inline]
fn clone(&self) -> Self {
match self.variant() {
Variant::Inline(inline) => Self::from_inline(*inline),
Variant::Spilled(spilled) => Self::from_eco(spilled.clone()),
}
}
}
impl Drop for DynamicVec {
#[inline]
fn drop(&mut self) {
if let VariantMut::Spilled(spilled) = self.variant_mut() {
unsafe {
// Safety: We are guaranteed to have a valid `EcoVec`.
ptr::drop_in_place(spilled);
}
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub(crate) struct InlineVec {
/// Storage!
buf: [u8; LIMIT],
/// Invariant: After masking off the tag, never exceeds LIMIT.
tagged_len: u8,
}
impl InlineVec {
#[inline]
pub const fn new() -> Self {
// Safety: Trivially, 0 <= LIMIT
unsafe { Self::from_buf([0; LIMIT], 0) }
}
#[inline]
pub const fn from_slice(bytes: &[u8]) -> Result<Self, ()> {
let len = bytes.len();
if len > LIMIT {
return Err(());
}
let mut buf = [0; LIMIT];
let mut i = 0;
while i < len {
buf[i] = bytes[i];
i += 1;
}
// Safety: If len > LIMIT, Err was returned earlier.
unsafe { Ok(Self::from_buf(buf, len)) }
}
/// The given length may not exceed LIMIT.
#[inline]
pub const unsafe fn from_buf(buf: [u8; LIMIT], len: usize) -> Self {
debug_assert!(len <= LIMIT);
Self { buf, tagged_len: len as u8 | LEN_TAG }
}
#[inline]
pub fn len(&self) -> usize {
usize::from(self.tagged_len & LEN_MASK)
}
/// The given length may not exceed LIMIT.
#[inline]
unsafe fn set_len(&mut self, len: usize) {
debug_assert!(len <= LIMIT);
self.tagged_len = len as u8 | LEN_TAG;
}
#[inline]
pub fn as_slice(&self) -> &[u8] {
// Safety: We have the invariant `len <= LIMIT`.
unsafe { self.buf.get_unchecked(..self.len()) }
}
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [u8] {
// Safety: We have the invariant `len <= LIMIT`.
let len = self.len();
unsafe { self.buf.get_unchecked_mut(..len) }
}
#[inline]
pub fn clear(&mut self) {
unsafe {
// Safety: Trivially, `0 <= LIMIT`.
self.set_len(0);
}
}
#[inline]
pub fn push(&mut self, byte: u8) -> Result<(), ()> {
let len = self.len();
if let Some(slot) = self.buf.get_mut(len) {
*slot = byte;
unsafe {
// Safety: The `get_mut` call guarantees that `len < LIMIT`.
self.set_len(len + 1);
}
Ok(())
} else {
Err(())
}
}
#[inline]
pub fn extend_from_slice(&mut self, bytes: &[u8]) -> Result<(), ()> {
let len = self.len();
let grown = len + bytes.len();
if let Some(segment) = self.buf.get_mut(len..grown) {
segment.copy_from_slice(bytes);
unsafe {
// Safety: The `get_mut` call guarantees that `grown <= LIMIT`.
self.set_len(grown);
}
Ok(())
} else {
Err(())
}
}
#[inline]
pub fn insert_slice(&mut self, index: usize, bytes: &[u8]) -> Result<(), ()> {
let len = self.len();
assert!(index <= len, "index {index} out of range for slice of length {len}");
let grown = len + bytes.len();
let tail_len = len - index;
if grown <= LIMIT {
let ptr = self.buf.as_mut_ptr();
unsafe {
// Safety: Checked that `index <= len` and
// `len + bytes.len() == grown < LIMIT`.
core::ptr::copy(ptr.add(index), ptr.add(index + bytes.len()), tail_len);
// Safety: Checked that `index <= len` and
// `len + bytes.len() == grown < LIMIT`.
core::ptr::copy_nonoverlapping(
bytes.as_ptr(),
ptr.add(index),
bytes.len(),
);
// Safety: Checked that `grown < LIMIT`
self.set_len(grown);
}
Ok(())
} else {
Err(())
}
}
#[inline]
pub fn truncate(&mut self, target: usize) {
if target < self.len() {
unsafe {
// Safety: Checked that it's smaller than the current length,
// which cannot exceed LIMIT itself.
self.set_len(target);
}
}
}
#[inline]
pub fn remove_range<R>(&mut self, range: R)
where
R: RangeBounds<usize>,
{
let len = self.len();
let range = crate::vendor::slice::range(range, ..len);
let tail_len = len - range.end;
let target = len - range.len();
let ptr = self.buf.as_mut_ptr();
unsafe {
// Safety: The range is in bounds
core::ptr::copy(ptr.add(range.end), ptr.add(range.start), tail_len);
// Safety: Checked that it's smaller than the current length,
// which cannot exceed LIMIT itself.
self.set_len(target);
}
}
}