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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
use core::cmp;
use core::fmt;
use core::hash;
use core::marker::PhantomData;
use core::mem;
use core::num::NonZeroU64;
use core::ops::{Deref, DerefMut, Index, IndexMut};
use core::ptr;
use core::slice::{self, SliceIndex};
use swim_mem::block::{Block, Layout, ZSP};
use swim_mem::alloc::{Hold, Holder, HoldError, AllocTag, Stow, TryClone, CloneIntoHold};
use swim_mem::lease::PtrBuf;
use crate::item::{Item, Value};

/// `Value` variant representing a collection of `Item`s.
#[derive(Eq, Ord)]
#[repr(C)]
pub struct Record<'a> {
    /// Discriminant with a type between `RECORD_TYPE_MIN` and `RECORD_TYPE_MAX`
    /// at the lowest byte address.
    ///
    /// ```text
    /// 0        1        2        3        4        5        6        7        8
    /// +--------+--------+--------+--------+--------+--------+--------+--------+
    /// |  type  |      0 |      0 |      0 |      0 |      0 |      0 |      0 |
    /// +--------+--------+--------+--------+--------+--------+--------+--------+
    /// ```
    _0: NonZeroU64,
    /// Raw pointer to either a PtrBuf<'a, Item<'a>>, or an empty placeholder allocation.
    _1: *mut Item<'a>,
    /// Variant over allocation lifetime.
    lifetime: PhantomData<&'a ()>,
}

impl<'a> Record<'a> {
    pub fn try_hold_slice<'b>(hold: &dyn Hold<'a>, data: &[Item<'b>]) -> Result<Record<'a>, HoldError> {
        unsafe {
            let len = data.len();
            if len == 0 {
                let block = hold.alloc(Layout::empty())?;
                Ok(Record {
                    _0: NonZeroU64::new_unchecked(Value::discriminant(Value::RECORD0_TYPE)),
                    _1: block.into_raw() as *mut Item<'a>,
                    lifetime: PhantomData,
                })
            } else {
                let buf = PtrBuf::try_hold_clone(hold, data)?;
                Ok(Record {
                    _0: NonZeroU64::new_unchecked(Value::discriminant(Value::RECORD_TYPE)),
                    _1: PtrBuf::into_raw(buf),
                    lifetime: PhantomData,
                })
            }
        }
    }

    pub fn hold_slice<'b>(hold: &dyn Hold<'a>, data: &[Item<'b>]) -> Record<'a> {
        Record::try_hold_slice(hold, data).unwrap()
    }

    pub fn from_slice<'b>(data: &[Item<'b>]) -> Record<'a> {
        Record::hold_slice(Hold::global(), data)
    }

    /// Returns a pointer to the tag in the first byte of this `Record`.
    #[inline(always)]
    pub(crate) unsafe fn tag_ptr(&self) -> *mut u8 {
        mem::transmute::<&Record<'a>, *mut u8>(self)
    }

    /// Returns the tag from the first byte of this `Record`.
    #[inline(always)]
    pub(crate) fn tag(&self) -> u8 {
        unsafe { *self.tag_ptr() }
    }

    /// Returns the type tag from the low 7 bits of the first byte of this `Record`.
    #[inline(always)]
    pub(crate) fn type_tag(&self) -> u8 {
        self.tag() & Value::TYPE_MASK
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        let type_tag = self.type_tag();
        if type_tag == Value::RECORD_TYPE {
            // Reconstitute a reference to the buffer lease.
            let buf = unsafe { mem::transmute::<_, &PtrBuf<'a, Item<'a>>>(&self._1) };
            // Return whether or not the buffer resident is empty.
            buf.is_empty()
        } else if type_tag == Value::RECORD0_TYPE {
            true
        } else {
            unreachable!();
        }
    }

    #[inline]
    pub fn len(&self) -> usize {
        let type_tag = self.type_tag();
        if type_tag == Value::RECORD_TYPE {
            // Reconstitute a reference to the buffer lease.
            let buf = unsafe { mem::transmute::<_, &PtrBuf<'a, Item<'a>>>(&self._1) };
            // Return the length of the buffer resident.
            buf.len()
        } else if type_tag == Value::RECORD0_TYPE {
            0
        } else {
            unreachable!();
        }
    }

    #[inline]
    pub fn cap(&self) -> usize {
        let type_tag = self.type_tag();
        if type_tag == Value::RECORD_TYPE {
            // Reconstitute a reference to the buffer lease.
            let buf = unsafe { mem::transmute::<_, &PtrBuf<'a, Item<'a>>>(&self._1) };
            // Return the capacity of the buffer resident.
            buf.cap()
        } else if type_tag == Value::RECORD0_TYPE {
            0
        } else {
            unreachable!();
        }
    }

    /// Upcasts this `Record` reference to a `Value` reference.
    #[inline]
    pub fn as_value(&self) -> &Value<'a> {
        unsafe { mem::transmute::<&Record<'a>, &Value<'a>>(self) }
    }

    /// Upcasts this `Record` reference to a mutable `Value` reference.
    #[inline]
    pub fn as_mut_value(&mut self) -> &mut Value<'a> {
        unsafe { mem::transmute::<&mut Record<'a>, &mut Value<'a>>(self) }
    }

    /// Upcasts this `Record` to a `Value`.
    #[inline]
    pub fn into_value(self) -> Value<'a> {
        unsafe { mem::transmute::<Record<'a>, Value<'a>>(self) }
    }

    /// Upcasts this `Record` to an `Item`.
    #[inline]
    pub fn into_item(self) -> Item<'a> {
        Item::from_value(self.into_value())
    }

    pub fn as_slice(&self) -> &[Item<'a>] {
        let type_tag = self.type_tag();
        if type_tag == Value::RECORD_TYPE {
            // Reconstitute a reference to the buffer lease.
            let buf = unsafe { mem::transmute::<_, &PtrBuf<'a, Item<'a>>>(&self._1) };
            // Return the resident slice.
            buf.as_slice()
        } else if type_tag == Value::RECORD0_TYPE {
            // Return an empty slice.
            unsafe { slice::from_raw_parts(ZSP as *const Item<'a>, 0) }
        } else {
            unreachable!();
        }
    }

    pub fn as_mut_slice(&mut self) -> &mut [Item<'a>] {
        let type_tag = self.type_tag();
        if type_tag == Value::RECORD_TYPE {
            // Reconstitute a reference to the buffer lease.
            let buf = unsafe { mem::transmute::<_, &mut PtrBuf<'a, Item<'a>>>(&mut self._1) };
            // Return the resident slice.
            buf.as_mut_slice()
        } else if type_tag == Value::RECORD0_TYPE {
            // Return an empty slice.
            unsafe { slice::from_raw_parts_mut(ZSP as *mut Item<'a>, 0) }
        } else {
            unreachable!();
        }
    }

    pub fn try_reserve(&mut self, ext: usize) -> Result<(), HoldError> {
        unsafe {
            let tag = self.tag();
            let type_tag = tag & Value::TYPE_MASK;
            if type_tag == Value::RECORD_TYPE {
                // Reconstitute a reference to the buffer lease.
                let buf = mem::transmute::<_, &mut PtrBuf<'a, Item<'a>>>(&mut self._1);
                // Reserve capacity in the buffer.
                buf.try_reserve(ext)
            } else if type_tag == Value::RECORD0_TYPE {
                if ext != 0 {
                    // Reconstruct the placeholder allocation block.
                    let block = Block::from_raw_parts(self._1 as *mut u8, 0);
                    // Get a reference to the hold that allocates this value.
                    let hold = AllocTag::from_ptr(self._1 as *mut u8).holder();
                    // Allocate a new buffer in the hold.
                    let buf = PtrBuf::try_hold_cap(hold, ext)?;
                    // Modify the discriminant tag with the new record type.
                    let tag = tag & Value::ATTR_FLAG | Value::RECORD_TYPE;
                    // Write the new discriminant.
                    ptr::write(&mut self._0, NonZeroU64::new_unchecked(Value::discriminant(tag)));
                    // Write the new data pointer.
                    ptr::write(&mut self._1, PtrBuf::into_raw(buf));
                    // Deallocate the placeholder block.
                    hold.dealloc(block);
                }
                Ok(())
            } else {
                unreachable!();
            }
        }
    }

    pub fn reserve(&mut self, ext: usize) {
        self.try_reserve(ext).unwrap();
    }

    pub fn try_reserve_exact(&mut self, ext: usize) -> Result<(), HoldError> {
        unsafe {
            let tag = self.tag();
            let type_tag = tag & Value::TYPE_MASK;
            if type_tag == Value::RECORD_TYPE {
                // Reconstitute a reference to the buffer lease.
                let buf = mem::transmute::<_, &mut PtrBuf<'a, Item<'a>>>(&mut self._1);
                // Reserve capacity in the buffer.
                buf.try_reserve_exact(ext)
            } else if type_tag == Value::RECORD0_TYPE {
                if ext != 0 {
                    // Reconstruct the placeholder allocation block.
                    let block = Block::from_raw_parts(self._1 as *mut u8, 0);
                    // Get a reference to the hold that allocates this value.
                    let hold = AllocTag::from_ptr(self._1 as *mut u8).holder();
                    // Allocate a new buffer in the hold.
                    let buf = PtrBuf::try_hold_cap(hold, ext)?;
                    // Modify the discriminant tag with the new record type.
                    let tag = tag & Value::ATTR_FLAG | Value::RECORD_TYPE;
                    // Write the new discriminant.
                    ptr::write(&mut self._0, NonZeroU64::new_unchecked(Value::discriminant(tag)));
                    // Write the new data pointer.
                    ptr::write(&mut self._1, PtrBuf::into_raw(buf));
                    // Deallocate the placeholder block.
                    hold.dealloc(block);
                }
                Ok(())
            } else {
                unreachable!();
            }
        }
    }

    pub fn reserve_exact(&mut self, ext: usize) {
        self.try_reserve_exact(ext).unwrap();
    }

    pub fn try_reserve_in_place(&mut self, ext: usize) -> Result<(), HoldError> {
        unsafe {
            let type_tag = self.type_tag();
            if type_tag == Value::RECORD_TYPE {
                // Reconstitute a reference to the buffer lease.
                let buf = mem::transmute::<_, &mut PtrBuf<'a, Item<'a>>>(&mut self._1);
                // Reserve capacity in the buffer.
                buf.try_reserve_in_place(ext)
            } else if type_tag == Value::RECORD0_TYPE {
                if ext == 0 {
                    Ok(())
                } else {
                    Err(HoldError::Unsupported("resize from empty"))
                }
            } else {
                unreachable!();
            }
        }
    }

    pub fn try_reserve_in_place_exact(&mut self, ext: usize) -> Result<(), HoldError> {
        unsafe {
            let type_tag = self.type_tag();
            if type_tag == Value::RECORD_TYPE {
                // Reconstitute a reference to the buffer lease.
                let buf = mem::transmute::<_, &mut PtrBuf<'a, Item<'a>>>(&mut self._1);
                // Reserve capacity in the buffer.
                buf.try_reserve_in_place_exact(ext)
            } else if type_tag == Value::RECORD0_TYPE {
                if ext == 0 {
                    Ok(())
                } else {
                    Err(HoldError::Unsupported("resize from empty"))
                }
            } else {
                unreachable!();
            }
        }
    }

    pub fn try_push(&mut self, item: Item<'a>) -> Result<(), HoldError> {
        unsafe {
            self.try_reserve(1)?;
            let buf = mem::transmute::<_, &mut PtrBuf<'a, Item<'a>>>(&mut self._1);
            let len = buf.len();
            let data = buf.as_mut_ptr().wrapping_add(len);
            ptr::write(data, item);
            buf.set_len(len.wrapping_add(1));
            Ok(())
        }
    }

    pub fn push(&mut self, item: Item<'a>) {
        self.try_push(item).unwrap();
    }

    pub fn try_insert(&mut self, index: usize, item: Item<'a>) -> Result<(), HoldError> {
        unsafe {
            let len = self.len();
            assert!(index <= len);
            self.try_reserve(1)?;
            let buf = mem::transmute::<_, &mut PtrBuf<'a, Item<'a>>>(&mut self._1);
            let data = buf.as_mut_ptr().wrapping_add(len);
            ptr::copy(data, data.wrapping_add(1), len.wrapping_sub(index));
            ptr::write(data, item);
            buf.set_len(len.wrapping_add(1));
            Ok(())
        }
    }

    pub fn insert(&mut self, index: usize, item: Item<'a>) {
        self.try_insert(index, item).unwrap();
    }

    pub fn pop(&mut self) -> Option<Item<'a>> {
        unsafe {
            let len = self.len();
            if len != 0 {
                let buf = mem::transmute::<_, &mut PtrBuf<'a, Item<'a>>>(&mut self._1);
                let len = len.wrapping_sub(1);
                buf.set_len(len);
                let data = buf.as_ptr().wrapping_add(len);
                Some(ptr::read(data))
            } else {
                None
            }
        }
    }

    pub fn remove(&mut self, index: usize) -> Item<'a> {
        unsafe {
            let len = self.len();
            assert!(index <= len);
            let buf = mem::transmute::<_, &mut PtrBuf<'a, Item<'a>>>(&mut self._1);
            let data = buf.as_mut_ptr().wrapping_add(index);
            let item = ptr::read(data);
            ptr::copy(data.wrapping_add(1), data, len.wrapping_sub(index).wrapping_add(1));
            buf.set_len(len.wrapping_sub(1));
            item
        }
    }

    pub fn truncate(&mut self, new_len: usize) {
        unsafe {
            let old_len = self.len();
            if old_len > new_len {
                let buf = mem::transmute::<_, &mut PtrBuf<'a, Item<'a>>>(&mut self._1);
                let tail = buf.as_mut_ptr().wrapping_add(new_len);
                ptr::drop_in_place(slice::from_raw_parts_mut(tail, old_len.wrapping_sub(new_len)));
                buf.set_len(new_len);
            }
        }
    }

    pub fn clear(&mut self) {
        self.truncate(0);
    }

    pub(crate) unsafe fn dealloc(&mut self) {
        let type_tag = self.type_tag();
        if type_tag == Value::RECORD_TYPE {
            // Reconsitute the Hold-allocated buffer.
            let buf = PtrBuf::<'a, Item<'a>, ()>::from_raw(self._1);
            // And drop it.
            mem::drop(buf);
        } else if type_tag == Value::RECORD0_TYPE {
            // Reconstruct the empty placeholder allocation block.
            let block = Block::from_raw_parts(self._1 as *mut u8, 0);
            // Get a pointer to the AllocTag of the block,
            // and use it to deallocate the empty placeholder block.
            AllocTag::from_ptr(self._1 as *mut u8).dealloc(block);
        } else {
            unreachable!();
        }
    }
}

impl<'a> Holder<'a> for Record<'a> {
    #[inline]
    fn holder(&self) -> &'a dyn Hold<'a> {
        let type_tag = self.type_tag();
        if type_tag == Value::RECORD_TYPE {
            // Reconstitute a reference to the buffer lease.
            let buf = unsafe { mem::transmute::<_, &PtrBuf<'a, Item<'a>>>(&self._1) };
            // Return the buffer resident holder.
            buf.holder()
        } else if type_tag == Value::RECORD0_TYPE {
            // Return the empty placeholder allocation holder.
            AllocTag::from_ptr(self._1 as *mut u8).holder()
        } else {
            unreachable!();
        }
    }
}

impl<'a> Deref for Record<'a> {
    type Target = [Item<'a>];

    #[inline]
    fn deref(&self) -> &[Item<'a>] {
        self.as_slice()
    }
}

impl<'a> DerefMut for Record<'a> {
    #[inline]
    fn deref_mut(&mut self) -> &mut [Item<'a>] {
        self.as_mut_slice()
    }
}

impl<'a> AsRef<Value<'a>> for Record<'a> {
    #[inline]
    fn as_ref(&self) -> &Value<'a> {
        self.as_value()
    }
}

impl<'a> AsRef<[Item<'a>]> for Record<'a> {
    #[inline]
    fn as_ref(&self) -> &[Item<'a>] {
        self.as_slice()
    }
}

impl<'a> AsMut<Value<'a>> for Record<'a> {
    #[inline]
    fn as_mut(&mut self) -> &mut Value<'a> {
        self.as_mut_value()
    }
}

impl<'a> AsMut<[Item<'a>]> for Record<'a> {
    #[inline]
    fn as_mut(&mut self) -> &mut [Item<'a>] {
        self.as_mut_slice()
    }
}

impl<'a, Idx: SliceIndex<[Item<'a>]> + 'a> Index<Idx> for Record<'a> {
    type Output = Idx::Output;

    #[inline]
    fn index(&self, index: Idx) -> &Idx::Output {
        self.as_slice().index(index)
    }
}

impl<'a, Idx: SliceIndex<[Item<'a>]> + 'a> IndexMut<Idx> for Record<'a> {
    #[inline]
    fn index_mut(&mut self, index: Idx) -> &mut Idx::Output {
        self.as_mut_slice().index_mut(index)
    }
}

impl<'a> PartialEq for Record<'a> {
    #[inline]
    fn eq(&self, that: &Record<'a>) -> bool {
        self.as_slice().eq(that.as_slice())
    }

    #[inline]
    fn ne(&self, that: &Record<'a>) -> bool {
        self.as_slice().ne(that.as_slice())
    }
}

impl<'a> cmp::PartialOrd<Record<'a>> for Record<'a> {
    #[inline]
    fn partial_cmp(&self, that: &Record<'a>) -> Option<cmp::Ordering> {
        self.as_slice().partial_cmp(that.as_slice())
    }

    #[inline]
    fn lt(&self, that: &Record<'a>) -> bool {
        self.as_slice().lt(that.as_slice())
    }

    #[inline]
    fn le(&self, that: &Record<'a>) -> bool {
        self.as_slice().le(that.as_slice())
    }

    #[inline]
    fn ge(&self, that: &Record<'a>) -> bool {
        self.as_slice().ge(that.as_slice())
    }

    #[inline]
    fn gt(&self, that: &Record<'a>) -> bool {
        self.as_slice().gt(that.as_slice())
    }
}

impl<'a> hash::Hash for Record<'a> {
    #[inline]
    fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
        self.as_slice().hash(hasher);
    }
}

impl<'a> fmt::Debug for Record<'a> {
    fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
        unimplemented!(); // TODO
    }
}

impl<'a> Clone for Record<'a> {
    fn clone(&self) -> Record<'a> {
        Record::from_slice(self.as_slice())
    }
}

impl<'a> TryClone for Record<'a> {
    fn try_clone(&self) -> Result<Record<'a>, HoldError> {
        self.try_clone_into_hold(self.holder())
    }
}

impl<'a, 'b> CloneIntoHold<'a, Record<'a>> for Record<'b> {
    fn try_clone_into_hold(&self, hold: &Hold<'a>) -> Result<Record<'a>, HoldError> {
        Record::try_hold_slice(hold, self.as_slice())
    }
}

impl<'a, 'b> Stow<'b, Record<'b>> for Record<'a> {
    unsafe fn stow(src: *mut Record<'a>, dst: *mut Record<'b>, hold: &Hold<'b>) -> Result<(), HoldError> {
        let len = (*src).len();
        if len == 0 {
            let block = hold.alloc(Layout::empty())?;
            ptr::write(&mut (*dst)._0, NonZeroU64::new_unchecked(Value::discriminant(Value::RECORD0_TYPE)));
            ptr::write(&mut (*dst)._1, block.into_raw() as *mut Item<'b>);
        } else {
            let buf = PtrBuf::try_hold_clone(hold, (*src).as_slice())?;
            ptr::write(&mut (*dst)._0, NonZeroU64::new_unchecked(Value::discriminant(Value::RECORD_TYPE)));
            ptr::write(&mut (*dst)._1, PtrBuf::into_raw(buf));
        }
        Ok(())
    }

    unsafe fn unstow(_src: *mut Record<'a>, _dst: *mut Record<'b>) {
        unimplemented!();
    }
}

impl<'a> Drop for Record<'a> {
    fn drop(&mut self) {
        unsafe { self.dealloc(); }
    }
}