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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
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, Add, AddAssign};
use core::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
use core::ptr;
use core::slice;
use core::str;
use swim_mem::block::{Block, Layout};
use swim_mem::alloc::{Hold, Holder, HoldError, AllocTag, Stow, TryClone, CloneIntoHold};
use swim_mem::lease::PtrString;
use crate::item::{Item, Value};

/// `Value` variant representing a Unicode string.
///
/// # Examples
///
/// Create a `Text` value from a primitive `str`:
///
/// ```
/// # extern crate swim_c_rt;
/// # use swim_structure::item::Text;
/// let value = Text::from_str("Hello");
/// # assert_eq!(value.as_str(), "Hello");
/// ```
///
/// Get a reference to the primitive `str` owned by a `Text` value:
///
/// ```
/// # extern crate swim_c_rt;
/// # use swim_structure::item::Text;
/// let value = Text::from_str("Hello, world!");
/// let primitive = value.as_str();
/// # assert_eq!(primitive, "Hello, world!");
/// ```
///
/// Concatenate a `Text` value with another `str` using the `+` operator:
///
/// ```
/// # extern crate swim_c_rt;
/// # use swim_structure::item::Text;
/// let value = Text::from_str("Hello");
/// let message = value + " " + "world";
/// # assert_eq!(message.len(), 11);
#[derive(Eq, Ord)]
#[repr(C)]
pub struct Text<'a> {
    /// Discriminant with a type between `TEXT_TYPE_MIN` and `TEXT_TYPE_MAX`
    /// at the lowest byte address, and an optional embedded string in the
    /// subsequent 7 bytes.
    ///
    /// ```text
    /// 0        1        2        3        4        5        6        7        8
    /// +--------+--------+--------+--------+--------+--------+--------+--------+
    /// |  type  |     c0 |     c1 |     c2 |     c3 |     c4 |     c5 |     c6 |
    /// +--------+--------+--------+--------+--------+--------+--------+--------+
    /// ```
    _0: NonZeroU64,
    /// Raw pointer to either a PtrString<'a>, or an empty allocation.
    _1: *mut u8,
    /// Variant over allocation lifetime.
    lifetime: PhantomData<PtrString<'a>>,
}

impl<'a> Text<'a> {
    pub fn try_hold_str(hold: &dyn Hold<'a>, data: &str) -> Result<Text<'a>, HoldError> {
        unsafe {
            let len = data.len();
            if len <= Value::TEXT_EMBED_MAX as usize {
                let block = hold.alloc(Layout::empty())?;
                let mut value = Text {
                    _0: NonZeroU64::new_unchecked(Value::discriminant(Value::TEXT0_TYPE + len as u8)),
                    _1: block.into_raw(),
                    lifetime: PhantomData,
                };
                let embed_ptr = (&mut value._0 as *mut NonZeroU64 as *mut u8).wrapping_add(1);
                ptr::copy_nonoverlapping(data.as_ptr(), embed_ptr, len);
                Ok(value)
            } else {
                let string = PtrString::try_hold_copy(hold, data)?;
                Ok(Text {
                    _0: NonZeroU64::new_unchecked(Value::discriminant(Value::TEXT_TYPE)),
                    _1: PtrString::into_raw(string),
                    lifetime: PhantomData,
                })
            }
        }
    }

    pub fn hold_str(hold: &dyn Hold<'a>, data: &str) -> Text<'a> {
        Text::try_hold_str(hold, data).unwrap()
    }

    pub fn from_str(data: &str) -> Text<'a> {
        Text::hold_str(Hold::global(), data)
    }

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

    /// Returns the tag from the first byte of this `Text`.
    #[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 `Text`.
    #[inline(always)]
    pub(crate) fn type_tag(&self) -> u8 {
        self.tag() & Value::TYPE_MASK
    }

    pub fn is_empty(&self) -> bool {
        let type_tag = self.type_tag();
        if type_tag == Value::TEXT_TYPE {
            // Reconstitute a reference to the string lease.
            let string = unsafe { mem::transmute::<_, &PtrString<'a>>(&self._1) };
            // Return whether or not the string resident is empty.
            string.is_empty()
        } else {
            match type_tag.checked_sub(Value::TEXT0_TYPE) {
                // Return whether or not the embedded string is empty.
                Some(len) => len == 0,
                None => unreachable!(),
            }
        }
    }

    pub fn len(&self) -> usize {
        let type_tag = self.type_tag();
        if type_tag == Value::TEXT_TYPE {
            // Reconstitute a reference to the string lease.
            let string = unsafe { mem::transmute::<_, &PtrString<'a>>(&self._1) };
            // Return the length of the string resident.
            string.len()
        } else {
            match type_tag.checked_sub(Value::TEXT0_TYPE) {
                // Return the length of the embedded string.
                Some(len) => len as usize,
                None => unreachable!(),
            }
        }
    }

    pub unsafe fn set_len(&mut self, new_len: usize) {
        let type_tag = self.type_tag();
        if type_tag == Value::TEXT_TYPE {
            // Reconstitute a mutable reference to the string lease.
            let string = mem::transmute::<_, &mut PtrString<'a>>(&mut self._1);
            // Set the length of the string resident.
            string.set_len(new_len)
        } else {
            assert!((new_len as u8) < Value::TEXT_EMBED_MAX);
            // Make a discriminant tag with the new embedded string length.
            let tag = Value::TEXT0_TYPE.wrapping_add(new_len as u8);
            // Splice the updated tag into the discriminant.
            let discriminant = self._0.get() & !Value::discriminant(Value::TYPE_MASK) | Value::discriminant(tag);
            // Write the new discriminant.
            ptr::write(&mut self._0, NonZeroU64::new_unchecked(discriminant));
        }
    }

    pub fn cap(&self) -> usize {
        let type_tag = self.type_tag();
        if type_tag == Value::TEXT_TYPE {
            // Reconstitute a reference to the string lease.
            let string = unsafe { mem::transmute::<_, &PtrString<'a>>(&self._1) };
            // Return the capacity of the string resident.
            string.cap()
        } else {
            match type_tag.checked_sub(Value::TEXT0_TYPE) {
                // Return the capacity of the embedded string.
                Some(_) => Value::TEXT_EMBED_MAX as usize,
                None => unreachable!(),
            }
        }
    }

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

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

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

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

    pub fn as_ptr(&self) -> *const u8 {
        let type_tag = self.type_tag();
        if type_tag == Value::TEXT_TYPE {
            // Reconstitute a reference to the string lease.
            let string = unsafe { mem::transmute::<_, &PtrString<'a>>(&self._1) };
            // Return a pointer to the resident data.
            string.as_ptr()
        } else {
            match type_tag.checked_sub(Value::TEXT0_TYPE) {
                // Return a pointer to the embedded string.
                Some(_) => (&self._0 as *const NonZeroU64 as *const u8).wrapping_add(1),
                None => unreachable!(),
            }
        }
    }

    pub fn as_mut_ptr(&mut self) -> *mut u8 {
        let type_tag = self.type_tag();
        if type_tag == Value::TEXT_TYPE {
            // Reconstitute a mutable reference to the string lease.
            let string = unsafe { mem::transmute::<_, &mut PtrString<'a>>(&mut self._1) };
            // Return a mutable pointer to the resident data.
            string.as_mut_ptr()
        } else {
            match type_tag.checked_sub(Value::TEXT0_TYPE) {
                // Return a mutable pointer to the embedded string.
                Some(_) => (&mut self._0 as *mut NonZeroU64 as *mut u8).wrapping_add(1),
                None => unreachable!(),
            }
        }
    }

    pub fn as_slice(&self) -> &[u8] {
        let type_tag = self.type_tag();
        if type_tag == Value::TEXT_TYPE {
            // Reconstitute a reference to the string lease.
            let string = unsafe { mem::transmute::<_, &PtrString<'a>>(&self._1) };
            // Return the resident slice.
            string.as_slice()
        } else {
            match type_tag.checked_sub(Value::TEXT0_TYPE) {
                Some(len) => {
                    // Get a pointer to the embedded string.
                    let embed_ptr = (&self._0 as *const NonZeroU64 as *const u8).wrapping_add(1);
                    // Return the embedded slice.
                    unsafe { slice::from_raw_parts(embed_ptr, len as usize) }
                },
                None => unreachable!(),
            }
        }
    }

    pub fn as_str(&self) -> &str {
        let type_tag = self.type_tag();
        if type_tag == Value::TEXT_TYPE {
            // Reconstitute a reference to the string lease.
            let string = unsafe { mem::transmute::<_, &PtrString<'a>>(&self._1) };
            // Return the resident string.
            string.as_str()
        } else {
            match type_tag.checked_sub(Value::TEXT0_TYPE) {
                Some(len) => {
                    // Get a pointer to the embedded string.
                    let embed_ptr = (&self._0 as *const NonZeroU64 as *const u8).wrapping_add(1);
                    // Return the embedded string.
                    unsafe { str::from_utf8_unchecked(slice::from_raw_parts(embed_ptr, len as usize)) }
                },
                None => unreachable!(),
            }
        }
    }

    pub fn as_mut_str(&mut self) -> &mut str {
        let type_tag = self.type_tag();
        if type_tag == Value::TEXT_TYPE {
            // Reconstitute a reference to the string lease.
            let string = unsafe { mem::transmute::<_, &mut PtrString<'a>>(&mut self._1) };
            // Return the resident string.
            string.as_mut_str()
        } else {
            match type_tag.checked_sub(Value::TEXT0_TYPE) {
                Some(len) => {
                    // Get a pointer to the embedded string.
                    let embed_ptr = (&mut self._0 as *mut NonZeroU64 as *mut u8).wrapping_add(1);
                    // Return the embedded string.
                    unsafe { str::from_utf8_unchecked_mut(slice::from_raw_parts_mut(embed_ptr, len as usize)) }
                },
                None => 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::TEXT_TYPE {
                // Reconstitute a reference to the string lease.
                let string = mem::transmute::<_, &mut PtrString<'a>>(&mut self._1);
                // Reserve capacity in the string.
                string.try_reserve(ext)
            } else {
                match type_tag.checked_sub(Value::TEXT0_TYPE) {
                    Some(len) => {
                        if (Value::TEXT_EMBED_MAX.wrapping_sub(len) as usize) < ext {
                            // Get a pointer to the embedded string.
                            let embed_ptr = (&self._0 as *const NonZeroU64 as *const u8).wrapping_add(1);
                            // 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 string in the hold.
                            let mut string = PtrString::try_hold_cap(hold, len as usize + ext)?;
                            // Copy the embedded slice into the new string.
                            ptr::copy_nonoverlapping(embed_ptr, string.as_mut_ptr(), len as usize);
                            // Set the length of the new string.
                            string.set_len(len as usize);
                            // Modify the discriminant tag with the new record type.
                            let tag = tag & Value::ATTR_FLAG | Value::TEXT_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, PtrString::into_raw(string));
                            // Deallocate the placeholder block.
                            hold.dealloc(block);
                        }
                        Ok(())
                    },
                    None => 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::TEXT_TYPE {
                // Reconstitute a reference to the string lease.
                let string = mem::transmute::<_, &mut PtrString<'a>>(&mut self._1);
                // Reserve capacity in the string.
                string.try_reserve_exact(ext)
            } else {
                match type_tag.checked_sub(Value::TEXT0_TYPE) {
                    Some(len) => {
                        if (Value::TEXT_EMBED_MAX.wrapping_sub(len) as usize) < ext {
                            // Get a pointer to the embedded string.
                            let embed_ptr = (&self._0 as *const NonZeroU64 as *const u8).wrapping_add(1);
                            // 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 string in the hold.
                            let mut string = PtrString::try_hold_cap(hold, len as usize + ext)?;
                            // Copy the embedded slice into the new string.
                            ptr::copy_nonoverlapping(embed_ptr, string.as_mut_ptr(), len as usize);
                            // Set the length of the new string.
                            string.set_len(len as usize);
                            // Modify the discriminant tag with the new record type.
                            let tag = tag & Value::ATTR_FLAG | Value::TEXT_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, PtrString::into_raw(string));
                            // Deallocate the placeholder block.
                            hold.dealloc(block);
                        }
                        Ok(())
                    },
                    None => 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::TEXT_TYPE {
                // Reconstitute a reference to the string lease.
                let string = mem::transmute::<_, &mut PtrString<'a>>(&mut self._1);
                // Reserve capacity in the string.
                string.try_reserve_in_place(ext)
            } else {
                match type_tag.checked_sub(Value::TEXT0_TYPE) {
                    Some(len) => {
                        if (Value::TEXT_EMBED_MAX.wrapping_sub(len) as usize) >= ext {
                            Ok(())
                        } else {
                            Err(HoldError::Oversized)
                        }
                    },
                    None => 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::TEXT_TYPE {
                // Reconstitute a reference to the string lease.
                let string = mem::transmute::<_, &mut PtrString<'a>>(&mut self._1);
                // Reserve capacity in the string.
                string.try_reserve_in_place_exact(ext)
            } else {
                match type_tag.checked_sub(Value::TEXT0_TYPE) {
                    Some(len) => {
                        if (Value::TEXT_EMBED_MAX.wrapping_sub(len) as usize) >= ext {
                            Ok(())
                        } else {
                            Err(HoldError::Oversized)
                        }
                    },
                    None => unreachable!(),
                }
            }
        }
    }

    pub fn try_push(&mut self, c: char) -> Result<(), HoldError> {
        unsafe {
            let mut bytes = [0u8; 4];
            let n = c.encode_utf8(&mut bytes).len();
            self.try_reserve(n)?;
            let len = self.len();
            let data = self.as_mut_ptr().wrapping_add(len);
            ptr::copy_nonoverlapping(bytes.as_ptr(), data, n);
            self.set_len(len.wrapping_add(n));
            Ok(())
        }
    }

    pub fn push(&mut self, c: char) {
        self.try_push(c).unwrap();
    }

    pub fn try_push_str(&mut self, s: &str) -> Result<(), HoldError> {
        unsafe {
            let n = s.len();
            self.try_reserve(n)?;
            let len = self.len();
            let data = self.as_mut_ptr().wrapping_add(len);
            ptr::copy_nonoverlapping(s.as_ptr(), data, n);
            self.set_len(len.wrapping_add(n));
            Ok(())
        }
    }

    pub fn push_str(&mut self, s: &str) {
        self.try_push_str(s).unwrap();
    }

    pub fn try_insert(&mut self, index: usize, c: char) -> Result<(), HoldError> {
        unsafe {
            assert!(self.as_str().is_char_boundary(index));
            let mut slice = [0u8; 4];
            let slice = c.encode_utf8(&mut slice).as_bytes();
            self.try_insert_slice(index, slice)
        }
    }

    pub fn insert(&mut self, index: usize, c: char) {
        self.try_insert(index, c).unwrap();
    }

    pub fn try_insert_str(&mut self, index: usize, s: &str) -> Result<(), HoldError> {
        unsafe {
            assert!(self.as_str().is_char_boundary(index));
            self.try_insert_slice(index, s.as_bytes())
        }
    }

    pub fn insert_str(&mut self, index: usize, s: &str) {
        self.try_insert_str(index, s).unwrap();
    }

    pub unsafe fn try_insert_slice(&mut self, index: usize, slice: &[u8]) -> Result<(), HoldError> {
        let n = slice.len();
        self.try_reserve(n)?;
        let len = self.len();
        let data = self.as_mut_ptr();
        ptr::copy(data.wrapping_add(index),
                  data.wrapping_add(index.wrapping_add(n)),
                  len.wrapping_sub(index));
        ptr::copy(slice.as_ptr(),
                  data.wrapping_add(index),
                  n);
        self.set_len(len.wrapping_add(n));
        Ok(())
    }

    pub unsafe fn insert_slice(&mut self, index: usize, slice: &[u8]) {
        self.try_insert_slice(index, slice).unwrap();
    }

    pub fn pop(&mut self) -> Option<char> {
        unsafe {
            let c = self.as_str().chars().rev().next()?;
            self.set_len(self.len().wrapping_sub(c.len_utf8()));
            Some(c)
        }
    }

    pub fn remove(&mut self, index: usize) -> char {
        unsafe {
            let c = self.as_str()[index..].chars().next().unwrap();
            let n = c.len_utf8();
            let next = index.wrapping_add(n);
            let len = self.len();
            let data = self.as_mut_ptr();
            ptr::copy(data.wrapping_add(next),
                      data.wrapping_add(index),
                      len.wrapping_sub(next));
            self.set_len(len.wrapping_sub(n));
            c
        }
    }

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

    pub(crate) unsafe fn dealloc(&mut self) {
        let type_tag = self.type_tag();
        if type_tag == Value::TEXT_TYPE {
            // Reconsitute the Hold-allocated string.
            let string = PtrString::<'a, ()>::from_raw(self._1);
            // And drop it.
            mem::drop(string);
        } else {
            match type_tag.checked_sub(Value::TEXT0_TYPE) {
                Some(_) => {
                    // Reconstruct the empty allocation block.
                    let block = Block::from_raw_parts(self._1, 0);
                    // Get a pointer to the AllocTag of the block,
                    // and use it to deallocate the empty block.
                    AllocTag::from_ptr(self._1).dealloc(block);
                },
                None => unreachable!(),
            }
        }
    }
}

impl<'a> Holder<'a> for Text<'a> {
    #[inline]
    fn holder(&self) -> &'a dyn Hold<'a> {
        let type_tag = self.type_tag();
        if type_tag == Value::TEXT_TYPE {
            // Reconstitute a reference to the string lease.
            let string = unsafe { mem::transmute::<_, &PtrString<'a>>(&self._1) };
            // Return the string resident holder.
            string.holder()
        } else {
            match type_tag.checked_sub(Value::TEXT0_TYPE) {
                // Return the embedded string holder.
                Some(_) => AllocTag::from_ptr(self._1).holder(),
                None => unreachable!(),
            }
        }
    }
}

impl<'a> Deref for Text<'a> {
    type Target = str;

    #[inline]
    fn deref(&self) -> &str {
        self.as_str()
    }
}

impl<'a> DerefMut for Text<'a> {
    #[inline]
    fn deref_mut(&mut self) -> &mut str {
        self.as_mut_str()
    }
}

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

impl<'a> AsRef<str> for Text<'a> {
    #[inline]
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

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

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

impl<'a> AsMut<str> for Text<'a> {
    #[inline]
    fn as_mut(&mut self) -> &mut str {
        self.as_mut_str()
    }
}

impl<'a> Index<Range<usize>> for Text<'a> {
    type Output = str;

    #[inline]
    fn index(&self, index: Range<usize>) -> &str {
        self.as_str().index(index)
    }
}

impl<'a> Index<RangeFrom<usize>> for Text<'a> {
    type Output = str;

    #[inline]
    fn index(&self, index: RangeFrom<usize>) -> &str {
        self.as_str().index(index)
    }
}

impl<'a> Index<RangeFull> for Text<'a> {
    type Output = str;

    #[inline]
    fn index(&self, index: RangeFull) -> &str {
        self.as_str().index(index)
    }
}

impl<'a> Index<RangeInclusive<usize>> for Text<'a> {
    type Output = str;

    #[inline]
    fn index(&self, index: RangeInclusive<usize>) -> &str {
        self.as_str().index(index)
    }
}

impl<'a> Index<RangeTo<usize>> for Text<'a> {
    type Output = str;

    #[inline]
    fn index(&self, index: RangeTo<usize>) -> &str {
        self.as_str().index(index)
    }
}

impl<'a> Index<RangeToInclusive<usize>> for Text<'a> {
    type Output = str;

    #[inline]
    fn index(&self, index: RangeToInclusive<usize>) -> &str {
        self.as_str().index(index)
    }
}

impl<'a> IndexMut<Range<usize>> for Text<'a> {
    #[inline]
    fn index_mut(&mut self, index: Range<usize>) -> &mut str {
        self.as_mut_str().index_mut(index)
    }
}

impl<'a> IndexMut<RangeFrom<usize>> for Text<'a> {
    #[inline]
    fn index_mut(&mut self, index: RangeFrom<usize>) -> &mut str {
        self.as_mut_str().index_mut(index)
    }
}

impl<'a> IndexMut<RangeFull> for Text<'a> {
    #[inline]
    fn index_mut(&mut self, index: RangeFull) -> &mut str {
        self.as_mut_str().index_mut(index)
    }
}

impl<'a> IndexMut<RangeInclusive<usize>> for Text<'a> {
    #[inline]
    fn index_mut(&mut self, index: RangeInclusive<usize>) -> &mut str {
        self.as_mut_str().index_mut(index)
    }
}

impl<'a> IndexMut<RangeTo<usize>> for Text<'a> {
    #[inline]
    fn index_mut(&mut self, index: RangeTo<usize>) -> &mut str {
        self.as_mut_str().index_mut(index)
    }
}

impl<'a> IndexMut<RangeToInclusive<usize>> for Text<'a> {
    #[inline]
    fn index_mut(&mut self, index: RangeToInclusive<usize>) -> &mut str {
        self.as_mut_str().index_mut(index)
    }
}

impl<'a, 'b> Add<&'b str> for Text<'a> {
    type Output = Text<'a>;

    #[inline]
    fn add(mut self, rhs: &'b str) -> Text<'a> {
        self.push_str(rhs);
        self
    }
}

impl<'a, 'b> AddAssign<&'b str> for Text<'a> {
    #[inline]
    fn add_assign(&mut self, rhs: &'b str) {
        self.push_str(rhs);
    }
}

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

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

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

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

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

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

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

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

impl<'a> fmt::Display for Text<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self.as_str(), f)
    }
}

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

impl<'a> Clone for Text<'a> {
    fn clone(&self) -> Text<'a> {
        Text::from_str(self.as_str())
    }
}

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

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

impl<'a, 'b> Stow<'b, Text<'b>> for Text<'a> {
    unsafe fn stow(src: *mut Text<'a>, dst: *mut Text<'b>, hold: &Hold<'b>) -> Result<(), HoldError> {
        let len = (*src).len();
        if len <= Value::TEXT_EMBED_MAX as usize {
            let block = hold.alloc(Layout::empty())?;
            ptr::write(&mut (*dst)._0, NonZeroU64::new_unchecked(Value::discriminant(Value::TEXT0_TYPE + len as u8)));
            ptr::write(&mut (*dst)._1, block.into_raw());
            let embed_ptr = (&mut (*dst)._0 as *mut NonZeroU64 as *mut u8).wrapping_add(1);
            ptr::copy_nonoverlapping((*src).as_ptr(), embed_ptr, len);
        } else {
            let string = PtrString::try_hold_copy(hold, (*src).as_str())?;
            ptr::write(&mut (*dst)._0, NonZeroU64::new_unchecked(Value::discriminant(Value::TEXT_TYPE)));
            ptr::write(&mut (*dst)._1, PtrString::into_raw(string));
        }
        Ok(())
    }

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

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