smallbox 0.8.8

`Small Box` optimization: store small item on stack and fallback to heap for large item.
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
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
use core::any::Any;
use core::cell::UnsafeCell;
use core::cmp::Ordering;
use core::fmt;
use core::future::Future;
use core::hash::Hash;
use core::hash::{self};
use core::hint::unreachable_unchecked;
use core::marker::PhantomData;
#[cfg(feature = "coerce")]
use core::marker::Unsize;
use core::mem::ManuallyDrop;
use core::mem::MaybeUninit;
use core::mem::{self};
use core::ops;
#[cfg(feature = "coerce")]
use core::ops::CoerceUnsized;
use core::pin::Pin;
use core::ptr;
use core::ptr::NonNull;

use ::alloc::alloc;
use ::alloc::alloc::Layout;
use ::alloc::alloc::handle_alloc_error;
use ::alloc::boxed::Box;

use crate::sptr;

/// A sentinel pointer that signals that the value is stored on the stack
///
/// It is never supposed to be dereferenced
const INLINE_SENTINEL: *mut u8 = sptr::without_provenance_mut(0x1);

/// Minimum alignment for allocations
///
/// Forcing a minimum alignment prevents the allocator
/// from returning a pointer with the same address as `INLINE_SENTINEL`
const MIN_ALIGNMENT: usize = 2;

#[cfg(feature = "coerce")]
impl<T: ?Sized + Unsize<U>, U: ?Sized, Space> CoerceUnsized<SmallBox<U, Space>>
    for SmallBox<T, Space>
{
}

/// Box value on stack or on heap depending on its size
///
/// This macro is similar to `SmallBox::new`, which is used to create a new [`SmallBox`] instance,
/// but relaxes the constraint `T: Sized`.
/// In order to do that, this macro will check the coercion rules from type `T` to
/// the target type. This macro will invoke a compile-time error on any invalid type coercion.
///
/// You can think that it has the signature of `smallbox!<U: Sized, T: ?Sized>(val: U) ->
/// SmallBox<T, Space>`
///
/// # Example
///
/// ```
/// #[macro_use]
/// extern crate smallbox;
///
/// # fn main() {
/// use smallbox::SmallBox;
/// use smallbox::space::*;
///
/// let small: SmallBox<[usize], S4> = smallbox!([0usize; 2]);
/// let large: SmallBox<[usize], S4> = smallbox!([1usize; 8]);
///
/// assert_eq!(small.len(), 2);
/// assert_eq!(large[7], 1);
///
/// assert!(large.is_heap() == true);
/// # }
/// ```
#[macro_export]
macro_rules! smallbox {
    ( $e: expr ) => {{
        let val = $e;
        let ptr = ::core::ptr::addr_of!(val);
        #[allow(unsafe_code)]
        unsafe {
            $crate::SmallBox::new_unchecked(val, ptr)
        }
    }};
}

/// An optimized box that store value on stack or on heap depending on its size
pub struct SmallBox<T: ?Sized, Space> {
    space: MaybeUninit<UnsafeCell<Space>>,
    ptr: NonNull<T>,
    _phantom: PhantomData<T>,
}

impl<T: Default, Space> Default for SmallBox<T, Space> {
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T: ?Sized, Space> SmallBox<T, Space> {
    /// Box value on stack or on heap depending on its size.
    ///
    /// # Example
    ///
    /// ```
    /// use smallbox::SmallBox;
    /// use smallbox::space::*;
    ///
    /// let small: SmallBox<_, S4> = SmallBox::new([0usize; 2]);
    /// let large: SmallBox<_, S4> = SmallBox::new([1usize; 8]);
    ///
    /// assert_eq!(small.len(), 2);
    /// assert_eq!(large[7], 1);
    ///
    /// assert!(large.is_heap() == true);
    /// ```
    #[inline(always)]
    pub fn new(val: T) -> SmallBox<T, Space>
    where T: Sized {
        smallbox!(val)
    }

    #[doc(hidden)]
    #[inline]
    pub unsafe fn new_unchecked<U>(val: U, ptr: *const T) -> SmallBox<T, Space>
    where U: Sized {
        let val = ManuallyDrop::new(val);
        Self::new_copy(&val, ptr)
    }

    /// Change the capacity of [`SmallBox`].
    ///
    /// This method may move stack-allocated data from stack to heap
    /// when inline space is not sufficient. Once the data
    /// is moved to heap, it will never be moved back to stack.
    ///
    /// # Example
    ///
    /// ```
    /// use smallbox::SmallBox;
    /// use smallbox::space::S2;
    /// use smallbox::space::S4;
    ///
    /// let s: SmallBox<_, S4> = SmallBox::new([0usize; 4]);
    /// let m: SmallBox<_, S2> = s.resize();
    /// ```
    pub fn resize<ToSpace>(self) -> SmallBox<T, ToSpace> {
        let this = ManuallyDrop::new(self);

        if this.is_heap() {
            // don't change anything if data is already on heap
            let space = MaybeUninit::<UnsafeCell<ToSpace>>::uninit();
            SmallBox {
                space,
                ptr: this.ptr,
                _phantom: PhantomData,
            }
        } else {
            let val: &T = &this;
            unsafe { SmallBox::<T, ToSpace>::new_copy(val, sptr::from_ref(val)) }
        }
    }

    /// Returns true if data is allocated on heap.
    ///
    /// # Example
    ///
    /// ```
    /// use smallbox::SmallBox;
    /// use smallbox::space::S1;
    ///
    /// let stacked: SmallBox<usize, S1> = SmallBox::new(0usize);
    /// assert!(!stacked.is_heap());
    ///
    /// let heaped: SmallBox<(usize, usize), S1> = SmallBox::new((0usize, 1usize));
    /// assert!(heaped.is_heap());
    /// ```
    #[inline]
    pub fn is_heap(&self) -> bool {
        self.ptr.as_ptr().cast::<u8>() != INLINE_SENTINEL
    }

    unsafe fn new_copy<U>(val: &U, metadata_ptr: *const T) -> SmallBox<T, Space>
    where U: ?Sized {
        let layout = Layout::for_value::<U>(val);
        let space_layout = Layout::new::<Space>();

        let mut space = MaybeUninit::<UnsafeCell<Space>>::uninit();

        let (ptr_this, val_dst): (*mut u8, *mut u8) =
            if layout.size() <= space_layout.size() && layout.align() <= space_layout.align() {
                // Stack.
                (INLINE_SENTINEL, space.as_mut_ptr().cast())
            } else if layout.size() == 0 {
                // ZST with alignment greater than Space, which will behave like being stored on
                // heap but will not actually allocate.
                (
                    sptr::without_provenance_mut(layout.align()),
                    sptr::without_provenance_mut(layout.align()),
                )
            } else {
                // Heap.
                let layout = layout
                    // Safety: MIN_ALIGNMENT is 2, which is a valid power-of-two alignment.
                    .align_to(MIN_ALIGNMENT)
                    .unwrap_or_else(|_| unreachable_unchecked());
                let heap_ptr = alloc::alloc(layout);

                if heap_ptr.is_null() {
                    handle_alloc_error(layout)
                }

                (heap_ptr, heap_ptr)
            };

        // `self.ptr` always holds the metadata, even if stack allocated.
        let ptr = sptr::with_metadata_of_mut(ptr_this, metadata_ptr);
        // Safety: ptr is either INLINE_SENTINEL or returned from the allocator and checked for
        // null.
        let ptr = NonNull::new_unchecked(ptr);

        ptr::copy_nonoverlapping(sptr::from_ref(val).cast(), val_dst, layout.size());

        SmallBox {
            space,
            ptr,
            _phantom: PhantomData,
        }
    }

    unsafe fn downcast_unchecked<U: Any>(self) -> SmallBox<U, Space> {
        let this = ManuallyDrop::new(self);

        let size = mem::size_of::<U>();
        let mut space = MaybeUninit::<UnsafeCell<Space>>::uninit();

        if !this.is_heap() {
            ptr::copy_nonoverlapping::<u8>(
                this.space.as_ptr().cast(),
                space.as_mut_ptr().cast(),
                size,
            );
        };

        let ptr = this.ptr.cast();

        SmallBox {
            space,
            ptr,
            _phantom: PhantomData,
        }
    }

    #[inline]
    unsafe fn as_ptr(&self) -> *const T {
        if self.is_heap() {
            self.ptr.as_ptr()
        } else {
            sptr::with_metadata_of(self.space.as_ptr(), self.ptr.as_ptr())
        }
    }

    #[inline]
    unsafe fn as_mut_ptr(&mut self) -> *mut T {
        if self.is_heap() {
            self.ptr.as_ptr()
        } else {
            sptr::with_metadata_of_mut(self.space.as_mut_ptr(), self.ptr.as_ptr())
        }
    }

    /// Consumes the SmallBox and returns ownership of the boxed value
    ///
    /// # Examples
    /// ```
    /// use smallbox::SmallBox;
    /// use smallbox::space::S1;
    ///
    /// let stacked: SmallBox<_, S1> = SmallBox::new([21usize]);
    /// let val = stacked.into_inner();
    /// assert_eq!(val[0], 21);
    ///
    /// let boxed: SmallBox<_, S1> = SmallBox::new(vec![21, 56, 420]);
    /// let val = boxed.into_inner();
    /// assert_eq!(val[1], 56);
    /// ```
    #[inline]
    pub fn into_inner(self) -> T
    where T: Sized {
        let this = ManuallyDrop::new(self);
        let ret_val: T = unsafe { this.as_ptr().read() };

        // Just deallocates the heap memory without dropping the boxed value
        if this.is_heap() && mem::size_of::<T>() != 0 {
            // Safety: MIN_ALIGNMENT is 2, aligning to 2 should not create an invalid layout
            let layout = unsafe {
                Layout::new::<T>()
                    .align_to(MIN_ALIGNMENT)
                    .unwrap_or_else(|_| unreachable_unchecked())
            };
            unsafe {
                alloc::dealloc(this.ptr.as_ptr().cast::<u8>(), layout);
            }
        }

        ret_val
    }

    /// Creates a [`SmallBox`] from a standard [`Box`].
    ///
    /// The data will always be stored on the heap since it's already allocated there.
    /// This method transfers ownership from the [`Box`] to the [`SmallBox`] without copying
    /// or moving the data.
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate alloc;
    /// # use alloc::boxed::Box;
    ///
    /// use smallbox::SmallBox;
    /// use smallbox::space::S4;
    ///
    /// let boxed = Box::new([1, 2, 3, 4]);
    /// let small_box: SmallBox<_, S4> = SmallBox::from_box(boxed);
    ///
    /// assert!(small_box.is_heap());
    /// assert_eq!(*small_box, [1, 2, 3, 4]);
    /// ```
    pub fn from_box(boxed: ::alloc::boxed::Box<T>) -> Self {
        unsafe {
            let ptr = NonNull::new_unchecked(Box::into_raw(boxed));
            let space = MaybeUninit::<UnsafeCell<Space>>::uninit();
            SmallBox {
                space,
                ptr,
                _phantom: PhantomData,
            }
        }
    }

    /// Converts a [`SmallBox`] into a standard [`Box`].
    ///
    /// If the data is stored on the stack, it will be moved to the heap.
    /// If the data is already on the heap, ownership is transferred without
    /// copying or moving the data.
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate alloc;
    /// # use alloc::boxed::Box;
    ///
    /// use smallbox::SmallBox;
    /// use smallbox::space::S4;
    ///
    /// let small_box: SmallBox<_, S4> = SmallBox::new([1, 2, 3, 4]);
    /// let boxed: Box<[i32; 4]> = SmallBox::into_box(small_box);
    ///
    /// assert_eq!(*boxed, [1, 2, 3, 4]);
    /// ```
    pub fn into_box(boxed: SmallBox<T, Space>) -> ::alloc::boxed::Box<T> {
        unsafe {
            let mut enforce_heap = ManuallyDrop::new(boxed.resize::<()>());
            debug_assert!(enforce_heap.is_heap());
            Box::from_raw(enforce_heap.as_mut_ptr())
        }
    }
}

impl<Space> SmallBox<dyn Any, Space> {
    /// Attempt to downcast the box to a concrete type.
    ///
    /// # Examples
    ///
    /// ```
    /// #[macro_use]
    /// extern crate smallbox;
    ///
    /// # fn main() {
    /// use core::any::Any;
    ///
    /// use smallbox::SmallBox;
    /// use smallbox::space::*;
    ///
    /// fn print_if_string(value: SmallBox<dyn Any, S1>) {
    ///     if let Ok(string) = value.downcast::<String>() {
    ///         println!("String ({}): {}", string.len(), string);
    ///     }
    /// }
    ///
    /// fn main() {
    ///     let my_string = "Hello World".to_string();
    ///     print_if_string(smallbox!(my_string));
    ///     print_if_string(smallbox!(0i8));
    /// }
    /// # }
    /// ```
    #[inline]
    pub fn downcast<T: Any>(self) -> Result<SmallBox<T, Space>, Self> {
        if self.is::<T>() {
            unsafe { Ok(self.downcast_unchecked()) }
        } else {
            Err(self)
        }
    }
}

impl<Space> SmallBox<dyn Any + Send, Space> {
    /// Attempt to downcast the box to a concrete type.
    ///
    /// # Examples
    ///
    /// ```
    /// #[macro_use]
    /// extern crate smallbox;
    ///
    /// # fn main() {
    /// use core::any::Any;
    ///
    /// use smallbox::SmallBox;
    /// use smallbox::space::*;
    ///
    /// fn print_if_string(value: SmallBox<dyn Any, S1>) {
    ///     if let Ok(string) = value.downcast::<String>() {
    ///         println!("String ({}): {}", string.len(), string);
    ///     }
    /// }
    ///
    /// fn main() {
    ///     let my_string = "Hello World".to_string();
    ///     print_if_string(smallbox!(my_string));
    ///     print_if_string(smallbox!(0i8));
    /// }
    /// # }
    /// ```
    #[inline]
    pub fn downcast<T: Any>(self) -> Result<SmallBox<T, Space>, Self> {
        if self.is::<T>() {
            unsafe { Ok(self.downcast_unchecked()) }
        } else {
            Err(self)
        }
    }
}

impl<T: ?Sized, Space> ops::Deref for SmallBox<T, Space> {
    type Target = T;

    fn deref(&self) -> &T {
        unsafe { &*self.as_ptr() }
    }
}

impl<T: ?Sized, Space> ops::DerefMut for SmallBox<T, Space> {
    fn deref_mut(&mut self) -> &mut T {
        unsafe { &mut *self.as_mut_ptr() }
    }
}

impl<T: ?Sized, Space> ops::Drop for SmallBox<T, Space> {
    fn drop(&mut self) {
        unsafe {
            let layout = Layout::for_value::<T>(&*self)
                .align_to(MIN_ALIGNMENT)
                .unwrap_or_else(|_| unreachable_unchecked());

            ptr::drop_in_place::<T>(&mut **self);
            if self.is_heap() && layout.size() != 0 {
                alloc::dealloc(self.ptr.as_ptr().cast::<u8>(), layout);
            }
        }
    }
}

impl<T: Clone, Space> Clone for SmallBox<T, Space>
where T: Sized
{
    fn clone(&self) -> Self {
        let val: &T = self;
        SmallBox::new(val.clone())
    }
}

impl<T: ?Sized + fmt::Display, Space> fmt::Display for SmallBox<T, Space> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&**self, f)
    }
}

impl<T: ?Sized + fmt::Debug, Space> fmt::Debug for SmallBox<T, Space> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(&**self, f)
    }
}

impl<T: ?Sized, Space> fmt::Pointer for SmallBox<T, Space> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // It's not possible to extract the inner Unique directly from the Box,
        // instead we cast it to a *const which aliases the Unique
        let ptr: *const T = &**self;
        fmt::Pointer::fmt(&ptr, f)
    }
}

impl<T: ?Sized + PartialEq, Space> PartialEq for SmallBox<T, Space> {
    fn eq(&self, other: &SmallBox<T, Space>) -> bool {
        PartialEq::eq(&**self, &**other)
    }
}

impl<T: ?Sized + PartialOrd, Space> PartialOrd for SmallBox<T, Space> {
    fn partial_cmp(&self, other: &SmallBox<T, Space>) -> Option<Ordering> {
        PartialOrd::partial_cmp(&**self, &**other)
    }
    fn lt(&self, other: &SmallBox<T, Space>) -> bool {
        PartialOrd::lt(&**self, &**other)
    }
    fn le(&self, other: &SmallBox<T, Space>) -> bool {
        PartialOrd::le(&**self, &**other)
    }
    fn ge(&self, other: &SmallBox<T, Space>) -> bool {
        PartialOrd::ge(&**self, &**other)
    }
    fn gt(&self, other: &SmallBox<T, Space>) -> bool {
        PartialOrd::gt(&**self, &**other)
    }
}

impl<T: ?Sized + Ord, Space> Ord for SmallBox<T, Space> {
    fn cmp(&self, other: &SmallBox<T, Space>) -> Ordering {
        Ord::cmp(&**self, &**other)
    }
}

impl<T: ?Sized + Eq, Space> Eq for SmallBox<T, Space> {}

impl<T: ?Sized + Hash, Space> Hash for SmallBox<T, Space> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        (**self).hash(state);
    }
}

// We can implement Future for SmallBox soundly, even though it's not implemented for std Box.
// The reason why it's not implemented for std Box is only because Box<T>: Unpin unconditionally,
// even when T: !Unpin, which always allows getting &mut Box<T> from Pin<&mut Box<T>>.
// For SmallBox, this is not the case, because it might carry the data on the stack, so if T:
// !Unpin, then SmallBox<T>: !Unpin also. That means you can't get &mut SmallBox<T> from Pin<&mut
// SmallBox<T>> in safe code, so it's safe to implement Future for SmallBox directly.
impl<F: Future + ?Sized, S> Future for SmallBox<F, S> {
    type Output = F::Output;

    fn poll(
        self: Pin<&mut Self>,
        cx: &mut core::task::Context<'_>,
    ) -> core::task::Poll<Self::Output> {
        // Safety: When the SmallBox is pinned, the data on the stack is pinned.
        // The data on the heap is also pinned naturally, so all Pin guarantees are satisfied.
        unsafe { Pin::new_unchecked(&mut **self.get_unchecked_mut()) }.poll(cx)
    }
}

unsafe impl<T: ?Sized + Send, Space> Send for SmallBox<T, Space> {}
unsafe impl<T: ?Sized + Sync, Space> Sync for SmallBox<T, Space> {}

#[cfg(test)]
mod tests {
    use core::any::Any;
    use core::mem;
    use core::ptr::addr_of;

    use ::alloc::boxed::Box;
    use ::alloc::vec;
    use ::alloc::vec::Vec;

    use super::SmallBox;
    use crate::space::*;

    #[test]
    fn test_basic() {
        let stacked: SmallBox<usize, S1> = SmallBox::new(1234usize);
        assert!(*stacked == 1234);

        let heaped: SmallBox<(usize, usize), S1> = SmallBox::new((0, 1));
        assert!(*heaped == (0, 1));
    }

    #[test]
    fn test_new_unchecked() {
        let val = [0usize, 1];
        let ptr = addr_of!(val);

        unsafe {
            let stacked: SmallBox<[usize], S2> = SmallBox::new_unchecked(val, ptr);
            assert!(*stacked == [0, 1]);
            assert!(!stacked.is_heap());
        }

        let val = [0usize, 1, 2];
        let ptr = addr_of!(val);

        unsafe {
            let heaped: SmallBox<dyn Any, S2> = SmallBox::new_unchecked(val, ptr);
            assert!(heaped.is_heap());

            if let Some(array) = heaped.downcast_ref::<[usize; 3]>() {
                assert_eq!(*array, [0, 1, 2]);
            } else {
                unreachable!();
            }
        }
    }

    #[test]
    #[deny(unsafe_code)]
    fn test_macro() {
        let stacked: SmallBox<dyn Any, S1> = smallbox!(1234usize);
        if let Some(num) = stacked.downcast_ref::<usize>() {
            assert_eq!(*num, 1234);
        } else {
            unreachable!();
        }

        let heaped: SmallBox<dyn Any, S1> = smallbox!([0usize, 1]);
        if let Some(array) = heaped.downcast_ref::<[usize; 2]>() {
            assert_eq!(*array, [0, 1]);
        } else {
            unreachable!();
        }

        let is_even: SmallBox<dyn Fn(u8) -> bool, S1> = smallbox!(|num: u8| num % 2 == 0);
        assert!(!is_even(5));
        assert!(is_even(6));
    }

    #[test]
    #[cfg(feature = "coerce")]
    fn test_coerce() {
        let stacked: SmallBox<dyn Any, S1> = SmallBox::new(1234usize);
        if let Some(num) = stacked.downcast_ref::<usize>() {
            assert_eq!(*num, 1234);
        } else {
            unreachable!();
        }

        let heaped: SmallBox<dyn Any, S1> = SmallBox::new([0usize, 1]);
        if let Some(array) = heaped.downcast_ref::<[usize; 2]>() {
            assert_eq!(*array, [0, 1]);
        } else {
            unreachable!();
        }
    }

    #[test]
    fn test_drop() {
        use core::cell::Cell;

        #[allow(dead_code)]
        struct Struct<'a>(&'a Cell<bool>, u8);
        impl<'a> Drop for Struct<'a> {
            fn drop(&mut self) {
                self.0.set(true);
            }
        }

        let flag = Cell::new(false);
        let stacked: SmallBox<_, S2> = SmallBox::new(Struct(&flag, 0));
        assert!(!stacked.is_heap());
        assert!(!flag.get());
        drop(stacked);
        assert!(flag.get());

        let flag = Cell::new(false);
        let heaped: SmallBox<_, S1> = SmallBox::new(Struct(&flag, 0));
        assert!(heaped.is_heap());
        assert!(!flag.get());
        drop(heaped);
        assert!(flag.get());
    }

    #[test]
    fn test_dont_drop_space() {
        #[allow(dead_code)]
        struct NoDrop(S1);
        impl Drop for NoDrop {
            fn drop(&mut self) {
                unreachable!();
            }
        }

        drop(SmallBox::<_, NoDrop>::new([true]));
    }

    #[test]
    fn test_oversize() {
        let fit = SmallBox::<_, S1>::new([1usize]);
        let oversize = SmallBox::<_, S1>::new([1usize, 2]);
        assert!(!fit.is_heap());
        assert!(oversize.is_heap());
    }

    #[test]
    fn test_resize() {
        let m = SmallBox::<_, S4>::new([1usize, 2]);
        let l = m.resize::<S8>();
        assert!(!l.is_heap());
        let m = l.resize::<S4>();
        assert!(!m.is_heap());
        let s = m.resize::<S2>();
        assert!(!s.is_heap());
        let xs = s.resize::<S1>();
        assert!(xs.is_heap());
        let m = xs.resize::<S4>();
        assert!(m.is_heap());
        assert_eq!(*m, [1usize, 2]);
    }

    #[test]
    fn test_clone() {
        let stacked: SmallBox<[usize; 2], S2> = smallbox!([1usize, 2]);
        assert_eq!(stacked, stacked.clone())
    }

    #[test]
    fn test_zst() {
        struct ZSpace;

        let zst: SmallBox<[usize], S1> = smallbox!([1usize; 0]);
        assert_eq!(*zst, [1usize; 0]);

        let zst: SmallBox<[usize], ZSpace> = smallbox!([1usize; 0]);
        assert_eq!(*zst, [1usize; 0]);
        let zst: SmallBox<[usize], ZSpace> = smallbox!([1usize; 2]);
        assert_eq!(*zst, [1usize; 2]);
    }

    #[test]
    fn test_downcast() {
        let stacked: SmallBox<dyn Any, S1> = smallbox!(0x01u32);
        assert!(!stacked.is_heap());
        assert_eq!(SmallBox::new(0x01), stacked.downcast::<u32>().unwrap());

        let heaped: SmallBox<dyn Any, S1> = smallbox!([1usize, 2]);
        assert!(heaped.is_heap());
        assert_eq!(
            smallbox!([1usize, 2]),
            heaped.downcast::<[usize; 2]>().unwrap()
        );

        let stacked_send: SmallBox<dyn Any + Send, S1> = smallbox!(0x01u32);
        assert!(!stacked_send.is_heap());
        assert_eq!(SmallBox::new(0x01), stacked_send.downcast::<u32>().unwrap());

        let heaped_send: SmallBox<dyn Any + Send, S1> = smallbox!([1usize, 2]);
        assert!(heaped_send.is_heap());
        assert_eq!(
            SmallBox::new([1usize, 2]),
            heaped_send.downcast::<[usize; 2]>().unwrap()
        );

        let mismatched: SmallBox<dyn Any, S1> = smallbox!(0x01u32);
        assert!(mismatched.downcast::<u8>().is_err());
        let mismatched: SmallBox<dyn Any, S1> = smallbox!(0x01u32);
        assert!(mismatched.downcast::<u64>().is_err());
    }

    #[test]
    fn test_option_encoding() {
        let tester: SmallBox<Box<()>, S2> = SmallBox::new(Box::new(()));
        assert!(Some(tester).is_some());
    }

    #[test]
    fn test_into_inner() {
        let tester: SmallBox<_, S1> = SmallBox::new([21usize]);
        let val = tester.into_inner();
        assert_eq!(val[0], 21);

        let tester: SmallBox<_, S1> = SmallBox::new(vec![21, 56, 420]);
        let val = tester.into_inner();
        assert_eq!(val[1], 56);
    }

    #[test]
    fn test_interior_mutability() {
        use core::cell::Cell;
        let cellbox = SmallBox::<Cell<u32>, S1>::new(Cell::new(0));
        assert!(!cellbox.is_heap());
        cellbox.set(1);
        assert_eq!(cellbox.get(), 1);
    }

    #[test]
    fn test_future() {
        let boxed_fut: SmallBox<_, S1> = SmallBox::new(async { 123 });

        assert_eq!(futures::executor::block_on(boxed_fut), 123);
    }

    #[test]
    fn test_variance() {
        #[allow(dead_code)]
        fn test<'short, 'long: 'short>(val: SmallBox<&'long str, S1>) -> SmallBox<&'short str, S1> {
            val
        }
    }

    #[test]
    fn test_overaligned_zst() {
        #[repr(align(512))]
        #[derive(Debug, PartialEq, Eq)]
        struct OveralignedZst;

        let zst: SmallBox<OveralignedZst, S1> = smallbox!(OveralignedZst);
        #[allow(clippy::as_conversions)]
        let zst_addr = addr_of!(*zst) as usize;
        assert_eq!(*zst, OveralignedZst);
        assert_eq!(zst_addr % 512, 0);
    }

    #[test]
    fn test_overaligned_zst_dyn() {
        #[repr(align(512))]
        #[derive(Debug, PartialEq, Eq)]
        struct OveralignedZst;

        trait Foo {}

        impl Foo for OveralignedZst {}

        let zst: SmallBox<dyn Foo, S1> = smallbox!(OveralignedZst);
        #[allow(clippy::as_conversions)]
        let zst_addr = addr_of!(*zst) as *const () as usize;
        assert_eq!(zst_addr % 512, 0);
    }

    #[test]
    fn test_null_ptr_optimization() {
        assert_eq!(
            mem::size_of::<SmallBox<i32, S1>>(),
            mem::size_of::<Option<SmallBox<i32, S1>>>()
        );
    }

    #[test]
    fn test_box_roundtrip() {
        // Box -> SmallBox -> Box
        let original_data = vec![1, 2, 3, 4, 5];
        let original_box: Box<dyn Any> = Box::new(original_data.clone());

        let intermediate_small_box: SmallBox<dyn Any, S4> = SmallBox::from_box(original_box);
        assert!(intermediate_small_box.is_heap());

        let final_box: Box<dyn Any> = SmallBox::into_box(intermediate_small_box);
        let final_data: &Vec<i32> = final_box.downcast_ref().unwrap();
        assert_eq!(original_data, *final_data);

        // SmallBox -> Box -> SmallBox
        let original_small_box: SmallBox<dyn Any, S4> = smallbox!(original_data.clone());
        assert!(!original_small_box.is_heap());

        let intermediate_box: Box<dyn Any> = SmallBox::into_box(original_small_box);

        let final_small_box: SmallBox<dyn Any, S4> = SmallBox::from_box(intermediate_box);
        assert!(final_small_box.is_heap());
        let final_data: &Vec<i32> = final_small_box.downcast_ref().unwrap();
        assert_eq!(original_data, *final_data);
    }
}