tarc 0.1.6

Transposable, type-erasable, and FFI-safe Arc
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
//! # tarc
//!
//! ## Transposable, type-erasable, and FFI-compatible Arc.
//!
//! `tarc` is a fork of `std::sync::Arc` with several additions easing its use in type-erased
//! scenarios.

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::alloc::{alloc, dealloc};
use core::alloc::Layout;
use core::marker::Unpin;
use core::pin::Pin;
#[cfg(not(feature = "std"))]
use core::prelude::rust_2018::*;
use core::ptr::NonNull;
use core::sync::atomic::{fence, AtomicUsize, Ordering};
#[cfg(feature = "std")]
use std::alloc::{alloc, dealloc};

#[cfg(feature = "unwind")]
mod unwind;
#[cfg(feature = "unwind")]
use unwind::*;

#[cfg(feature = "unwind")]
type DropFnDefault = DropFnUnwind;
#[cfg(not(feature = "unwind"))]
type DropFnDefault = DropFn;

/// An opaque handle.
#[repr(transparent)]
pub struct Handle(BaseArc<()>);

impl<T: ?Sized> From<Arc<T>> for Handle {
    fn from(arc: Arc<T>) -> Self {
        Self(unsafe { arc.untranspose::<()>() })
    }
}

impl<T: ?Sized> From<BaseArc<T>> for Handle {
    fn from(arc: BaseArc<T>) -> Self {
        let data = arc.into_raw();
        Self(unsafe { BaseArc::from_raw(data as *const ()) })
    }
}

#[repr(C)]
struct ArcHeader {
    count: AtomicUsize,
    drop: DropFnDefault,
}

pub type DropFn = unsafe extern "C" fn(*mut ());
pub type DropInPlace = Option<DropFn>;

#[cfg(not(feature = "unwind"))]
pub type DropInPlaceDefault = DropInPlace;
#[cfg(feature = "unwind")]
pub type DropInPlaceDefault = DropInPlaceUnwind;

mod sealed {
    use super::*;

    pub trait DropFunc: Copy {
        unsafe fn invoke(self, data: *mut ());
    }

    impl DropFunc for DropInPlace {
        unsafe fn invoke(self, data: *mut ()) {
            if let Some(drop_fn) = self {
                drop_fn(data);
            }
        }
    }

    impl DropFunc for DropFn {
        unsafe fn invoke(self, data: *mut ()) {
            self(data)
        }
    }

    impl DropFunc for () {
        unsafe fn invoke(self, _: *mut ()) {}
    }

    impl<T: Sized> DropFunc for unsafe fn(*mut T) {
        unsafe fn invoke(self, data: *mut ()) {
            self(data as *mut T)
        }
    }
}

use sealed::DropFunc;

#[repr(C)]
struct DynArcHeader<T> {
    size: usize,
    alignment: usize,
    drop_in_place: T,
    hdr: ArcHeader,
}

unsafe fn do_drop_impl<T>(data: *mut ()) {
    core::ptr::drop_in_place(data as *mut T);
    let (header, layout) = layout::<T>();
    dealloc((data as *mut u8).sub(header), layout);
}

unsafe fn do_dyn_drop_impl<T: DropFunc>(data: *mut ()) {
    let header = (data as *mut DynArcHeader<T>).sub(1);
    let layout = Layout::from_size_align_unchecked((*header).size, (*header).alignment);

    (*header).drop_in_place.invoke(data);

    dealloc(header as *mut u8, layout);
}

#[cfg(not(feature = "unwind"))]
mod drops {
    use super::*;
    pub unsafe extern "C" fn do_drop<T>(data: *mut ()) {
        super::do_drop_impl::<T>(data)
    }
    pub unsafe extern "C" fn do_dyn_drop<T: DropFunc>(data: *mut ()) {
        super::do_dyn_drop_impl::<T>(data)
    }
}

use drops::*;

fn layout<T>() -> (usize, Layout) {
    header_layout::<DynArcHeader<DropInPlaceDefault>>(unsafe {
        Layout::from_size_align_unchecked(core::mem::size_of::<T>(), core::mem::align_of::<T>())
    })
}

fn header_layout<H>(inp: Layout) -> (usize, Layout) {
    let alignment = core::cmp::max(inp.align(), core::mem::align_of::<H>());
    let header_size = core::mem::size_of::<H>();

    // Alignment is always a power of two, thus bitwise ops are valid
    let align_up = |val| (val + (alignment - 1)) & !(alignment - 1);

    let header_aligned = align_up(header_size);
    let size = header_aligned + align_up(inp.size());

    // SAFETY: left half and right half of the size calculation are multiple of alignment
    let layout = unsafe { Layout::from_size_align_unchecked(size, alignment) };

    (header_aligned, layout)
}

unsafe fn allocate<T>() -> NonNull<T> {
    let (header, layout) = layout::<T>();
    let data = alloc(layout).add(header);

    assert!(!data.is_null());

    NonNull::new_unchecked(data as *mut T)
}

unsafe fn initialize<T>(data: NonNull<T>, val: T) {
    let header = (data.as_ptr() as *mut ArcHeader).sub(1);
    header.write(ArcHeader {
        count: AtomicUsize::new(1),
        drop: do_drop::<T>,
    });
    data.as_ptr().write(val);
}

/// Arc that is safe to transpose and type-erase.
///
/// Standard `BaseArc<T>` allocates space for a header and places the header contents at the start
/// of that space, which means the offset between data and header depends on the type alignment.
/// This Arc puts the header right next to the data, making header accesses not rely on the
/// underlying type. This allows to safely transmute `Arc<T>` into `Arc<()>`, or any type that `T`
/// is safe to transmute into.
///
/// In addition, you may convert `BaseArc<T>` to `Arc<O>` if `O` is stored within `T` and there is
/// a `AsRef<O>` implementation on `T`.
///
/// Semantically, this arc differs from the standard library in its absence of weak references.
/// This is so that the header fits within 2 pointers.
#[repr(transparent)]
pub struct BaseArc<T: ?Sized> {
    data: NonNull<T>,
}

impl BaseArc<()> {
    /// Create a custom Arc.
    ///
    /// This function allows to create a custom dynamically sized arc with custom cleanup routine.
    ///
    /// Note that the contents of the created arc are uninitialized.
    ///
    ///
    ///
    /// # Arguments
    ///
    /// - `layout` - size and alignment of the object being created.
    /// - `drop_in_place` - optional cleanup routine to be invoked on upon releasing the arc.
    ///
    /// # Panics
    ///
    /// This function panics if it fails to allocate enough data for the arc.
    ///
    /// # Safety
    ///
    /// Technically only correct `drop_in_place` implementation needs to be provided. That means,
    /// the caller must ensure that `drop_in_place` releases the contents of the initialized arc
    /// (after it has been returned from this function to the caller), correctly. If `None` is
    /// passed as the cleanup routine, this function should be safe.
    pub unsafe fn new_custom<T: DropFunc>(layout: Layout, drop_in_place: T) -> Self {
        let (header, layout) = header_layout::<DynArcHeader<T>>(layout);
        let data = alloc(layout);

        assert!(!data.is_null());

        let hdr = data as *mut DynArcHeader<T>;

        hdr.write(DynArcHeader {
            size: layout.size(),
            alignment: layout.align(),
            drop_in_place,
            hdr: ArcHeader {
                count: AtomicUsize::new(1),
                drop: do_dyn_drop::<T>,
            },
        });

        let data = NonNull::new_unchecked(data.add(header) as *mut _);

        Self { data }
    }

    #[deprecated = "Use new_custom"]
    pub unsafe fn custom(layout: Layout, drop_in_place: DropInPlace) -> Self {
        Self::new_custom(layout, drop_in_place)
    }
}

impl<T> BaseArc<T> {
    pub fn new(val: T) -> Self {
        let data = unsafe { allocate::<T>() };
        unsafe {
            initialize::<T>(data, val);
        }

        Self { data }
    }

    pub fn pin(data: T) -> Pin<Self> {
        unsafe { Pin::new_unchecked(Self::new(data)) }
    }

    pub fn transpose<O>(self) -> Arc<O>
    where
        T: AsRef<O>,
    {
        // Make sure the type can at least be within T
        assert!(core::mem::size_of::<O>() <= core::mem::size_of::<T>());

        let transposed = (*self).as_ref() as *const _ as *const u8;
        let raw = self.into_raw() as *const u8;

        unsafe {
            let off = (transposed as usize).checked_sub(raw as usize).unwrap();

            // Make sure the pointer is within T
            assert!(off <= (core::mem::size_of::<T>() - core::mem::size_of::<O>()));

            Arc::from_raw(raw.add(off) as *const O, off)
        }
    }
}

impl<T: ?Sized> BaseArc<T> {
    fn header(&self) -> &ArcHeader {
        unsafe { &*((self.data.as_ptr() as *mut u8) as *mut ArcHeader).sub(1) }
    }

    /// Convert a pointer to managed `BaseArc`.
    ///
    /// # Safety
    ///
    /// `data` must be originally created through `BaseArc::new`.
    pub unsafe fn from_raw(data: *const T) -> Self {
        Self {
            data: NonNull::new_unchecked(data as *mut _),
        }
    }

    pub fn into_raw(self) -> *const T {
        let ret = self.data.as_ptr() as *const _;
        core::mem::forget(self);
        ret
    }

    /// Increment the strong reference count
    ///
    /// This function takes a pointer to `BaseArc`-allocated data and increments its reference
    /// count.
    ///
    /// # Safety
    ///
    /// `ptr` must be originally created by `BaseArc::new`.
    pub unsafe fn increment_strong_count(ptr: *const T) {
        // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
        let arc = core::mem::ManuallyDrop::new(Self::from_raw(ptr));
        // Now increase refcount, but don't drop new refcount either
        let _arc_clone: core::mem::ManuallyDrop<_> = arc.clone();
    }

    /// Decrement the strong reference count
    ///
    /// This function takes a pointer to `BaseArc`-allocated data and decrements its reference
    /// count. If the count reaches 0, destructor is called.
    ///
    /// # Safety
    ///
    /// `ptr` must be originally created by `BaseArc::new`.
    pub unsafe fn decrement_strong_count(ptr: *const T) {
        core::mem::drop(BaseArc::from_raw(ptr));
    }

    // Needed for macro drop impl
    unsafe fn as_original_ptr<O: Sized>(&self) -> *const O {
        (self.data.as_ptr() as *const u8) as *const O
    }
}

/// Arc that is safe to transpose and type-erase.
///
/// Standard `Arc<T>` allocates space for a header and places the header contents at the start of
/// that space, which means the offset between data and header depends on the type alignment. This
/// Arc puts the header right next to the data, making header accesses not rely on the underlying
/// type. This allows to safely transmute `Arc<T>` into `Arc<()>`, or any type that `T` is safe to
/// transmute into.
///
/// In addition, you may convert `Arc<T>` to `Arc<O>` if `O` is stored within `T` and there is a
/// `AsRef<O>` implementation on `T`.
///
/// Semantically, this arc also differs from the standard library in its absence of weak references.
/// This is so that the header fits within 2 pointers.
#[repr(C)]
pub struct Arc<T: ?Sized> {
    data: NonNull<T>,
    offset: usize,
}

impl<T> Arc<T> {
    pub fn new(val: T) -> Self {
        let data = unsafe { allocate::<T>() };
        unsafe {
            initialize::<T>(data, val);
        }

        Self { data, offset: 0 }
    }

    pub fn pin(data: T) -> Pin<Self> {
        unsafe { Pin::new_unchecked(Self::new(data)) }
    }

    pub fn transpose<O>(self) -> Arc<O>
    where
        T: AsRef<O>,
    {
        // Make sure the type can at least be within T
        assert!(core::mem::size_of::<O>() <= core::mem::size_of::<T>());

        let transposed = (*self).as_ref() as *const _ as *const u8;
        let (raw, offset) = self.into_raw();
        let raw = raw as *const u8;

        unsafe {
            let off = (transposed as usize).checked_sub(raw as usize).unwrap();

            // Make sure the pointer is within T
            assert!(off <= (core::mem::size_of::<T>() - core::mem::size_of::<O>()));

            Arc::from_raw(raw.add(off) as *const O, offset + off)
        }
    }
}

impl<T: ?Sized> Arc<T> {
    fn header(&self) -> &ArcHeader {
        unsafe { &*((self.data.as_ptr() as *mut u8).sub(self.offset) as *mut ArcHeader).sub(1) }
    }

    /// Convert raw pointer to a managed `Arc`.
    ///
    /// # Safety
    ///
    /// `ptr` and `offset` needs to be a pair acquired from `Arc::into_raw` function call. More
    /// specifically, `ptr as *const u8 - offset` must point to start of the data of original
    /// `BaseArc`.
    pub unsafe fn from_raw(data: *const T, offset: usize) -> Self {
        Self {
            data: NonNull::new_unchecked(data as *mut _),
            offset,
        }
    }

    pub fn into_raw(self) -> (*const T, usize) {
        let ret = self.data.as_ptr() as *const _;
        let offset = self.offset;
        core::mem::forget(self);
        (ret, offset)
    }

    /// Increment the strong reference count.
    ///
    /// Takes pointer to Arc's data and byte offset of the data and increments the strong reference
    /// count.
    ///
    /// # Safety
    ///
    /// `ptr` and `offset` needs to be a pair acquired from `Arc::into_raw` function call. More
    /// specifically, `ptr as *const u8 - offset` must point to start of the data of original
    /// `BaseArc`.
    pub unsafe fn increment_strong_count(ptr: *const T, offset: usize) {
        // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
        let arc = core::mem::ManuallyDrop::new(Self::from_raw(ptr, offset));
        // Now increase refcount, but don't drop new refcount either
        let _arc_clone: core::mem::ManuallyDrop<_> = arc.clone();
    }

    /// Decrement the strong reference count.
    ///
    /// Takes pointer to Arc's data and byte offset of the data and decrements the strong reference
    /// count.
    ///
    /// If the reference count reaches 0, destructor is called.
    ///
    /// # Safety
    ///
    /// `ptr` and `offset` needs to be a pair acquired from `Arc::into_raw` function call. More
    /// specifically, `ptr as *const u8 - offset` must point to start of the data of original
    /// `BaseArc`.
    pub unsafe fn decrement_strong_count(ptr: *const T, offset: usize) {
        core::mem::drop(Arc::from_raw(ptr, offset));
    }

    /// Get `BaseArc<O>` out of this `Arc`
    ///
    /// # Safety
    ///
    /// The base arc must be of type `O`.
    pub unsafe fn untranspose<O>(self) -> BaseArc<O> {
        let (raw, offset) = self.into_raw();
        let raw = (raw as *const u8).sub(offset) as *const O;
        BaseArc::from_raw(raw)
    }

    /// Get the pointer to data of `BaseArc<O>`.
    ///
    /// # Safety
    ///
    /// The base arc must be of type `O`.
    pub unsafe fn as_original_ptr<O>(&self) -> *const O {
        (self.data.as_ptr() as *const u8).sub(self.offset) as *const O
    }

    pub fn is_original(&self) -> bool {
        self.offset == 0
    }

    pub fn into_base(self) -> Result<BaseArc<T>, Self> {
        if self.offset == 0 {
            let (raw, _) = self.into_raw();
            Ok(unsafe { BaseArc::from_raw(raw) })
        } else {
            Err(self)
        }
    }
}

/// Expand all function-like macros in docs before docs themselves are interpreted.
///
/// This is a workaround for rustc < 1.54, where doc attributes cannot have functional macros in
/// them.
macro_rules! doc {
    ( $(#[doc = $expr1:expr])* ### #[doc = $expr2:expr] $($tt:tt)* ) => {
        doc! {
            $(#[doc = $expr1])*
            #[doc = $expr2]
            ###
            $($tt)*
        }
    };
    ( $(#[doc = $exprs:expr])* ### $($tt:tt)* ) => {
        $(#[doc = $exprs])*
        $($tt)*
    };
    ( $($tt:tt)* ) => {
        doc! {
            ###
            $($tt)*
        }
    }
}

macro_rules! arc_traits {
    ($mname:ident, $ty:ident) => {

        impl<T: ?Sized> $ty<T> {
            pub fn strong_count(&self) -> usize {
                self.header().count.load(Ordering::Acquire)
            }

            pub fn as_ptr(&self) -> *const T {
                self.data.as_ptr() as *const _
            }

            pub fn exclusive_ptr(&self) -> Option<NonNull<T>> {
                if self.strong_count() == 1 {
                    Some(self.data)
                } else {
                    None
                }
            }

            pub fn get_mut(&mut self) -> Option<&mut T> {
                self.exclusive_ptr().map(|v| unsafe { &mut *v.as_ptr() })
            }
        }

        mod $mname {
            use super::*;
            impl<T: Default> Default for $ty<T> {
                doc! {
                    #[doc = concat!("Creates a new `", stringify!($ty), "<T>`, with the `Default` value for `T`.")]
                    ///
                    /// # Examples
                    ///
                    /// ```
                    #[doc = concat!("use tarc::", stringify!($ty), ";")]
                    ///
                    #[doc = concat!("let x: ", stringify!($ty), "<i32> = Default::default();")]
                    /// assert_eq!(*x, 0);
                    /// ```
                    fn default() -> Self {
                        Self::new(T::default())
                    }
                }
            }

            impl<T: ?Sized> core::ops::Deref for $ty<T> {
                type Target = T;

                fn deref(&self) -> &Self::Target {
                    unsafe { self.data.as_ref() }
                }
            }

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

            impl<T: ?Sized> Unpin for $ty<T> {}

            unsafe impl<T: ?Sized + Sync + Send> Send for $ty<T> {}
            unsafe impl<T: ?Sized + Sync + Send> Sync for $ty<T> {}

            doc! {
                #[doc = concat!("Converts a `T` into an `", stringify!($ty), "<T>`")]
                ///
                /// The conversion moves the value into a
                #[doc = concat!("newly allocated `", stringify!($ty), "`. It is equivalent to")]
                #[doc = concat!(" calling `", stringify!($ty), "::new(t)`")]
                ///
                /// # Example
                /// ```rust
                #[doc = concat!("# use tarc:: ", stringify!($ty), ";")]
                /// let x = 5;
                #[doc = concat!("let arc = ", stringify!($ty), "::new(5);")]
                ///
                #[doc = concat!("assert_eq!(", stringify!($ty), "::from(x), arc);")]
                /// ```
                impl<T> From<T> for $ty<T> {
                    fn from(t: T) -> Self {
                        Self::new(t)
                    }
                }
            }

            impl<T: ?Sized> Clone for $ty<T> {
                doc! {
                    #[doc = concat!("Makes a clone of the `", stringify!($ty), "` pointer.")]
                    ///
                    /// This creates another pointer to the same allocation, increasing the
                    /// strong reference count.
                    ///
                    /// # Examples
                    ///
                    /// ```
                    #[doc = concat!("use tarc::", stringify!($ty), ";")]
                    ///
                    #[doc = concat!("let five = ", stringify!($ty), "::new(5);")]
                    ///
                    #[doc = concat!("let _ = ", stringify!($ty), "::clone(&five);")]
                    /// ```
                    fn clone(&self) -> Self {
                        if self.header().count.fetch_add(1, Ordering::Relaxed) > core::isize::MAX as usize {
                            panic!()
                        }

                        Self { ..*self }
                    }
                }
            }

            impl<T: ?Sized> Drop for $ty<T> {
                doc! {
                    #[doc = concat!("Drops the `", stringify!($ty), "`.")]
                    ///
                    /// This will decrement the reference count. If the strong reference
                    /// count reaches zero then the no more references, so we `drop` the inner value.
                    ///
                    /// # Examples
                    ///
                    /// ```
                    #[doc = concat!("use tarc::", stringify!($ty), ";")]
                    ///
                    /// struct Foo;
                    ///
                    /// impl Drop for Foo {
                    ///     fn drop(&mut self) {
                    ///         println!("dropped!");
                    ///     }
                    /// }
                    ///
                    #[doc = concat!("let foo  = ", stringify!($ty), "::new(Foo);")]
                    #[doc = concat!("let foo2 = ", stringify!($ty), "::clone(&foo);")]
                    ///
                    /// drop(foo);    // Doesn't print anything
                    /// drop(foo2);   // Prints "dropped!"
                    /// ```
                    fn drop(&mut self) {
                        let header = self.header();

                        if header.count.fetch_sub(1, Ordering::Release) != 1 {
                            return;
                        }

                        fence(Ordering::SeqCst);

                        unsafe { (header.drop)(self.as_original_ptr::<()>() as *mut ()) }
                    }
                }
            }

            impl<T: ?Sized + PartialEq> PartialEq for $ty<T> {
                doc! {
                    #[doc = concat!("Equality for two `", stringify!($ty), "`s.")]
                    ///
                    #[doc = concat!("Two `", stringify!($ty), "`s are equal if their inner values are equal, even if they are")]
                    /// stored in different allocation.
                    ///
                    /// If `T` also implements `Eq` (implying reflexivity of equality),
                    #[doc = concat!("two `", stringify!($ty), "`s that point to the same allocation are always equal.")]
                    ///
                    /// # Examples
                    ///
                    /// ```
                    #[doc = concat!("use tarc::", stringify!($ty), ";")]
                    ///
                    #[doc = concat!("let five = ", stringify!($ty), "::new(5);")]
                    ///
                    #[doc = concat!("assert!(five == ", stringify!($ty), "::new(5));")]
                    /// ```
                    #[inline]
                    fn eq(&self, other: &$ty<T>) -> bool {
                        // TODO: implement this optimization with specialization
                        /*self.as_ptr() == other.as_ptr() ||*/ (**self == **other)
                    }
                }

                doc! {
                    #[doc = concat!("Inequality for two `", stringify!($ty), "`s.")]
                    ///
                    #[doc = concat!("Two `", stringify!($ty), "`s are unequal if their inner values are unequal.")]
                    ///
                    /// If `T` also implements `Eq` (implying reflexivity of equality),
                    #[doc = concat!("two `", stringify!($ty), "`s that point to the same value are never unequal.")]
                    ///
                    /// # Examples
                    ///
                    /// ```
                    #[doc = concat!("use tarc::", stringify!($ty), ";")]
                    ///
                    #[doc = concat!("let five = ", stringify!($ty), "::new(5);")]
                    ///
                    #[doc = concat!("assert!(five != ", stringify!($ty), "::new(6));")]
                    /// ```
                    #[inline]
                    #[allow(clippy::partialeq_ne_impl)]
                    fn ne(&self, other: &$ty<T>) -> bool {
                        // TODO: implement this optimization with specialization
                        /*self.as_ptr() != other.as_ptr() &&*/ (**self != **other)
                    }
                }
            }

            use core::cmp::{Ord, Ordering as CmpOrdering};

            impl<T: ?Sized + PartialOrd> PartialOrd for $ty<T> {
                doc! {
                    #[doc = concat!("Partial comparison for two `", stringify!($ty), "`s.")]
                    ///
                    /// The two are compared by calling `partial_cmp()` on their inner values.
                    ///
                    /// # Examples
                    ///
                    /// ```
                    #[doc = concat!("use tarc::", stringify!($ty), ";")]
                    /// use std::cmp::Ordering;
                    ///
                    #[doc = concat!("let five = ", stringify!($ty), "::new(5);")]
                    ///
                    #[doc = concat!("assert_eq!(Some(Ordering::Less), five.partial_cmp(&", stringify!($ty), "::new(6)));")]
                    /// ```
                    fn partial_cmp(&self, other: &$ty<T>) -> Option<CmpOrdering> {
                        (**self).partial_cmp(&**other)
                    }
                }

                doc! {
                    #[doc = concat!("Less-than comparison for two `", stringify!($ty), "`s.")]
                    ///
                    /// The two are compared by calling `<` on their inner values.
                    ///
                    /// # Examples
                    ///
                    /// ```
                    #[doc = concat!("use tarc::", stringify!($ty), ";")]
                    ///
                    #[doc = concat!("let five = ", stringify!($ty), "::new(5);")]
                    ///
                    #[doc = concat!("assert!(five < ", stringify!($ty), "::new(6));")]
                    /// ```
                    fn lt(&self, other: &$ty<T>) -> bool {
                        *(*self) < *(*other)
                    }
                }

                doc! {
                    #[doc = concat!("'Less than or equal to' comparison for two `", stringify!($ty), "`s.")]
                    ///
                    /// The two are compared by calling `<=` on their inner values.
                    ///
                    /// # Examples
                    ///
                    /// ```
                    #[doc = concat!("use tarc::", stringify!($ty), ";")]
                    ///
                    #[doc = concat!("let five = ", stringify!($ty), "::new(5);")]
                    ///
                    #[doc = concat!("assert!(five <= ", stringify!($ty), "::new(5));")]
                    /// ```
                    fn le(&self, other: &$ty<T>) -> bool {
                        *(*self) <= *(*other)
                    }
                }

                doc! {
                    #[doc = concat!("Greater-than comparison for two `", stringify!($ty), "`s.")]
                    ///
                    /// The two are compared by calling `>` on their inner values.
                    ///
                    /// # Examples
                    ///
                    /// ```
                    #[doc = concat!("use tarc::", stringify!($ty), ";")]
                    ///
                    #[doc = concat!("let five = ", stringify!($ty), "::new(5);")]
                    ///
                    #[doc = concat!("assert!(five > ", stringify!($ty), "::new(4));")]
                    /// ```
                    fn gt(&self, other: &$ty<T>) -> bool {
                        *(*self) > *(*other)
                    }
                }

                doc! {
                    #[doc = concat!("'Greater than or equal to' comparison for two `", stringify!($ty), "`s.")]
                    ///
                    /// The two are compared by calling `>=` on their inner values.
                    ///
                    /// # Examples
                    ///
                    /// ```
                    #[doc = concat!("use tarc::", stringify!($ty), ";")]
                    ///
                    #[doc = concat!("let five = ", stringify!($ty), "::new(5);")]
                    ///
                    #[doc = concat!("assert!(five >= ", stringify!($ty), "::new(5));")]
                    /// ```
                    fn ge(&self, other: &$ty<T>) -> bool {
                        *(*self) >= *(*other)
                    }
                }
            }

            impl<T: ?Sized + Eq> Eq for $ty<T> {}

            impl<T: ?Sized + Ord> Ord for $ty<T> {
                doc! {
                    #[doc = concat!("Comparison for two `", stringify!($ty), "`s.")]
                    ///
                    /// The two are compared by calling `cmp()` on their inner values.
                    ///
                    /// # Examples
                    ///
                    /// ```
                    #[doc = concat!("use tarc::", stringify!($ty), ";")]
                    /// use std::cmp::Ordering;
                    ///
                    #[doc = concat!("let five = ", stringify!($ty), "::new(5);")]
                    ///
                    #[doc = concat!("assert_eq!(Ordering::Less, five.cmp(& ", stringify!($ty), "::new(6)));")]
                    /// ```
                    fn cmp(&self, other: &$ty<T>) -> CmpOrdering {
                        (**self).cmp(&**other)
                    }
                }
            }

            use core::fmt;

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

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

            impl<T: ?Sized> fmt::Pointer for $ty<T> {
                fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                    fmt::Pointer::fmt(&(&**self as *const T), f)
                }
            }

            impl<T: ?Sized> core::borrow::Borrow<T> for $ty<T> {
                fn borrow(&self) -> &T {
                    &**self
                }
            }

            impl<T: ?Sized> AsRef<T> for $ty<T> {
                fn as_ref(&self) -> &T {
                    &**self
                }
            }

            #[cfg(feature = "std")]
            mod std_impl {
                use super::*;
                use std::error::Error;

                impl<T: Error + ?Sized> Error for $ty<T> {
                    #[allow(deprecated, deprecated_in_future)]
                    fn description(&self) -> &str {
                        Error::description(&**self)
                    }

                    #[allow(deprecated)]
                    fn cause(&self) -> Option<&dyn Error> {
                        Error::cause(&**self)
                    }

                    fn source(&self) -> Option<&(dyn Error + 'static)> {
                        Error::source(&**self)
                    }
                }
            }
        }
    };
}

arc_traits!(base_arc, BaseArc);
arc_traits!(arc, Arc);