boxing 0.1.2

Easy-to-use, cross-platform implementations for NaN and ptr boxes
Documentation
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
//! A base-level sound implementation of a NaN Box, capable of storing pointers and integer values,
//! while providing minimal overhead. An effective primitive for implementing more usage-specific
//! boxes on top of.

use super::{NEG_QUIET_NAN, QUIET_NAN, SIGN_MASK};
use crate::nan::singlenan::SingleNaNF64;
use crate::utils::ArrayExt;
use sptr::Strict;
use std::fmt;
use std::mem::ManuallyDrop;
use std::num::NonZeroU8;

/// Types that can be easily stored in a [`RawBox`]. This trait is implemented for some common
/// base types that people may want to store, that have 'obvious' ways to write them into storage
/// and can't cause immediate UB if the wrong type is read.
///
/// This means [`char`], references, and most types larger than 4 aren't included, so as to not
/// potentially implement something that only works if certain assumptions are made, or is very
/// likely to panic. Pointers are included only because they're very common, and most 64-bit systems
/// cap pointers to 48-bit addresses anyways.
pub trait RawStore: Sized {
    /// Store an instance of this type into a [`Value`]. This must always be sound, no matter the
    /// value of `self`.
    fn to_val(self, value: &mut Value);

    /// Read an instance of this type out of a [`Value`]. This must always be sound, even if the
    /// `Value` actually stores an instance of a different type.
    fn from_val(value: &Value) -> Self;
}

impl RawStore for [u8; 6] {
    #[inline]
    fn to_val(self, value: &mut Value) {
        value.set_data(self);
    }

    #[inline]
    fn from_val(value: &Value) -> Self {
        *value.data()
    }
}

impl RawStore for bool {
    #[inline]
    fn to_val(self, value: &mut Value) {
        value.set_data([1].truncate_to());
    }

    #[inline]
    fn from_val(value: &Value) -> Self {
        value.data()[0] == 1
    }
}

macro_rules! int_store {
    ($ty:ty) => {
        impl RawStore for $ty {
            #[inline]
            fn to_val(self, value: &mut Value) {
                let bytes = self.to_ne_bytes();
                value.set_data(bytes.truncate_to());
            }

            #[inline]
            fn from_val(value: &Value) -> Self {
                <$ty>::from_ne_bytes(value.data().truncate_to())
            }
        }
    };
}

int_store!(u8);
int_store!(u16);
int_store!(u32);

int_store!(i8);
int_store!(i16);
int_store!(i32);

fn store_ptr<P: Strict + Copy>(value: &mut Value, ptr: P) {
    #[cfg(target_pointer_width = "32")]
    {
        let val = (value.mut_val() as *mut [u8; 6])
            .cast::<*mut T>()
            .byte_offset(2);

        unsafe { val.write(self) };
    }
    #[cfg(target_pointer_width = "64")]
    {
        assert!(
            ptr.addr() <= 0x0000_FFFF_FFFF_FFFF,
            "Pointer too large to store in NaN box"
        );

        // SAFETY: We ensure pointer range will fit in 6 bytes, then mask it to match required NaN header rules
        let val = (unsafe { value.whole_mut() } as *mut [u8; 8]).cast::<P>();

        let ptr = Strict::map_addr(ptr, |addr| {
            addr | (usize::from(value.header().into_raw()) << 48)
        });

        // SAFETY: The pointer was derived from a valid mutable reference, and is guaranteed aligned
        unsafe { val.write(ptr) };
    }
}

fn load_ptr<P: Strict>(value: &Value) -> P {
    #[cfg(target_pointer_width = "32")]
    {
        let val = (value.ref_val() as *const [u8; 6])
            .cast::<P>()
            .byte_offset(2);

        unsafe { val.read() }
    }
    #[cfg(target_pointer_width = "64")]
    {
        // SAFETY: We promise to use the byte range carefully
        let val = (unsafe { value.whole() } as *const [u8; 8]).cast::<P>();

        // SAFETY: The pointer returned by `whole` is guaranteed 8-byte aligned
        let ptr = unsafe { val.read() };
        Strict::map_addr(ptr, |addr| addr & 0x0000_FFFF_FFFF_FFFF)
    }
}

impl<T> RawStore for *const T {
    fn to_val(self, value: &mut Value) {
        store_ptr::<*const T>(value, self);
    }

    fn from_val(value: &Value) -> Self {
        load_ptr::<*const T>(value)
    }
}

impl<T> RawStore for *mut T {
    fn to_val(self, value: &mut Value) {
        store_ptr::<*mut T>(value, self);
    }

    fn from_val(value: &Value) -> Self {
        load_ptr::<*mut T>(value)
    }
}

#[derive(Copy, Clone, PartialEq, Eq)]
enum TagVal {
    _P1,
    _P2,
    _P3,
    _P4,
    _P5,
    _P6,
    _P7,

    _N1,
    _N2,
    _N3,
    _N4,
    _N5,
    _N6,
    _N7,
}

/// The 'tag' of a [`RawBox`] - this is the sign bit and 3 unused bits of the top two bytes in an
/// [`f64`]. The sign bit may be either true or false, but the 3 bit value will never be `0`, so as
/// to prevent possible errors where an all-zero stored value becomes identical to the standard
/// `NaN` used by floats.
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct RawTag(TagVal);

impl RawTag {
    /// Create a new tag from a sign bit and a trailing tag value. If the provided value is greater
    /// than 7, it will be masked by `0b111` to convert it into the range `1..8`.
    #[inline]
    #[must_use]
    pub fn new(neg: bool, val: NonZeroU8) -> RawTag {
        // SAFETY: Value truncated into range 0-7
        unsafe { Self::new_unchecked(neg, val.get() & 0x07) }
    }

    /// Create a new tag from a sign bit and a trailing tag value. If the provided value is outside
    /// the range `1..8`, `None` will be returned.
    #[inline]
    #[must_use]
    pub fn new_checked(neg: bool, val: u8) -> Option<RawTag> {
        Some(RawTag(match (neg, val) {
            (false, 1) => TagVal::_P1,
            (false, 2) => TagVal::_P2,
            (false, 3) => TagVal::_P3,
            (false, 4) => TagVal::_P4,
            (false, 5) => TagVal::_P5,
            (false, 6) => TagVal::_P6,
            (false, 7) => TagVal::_P7,

            (true, 1) => TagVal::_N1,
            (true, 2) => TagVal::_N2,
            (true, 3) => TagVal::_N3,
            (true, 4) => TagVal::_N4,
            (true, 5) => TagVal::_N5,
            (true, 6) => TagVal::_N6,
            (true, 7) => TagVal::_N7,

            _ => return None,
        }))
    }

    /// Create a new tag from a sign bit and a trailing tag value, performing no validation.
    ///
    /// # Safety
    ///
    /// `val` must be in the range `1..8`
    #[inline]
    #[must_use]
    pub unsafe fn new_unchecked(neg: bool, val: u8) -> RawTag {
        RawTag(match (neg, val) {
            (false, 1) => TagVal::_P1,
            (false, 2) => TagVal::_P2,
            (false, 3) => TagVal::_P3,
            (false, 4) => TagVal::_P4,
            (false, 5) => TagVal::_P5,
            (false, 6) => TagVal::_P6,
            (false, 7) => TagVal::_P7,

            (true, 1) => TagVal::_N1,
            (true, 2) => TagVal::_N2,
            (true, 3) => TagVal::_N3,
            (true, 4) => TagVal::_N4,
            (true, 5) => TagVal::_N5,
            (true, 6) => TagVal::_N6,
            (true, 7) => TagVal::_N7,

            // SAFETY: Caller contract requires val is in range 1..8, so this is never hit
            _ => unsafe { core::hint::unreachable_unchecked() },
        })
    }

    /// Return sign bit of this tag
    #[inline]
    #[must_use]
    pub fn is_neg(self) -> bool {
        matches!(self.0, |TagVal::_N1| TagVal::_N2
            | TagVal::_N3
            | TagVal::_N4
            | TagVal::_N5
            | TagVal::_N6
            | TagVal::_N7)
    }

    /// Return the trailing value of this tag. The value is guaranteed to be in the range `1..8`.
    #[inline]
    #[must_use]
    pub fn val(self) -> NonZeroU8 {
        match self.0 {
            TagVal::_P1 | TagVal::_N1 => NonZeroU8::MIN,
            TagVal::_P2 | TagVal::_N2 => NonZeroU8::MIN.saturating_add(1),
            TagVal::_P3 | TagVal::_N3 => NonZeroU8::MIN.saturating_add(2),
            TagVal::_P4 | TagVal::_N4 => NonZeroU8::MIN.saturating_add(3),
            TagVal::_P5 | TagVal::_N5 => NonZeroU8::MIN.saturating_add(4),
            TagVal::_P6 | TagVal::_N6 => NonZeroU8::MIN.saturating_add(5),
            TagVal::_P7 | TagVal::_N7 => NonZeroU8::MIN.saturating_add(6),
        }
    }

    /// Return the combination sign bit and trailing value of this tag. The value is guaranteed to
    /// be in the range `1..8`.
    #[inline]
    #[must_use]
    pub fn neg_val(self) -> (bool, u8) {
        match self.0 {
            TagVal::_P1 => (false, 1),
            TagVal::_P2 => (false, 2),
            TagVal::_P3 => (false, 3),
            TagVal::_P4 => (false, 4),
            TagVal::_P5 => (false, 5),
            TagVal::_P6 => (false, 6),
            TagVal::_P7 => (false, 7),
            TagVal::_N1 => (true, 1),
            TagVal::_N2 => (true, 2),
            TagVal::_N3 => (true, 3),
            TagVal::_N4 => (true, 4),
            TagVal::_N5 => (true, 5),
            TagVal::_N6 => (true, 6),
            TagVal::_N7 => (true, 7),
        }
    }
}

impl fmt::Debug for RawTag {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RawTag")
            .field("neg", &self.is_neg())
            .field("val", &self.val())
            .finish()
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(transparent)]
struct Header(u16);

impl Header {
    #[inline]
    fn new(tag: RawTag) -> Header {
        let (neg, val) = tag.neg_val();
        Header(0x7FF8 | (u16::from(neg) << 15) | u16::from(val))
    }

    #[inline]
    fn tag(self) -> RawTag {
        // SAFETY: tag is guaranteed in range 1-7, we already truncated it and 0 never happens
        unsafe { RawTag::new_unchecked(self.get_sign(), self.get_tag()) }
    }

    #[inline]
    fn get_sign(self) -> bool {
        self.0 & 0x8000 != 0
    }

    #[inline]
    fn get_tag(self) -> u8 {
        (self.0 & 0x0007) as u8
    }

    #[inline]
    fn into_raw(self) -> u16 {
        self.0
    }
}

/// A non-float value stored in a NaN Box. This encompasses both the 'tag' stored in the top two
/// bytes and the trailing 6-byte stored value.
#[derive(Clone, Debug, PartialEq)]
#[repr(C, align(8))]
pub struct Value {
    #[cfg(target_endian = "big")]
    header: Header,
    data: [u8; 6],
    #[cfg(target_endian = "little")]
    header: Header,
}

impl Value {
    /// Create a new `Value` with the specified tag and contained data.
    #[inline]
    pub fn new(tag: RawTag, data: [u8; 6]) -> Value {
        Value {
            header: Header::new(tag),
            data,
        }
    }

    /// Create a new `Value` with the specified tag and all-zero contained data.
    #[inline]
    pub fn empty(tag: RawTag) -> Value {
        Value::new(tag, [0; 6])
    }

    /// Create a new `Value` with the specified tag, and containing the provided value.
    pub fn store<T: RawStore>(tag: RawTag, val: T) -> Value {
        let mut v = Value::new(tag, [0; 6]);
        T::to_val(val, &mut v);
        v
    }

    /// Load the specified type out of this `Value`. This performs no checking of the tag.
    pub fn load<T: RawStore>(self) -> T {
        T::from_val(&self)
    }

    /// Retrieve the [`RawTag`] of this `Value`. Downstream users should generally check this before
    /// calling [`load`](Self::load), as this type doesn't actually handle ensuring stored and
    /// loaded tag values match.
    #[inline]
    #[must_use]
    pub fn tag(&self) -> RawTag {
        self.header.tag()
    }

    #[inline]
    fn header(&self) -> &Header {
        &self.header
    }

    /// Set the contained data to the provided byte array
    #[inline]
    pub fn set_data(&mut self, val: [u8; 6]) {
        self.data = val;
    }

    /// Retrieve the data stored in this value as a byte array.
    ///
    /// # Alignment
    ///
    /// The byte array is guaranteed 2-byte aligned on all systems, and 4 byte aligned at either the
    /// start or at 2-byte offset depending on whether the system is little or big endian.
    #[inline]
    #[must_use]
    pub fn data(&self) -> &[u8; 6] {
        &self.data
    }

    /// Retrieve the data stored in this value as a mutable byte array.
    ///
    /// # Alignment
    /// The byte array is guaranteed 2-byte aligned on all systems, and 4 byte aligned at either the
    /// start or at 2-byte offset depending on whether the system is little or big endian.
    #[inline]
    #[must_use]
    pub fn data_mut(&mut self) -> &mut [u8; 6] {
        &mut self.data
    }

    /// This provides access to the whole `Value` as a byte array, which will be 8-byte aligned and
    /// filled with a valid `Value` (See the documentation on [`RawBox`] for more details on the
    /// exact layout)
    ///
    /// # Safety
    ///
    /// Strictly, this function is safe, as it doesn't allow mutation, but it is marked unsafe in
    /// combination with [`whole_mut`](Self::whole_mut) - the return of this value is guaranteed a
    /// valid value to write into the pointer returned by [`whole_mut`](Self::whole_mut). If you
    /// only need access to the 6 data bytes, prefer [`data`](Self::data)
    #[inline]
    #[must_use]
    pub unsafe fn whole(&self) -> &[u8; 8] {
        let ptr = (self as *const Value).cast::<[u8; 8]>();
        // SAFETY: `Value` contains no padding bytes, and is exactly 8 bytes long
        unsafe { &*ptr }
    }

    /// This provides access to the whole `Value` as a mutable byte array, which will be 8-byte
    /// aligned and filled with a valid `Value` (See the documentation on [`RawBox`] for more
    /// details on the exact layout)
    ///
    /// # Safety
    ///
    /// Any value written to this byte array must be a valid `Value`. This means the whole array,
    /// interpreted as a native-endian float, must be a `NaN`, and the 'tag' bits must be non-zero.
    /// There are no requirements on the value of the other 6 bytes. If you only need access to
    /// those bytes, prefer [`data_mut`](Self::data_mut).
    #[inline]
    #[must_use]
    pub unsafe fn whole_mut(&mut self) -> &mut [u8; 8] {
        let ptr = (self as *mut Value).cast::<[u8; 8]>();
        // SAFETY: `Value` contains no padding bytes, and is exactly 8 bytes long
        unsafe { &mut *ptr }
    }
}

/// A simple 'raw' NaN-boxed type, which provides no type checking of its own, but acts as a
/// primitive for easily implementing checked NaN Boxes on top of it.
///
/// # Layout
///
/// The contained value is laid out the same as an `f64` at minimum - 8 byte alignment, and reading
/// it as a float will always return either a correctly stored float value or NaN.
///
/// When storing a `NaN` float, it will be normalized into either `0x7FF8_0000_0000_0000` or
/// `0xFFF8_0000_0000_0000`, preserving only the sign bit of the provided value. All other float
/// values will be stored 'as-is'.
///
/// Non-float values will be any quiet `NaN` float value that has non-zero bits in the trailing 51
/// bit significand. This specific implementation requires that at least one of the first three bits,
/// which act as a 'tag' for determining the type of the remaining data normally, be set.
///
/// # Limitations
///
/// This type attempts to impose a minimal set of limitations to allow downstream users as much
/// freedom as possible, however some trade-off decisions must be made.
///
/// - All NaN float values are normalized to either `0x7FF8_0000_0000_0000` or `0xFFF8_0000_0000_0000`
///   - This preserves the signedness of a `NaN`, but otherwise discards any information. This should
///     be a fairly obvious necessity for function
/// - The stored 'tag' in the first two bytes of the `NaN` must have a non-zero trailing value
///   - This allows returning mutable references to contained values in many situations - without
///     this restriction, returning a mutable reference to the contained [`Value`] would require
///     a lot more checks, as setting the value to all-zero byte pattern could turn it into a valid
///     float NaN
///
#[repr(C)]
pub union RawBox {
    float: f64,
    value: ManuallyDrop<Value>,

    // Used for comparisons
    bits: u64,
    // Used when cloning, to preserve provenance
    #[cfg(target_pointer_width = "64")]
    ptr: *const (),
    #[cfg(target_pointer_width = "32")]
    ptr: (u32, *const ()),
}

impl RawBox {
    /// Create a new [`RawBox`] from a float value. All `NaN` float values will be normalized as
    /// specified in the layout section of the type documentation.
    #[inline]
    #[must_use]
    pub fn from_float(val: f64) -> RawBox {
        match (val.is_nan(), val.is_sign_positive()) {
            (true, true) => RawBox {
                float: f64::from_bits(QUIET_NAN),
            },
            (true, false) => RawBox {
                float: f64::from_bits(NEG_QUIET_NAN),
            },
            (false, _) => RawBox { float: val },
        }
    }

    /// Create a new [`RawBox`] from a non-float value stored in a `NaN` float representation.
    #[inline]
    #[must_use]
    pub fn from_value(value: Value) -> RawBox {
        RawBox {
            value: ManuallyDrop::new(value),
        }
    }

    /// Get the tag of the contained value, if the stored value isn't a float. Helper for
    /// `self.value().map(Value::tag)`.
    #[inline]
    #[must_use]
    pub fn tag(&self) -> Option<RawTag> {
        if self.is_value() {
            // SAFETY: We have ensured we contain a valid value
            Some(unsafe { self.value.tag() })
        } else {
            None
        }
    }

    /// Check whether the contained value is a float
    #[inline]
    #[must_use]
    pub fn is_float(&self) -> bool {
        // SAFETY: It is always sound to read this type as a float, or as raw bits
        (unsafe { !self.float.is_nan() } || unsafe { self.bits & SIGN_MASK == QUIET_NAN })
    }

    /// Check whether the contained value is a non-float value
    #[inline]
    #[must_use]
    pub fn is_value(&self) -> bool {
        // SAFETY: It is always sound to read this type as a float, or as raw bits
        (unsafe { self.float.is_nan() } && unsafe { self.bits & SIGN_MASK != QUIET_NAN })
    }

    /// Get a reference to this type as a float. Returns `Some` if a float is currently stored,
    /// `None` otherwise.
    #[inline]
    #[must_use]
    pub fn float(&self) -> Option<&f64> {
        if self.is_float() {
            // SAFETY: If we pass the check, we contain a float, and since you can't write through
            //         a &f64, can't change this to be NAN and mess it up
            Some(unsafe { &self.float })
        } else {
            None
        }
    }

    /// Get a mutable reference to this type as a float. Returns `Some` if a float is currently
    /// stored, `None` otherwise.
    ///
    /// This doesn't return a raw `f64` because then it would be possible for downstream users to
    /// write a non-normalized `NaN` value into it, breaking the contract of this type. The
    /// [`SingleNaNF64`] type exposes the value mutably while preventing that from happening.
    #[inline]
    #[must_use]
    pub fn float_mut(&mut self) -> Option<&mut SingleNaNF64> {
        if self.is_float() {
            // SAFETY: We have ensured we contain a valid float value
            SingleNaNF64::from_mut(unsafe { &mut self.float })
        } else {
            None
        }
    }

    /// Get a reference to this type as a non-float stored value. Returns `Some` if a non-float
    /// value is currently stored, `None` otherwise.
    #[inline]
    #[must_use]
    pub fn value(&self) -> Option<&Value> {
        if self.is_value() {
            // SAFETY: If we pass the check, we contain NaN-boxed data, and can safely ourselves as
            //         a data value
            Some(unsafe { &self.value })
        } else {
            None
        }
    }

    /// Get a mutable reference to this type as a non-float stored value. Returns `Some` if a
    /// non-float value is currently stored, `None` otherwise.
    #[inline]
    #[must_use]
    pub fn value_mut(&mut self) -> Option<&mut Value> {
        if self.is_value() {
            // SAFETY: If we pass the check, we contain NaN-boxed data, and can safely access
            //         the tail as raw bytes
            //         We ensure tag != 0 on creation to allow this, writing all 0 bytes to data
            //         can never break our invariants.
            Some(unsafe { &mut self.value })
        } else {
            None
        }
    }

    /// Convert this type into the inner float, if possible. Returns `Ok` if the stored type is
    /// currently a float, `Err(self)` otherwise.
    #[inline]
    pub fn into_float(self) -> Result<f64, Self> {
        if self.is_float() {
            // SAFETY: If we pass the check, we contain a float, and can pull it out
            Ok(unsafe { self.float })
        } else {
            Err(self)
        }
    }

    /// Convert this type into the inner value, if possible. Returns `Ok` if the stored type is
    /// currently a non-float value, `Err(self)` otherwise.
    #[inline]
    pub fn into_value(self) -> Result<Value, Self> {
        if self.is_value() {
            // SAFETY: If we pass the check, we contain raw data, and can pull it out
            Ok(ManuallyDrop::into_inner(unsafe { self.value }))
        } else {
            Err(self)
        }
    }

    /// Convert this type into the inner float, performing no checking. This is safe because
    /// non-float values are stored as `NaN` representation floats, meaning they are always valid
    /// to read as a floating-point value and cannot accidentally appear as a 'normal' value,
    /// instead poisoning future operations performed with them.
    #[inline]
    pub fn into_float_unchecked(self) -> f64 {
        // SAFETY: The inner value is *always* a valid float, if stored as a non-float this will
        //         simply return 'some NaN float value'
        unsafe { self.float }
    }
}

impl Clone for RawBox {
    #[inline]
    fn clone(&self) -> Self {
        RawBox {
            // SAFETY: It is always sound to read this type as bits, which
            ptr: unsafe { self.ptr },
        }
    }
}

impl fmt::Debug for RawBox {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.float() {
            Some(val) => f.debug_tuple("RawBox::Float").field(val).finish(),
            None => {
                let val = self.value().unwrap();

                f.debug_struct("RawBox::Data")
                    .field("tag", &val.tag())
                    .field("data", val.data())
                    .finish()
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn size_check() {
        assert_eq!(core::mem::size_of::<RawBox>(), 8);
    }

    #[test]
    fn test_roundtrip_float() {
        let a = RawBox::from_float(1.0);
        assert_eq!(a.into_float().ok(), Some(1.0));

        let b = RawBox::from_float(-1.0);
        assert_eq!(b.into_float().ok(), Some(-1.0));

        let c = RawBox::from_float(f64::NAN);
        assert!(c
            .into_float()
            .is_ok_and(|val| val.is_nan() && val.is_sign_positive()));

        let d = RawBox::from_float(-f64::NAN);
        assert!(d
            .into_float()
            .is_ok_and(|val| val.is_nan() && val.is_sign_negative()));
    }

    #[test]
    fn test_roundtrip_value() {
        let one = NonZeroU8::new(1).unwrap();
        let four = NonZeroU8::new(4).unwrap();
        let seven = NonZeroU8::new(7).unwrap();

        let a = RawBox::from_value(Value::empty(RawTag::new(false, one)));
        assert_eq!(
            a.into_value().ok(),
            Some(Value::empty(RawTag::new(false, one)))
        );

        let b = RawBox::from_value(Value::empty(RawTag::new(true, seven)));
        assert_eq!(
            b.into_value().ok(),
            Some(Value::empty(RawTag::new(true, seven)))
        );

        let c = RawBox::from_value(Value::new(RawTag::new(false, seven), [0, 0, 0, 0, 0, 1]));
        assert_eq!(
            c.into_value().ok(),
            Some(Value::new(RawTag::new(false, seven), [0, 0, 0, 0, 0, 1]))
        );

        let d = RawBox::from_value(Value::new(RawTag::new(false, four), [0x80, 0, 0, 0, 0, 0]));
        assert_eq!(
            d.into_value().ok(),
            Some(Value::new(RawTag::new(false, four), [0x80, 0, 0, 0, 0, 0]))
        );
    }

    #[test]
    fn test_roundtrip_u32() {
        let one = NonZeroU8::MIN;

        let a = RawBox::from_value(Value::store(RawTag::new(false, one), 0u32));
        assert_eq!(a.into_value().unwrap().load::<u32>(), 0u32);

        let b = RawBox::from_value(Value::store(RawTag::new(false, one), 1u32));
        assert_eq!(b.into_value().unwrap().load::<u32>(), 1u32);

        let c = RawBox::from_value(Value::store(RawTag::new(false, one), 0xFFFF_FFFFu32));
        assert_eq!(c.into_value().unwrap().load::<u32>(), 0xFFFF_FFFFu32);
    }

    #[test]
    fn test_roundtrip_i32() {
        let one = NonZeroU8::MIN;

        let a = RawBox::from_value(Value::store(RawTag::new(false, one), 0i32));
        assert_eq!(a.into_value().unwrap().load::<i32>(), 0i32);

        let b = RawBox::from_value(Value::store(RawTag::new(false, one), 1i32));
        assert_eq!(b.into_value().unwrap().load::<i32>(), 1i32);

        let c = RawBox::from_value(Value::store(RawTag::new(false, one), -1i32));
        assert_eq!(c.into_value().unwrap().load::<i32>(), -1i32);
    }

    #[test]
    fn test_roundtrip_ptr() {
        let mut data = Box::new(1);
        let ptr = &mut *data as *mut i32;

        let a = RawBox::from_value(Value::store(RawTag::new(false, NonZeroU8::MIN), ptr));
        let new_ptr = a.into_value().unwrap().load::<*mut i32>();
        assert_eq!(new_ptr, ptr);

        // Check that we can still read/write through the pointer
        assert_eq!(unsafe { *new_ptr }, 1);
        unsafe { *new_ptr = 2 };

        assert_eq!(*data, 2);
    }

    #[test]
    fn test_clone_ptr() {
        // This test is mostly for miri - it ensures we preserve provenance across cloning the underlying box

        let val = 1;

        let a = RawBox::from_value(Value::store(
            RawTag::new(false, NonZeroU8::MIN),
            &val as *const i32,
        ));

        let b = a.clone();

        let ptr = b.into_value().unwrap().load::<*const i32>();
        assert_eq!(unsafe { *ptr }, 1);
    }

    #[test]
    fn test_clone_float() {
        let a = RawBox::from_float(1.0);
        let b = a.clone();
        assert_eq!(b.into_float().ok(), Some(1.0));
    }
}