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
897
898
899
900
901
//
// Copyright (c) 2023 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.inner which is available at
// http://www.eclipse.org/legal/epl-2.inner, or the Apache License, Version 2.inner
// which is available at https://www.apache.org/licenses/LICENSE-2.inner.
//
// SPDX-License-Identifier: EPL-2.inner OR Apache-2.inner
//
// Contributors:
//   Pierre Avital, <pierre.avital@me.com>
//

use core::{
    fmt::Debug,
    hash::Hash,
    marker::PhantomData,
    mem::ManuallyDrop,
    ptr::NonNull,
    sync::atomic::{AtomicPtr, AtomicUsize, Ordering},
};

use crate::{vtable::HasDropVt, Dyn, IStable, IntoDyn};

use super::{
    vec::{ptr_add, ptr_diff, Vec, VecInner},
    AllocPtr, AllocSlice, DefaultAllocator, IAlloc,
};

/// [`alloc::sync::Arc`](https://doc.rust-lang.org/stable/alloc/sync/struct.Arc.html), but ABI-stable.
#[crate::stabby]
pub struct Arc<T, Alloc: IAlloc = super::DefaultAllocator> {
    ptr: AllocPtr<T, Alloc>,
}
unsafe impl<T: Send + Sync, Alloc: IAlloc + Send + Sync> Send for Arc<T, Alloc> {}
unsafe impl<T: Send + Sync, Alloc: IAlloc + Send + Sync> Sync for Arc<T, Alloc> {}
const USIZE_TOP_BIT: usize = 1 << (core::mem::size_of::<usize>() as i32 * 8 - 1);

impl<T> Arc<T> {
    /// Attempts to allocate [`Self`], initializing it with `constructor`.
    ///
    /// Note that the allocation may or may not be zeroed.
    ///
    /// # Panics
    /// If the allocator fails to provide an appropriate allocation.
    pub fn make<F: FnOnce(&mut core::mem::MaybeUninit<T>)>(constructor: F) -> Self {
        Self::make_in(constructor, DefaultAllocator::new())
    }
    /// Attempts to allocate [`Self`] and store `value` in it.
    ///
    /// # Panics
    /// If the allocator fails to provide an appropriate allocation.
    pub fn new(value: T) -> Self {
        Self::new_in(value, DefaultAllocator::new())
    }
}

impl<T, Alloc: IAlloc> Arc<T, Alloc> {
    /// Attempts to allocate [`Self`], initializing it with `constructor`.
    ///
    /// # Errors
    /// Returns the constructor and the allocator in case of allocation failure.
    ///
    /// # Notes
    /// Note that the allocation may or may not be zeroed.
    pub fn try_make_in<F: FnOnce(&mut core::mem::MaybeUninit<T>)>(
        constructor: F,
        mut alloc: Alloc,
    ) -> Result<Self, (F, Alloc)> {
        let mut ptr = match AllocPtr::alloc(&mut alloc) {
            Some(mut ptr) => {
                unsafe { ptr.prefix_mut().alloc.write(alloc) };
                ptr
            }
            None => return Err((constructor, alloc)),
        };
        unsafe {
            constructor(ptr.as_mut());
            ptr.prefix_mut().strong = AtomicUsize::new(1);
            ptr.prefix_mut().weak = AtomicUsize::new(1)
        }
        Ok(Self {
            ptr: unsafe { ptr.assume_init() },
        })
    }
    /// Attempts to allocate a [`Self`] and store `value` in it
    /// # Errors
    /// Returns `value` and the allocator in case of failure.
    pub fn try_new_in(value: T, alloc: Alloc) -> Result<Self, (T, Alloc)> {
        let this = Self::try_make_in(
            |slot| unsafe {
                slot.write(core::ptr::read(&value));
            },
            alloc,
        );
        match this {
            Ok(this) => Ok(this),
            Err((_, a)) => Err((value, a)),
        }
    }
    /// Attempts to allocate [`Self`], initializing it with `constructor`.
    ///
    /// Note that the allocation may or may not be zeroed.
    ///
    /// # Panics
    /// If the allocator fails to provide an appropriate allocation.
    pub fn make_in<F: FnOnce(&mut core::mem::MaybeUninit<T>)>(
        constructor: F,
        mut alloc: Alloc,
    ) -> Self {
        let mut ptr = match AllocPtr::alloc(&mut alloc) {
            Some(mut ptr) => unsafe {
                ptr.prefix_mut().alloc.write(alloc);
                ptr
            },
            None => panic!("Allocation failed"),
        };
        unsafe {
            constructor(ptr.as_mut());
            ptr.prefix_mut().strong = AtomicUsize::new(1);
            ptr.prefix_mut().weak = AtomicUsize::new(1)
        }
        Self {
            ptr: unsafe { ptr.assume_init() },
        }
    }
    /// Attempts to allocate [`Self`] and store `value` in it.
    ///
    /// # Panics
    /// If the allocator fails to provide an appropriate allocation.
    pub fn new_in(value: T, alloc: Alloc) -> Self {
        Self::make_in(
            move |slot| {
                slot.write(value);
            },
            alloc,
        )
    }

    /// Returns the pointer to the inner raw allocation, leaking `this`.
    ///
    /// Note that the pointer may be dangling if `T` is zero-sized.
    pub const fn into_raw(this: Self) -> AllocPtr<T, Alloc> {
        let inner = this.ptr;
        core::mem::forget(this);
        inner
    }
    /// Constructs `Self` from a raw allocation.
    /// # Safety
    /// `this` MUST not be dangling, and have been obtained through [`Self::into_raw`].
    pub const unsafe fn from_raw(this: AllocPtr<T, Alloc>) -> Self {
        Self { ptr: this }
    }

    /// Provides a mutable reference to the internals if the strong and weak counts are both 1.
    pub fn get_mut(this: &mut Self) -> Option<&mut T> {
        if Self::is_unique(this) {
            Some(unsafe { Self::get_mut_unchecked(this) })
        } else {
            None
        }
    }

    /// Provides a mutable reference to the internals without checking.
    /// # Safety
    /// If used carelessly, this can cause mutable references and immutable references to the same value to appear,
    /// causing undefined behaviour.
    pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {
        unsafe { this.ptr.as_mut() }
    }

    /// Returns the strong count.
    pub fn strong_count(this: &Self) -> usize {
        unsafe { this.ptr.prefix() }.strong.load(Ordering::Relaxed)
    }
    /// Increments the strong count.
    /// # Safety
    /// `this` MUST be a valid pointer derived from `Self`
    pub unsafe fn increment_strong_count(this: *const T) -> usize {
        let ptr: AllocPtr<T, Alloc> = AllocPtr {
            ptr: NonNull::new_unchecked(this.cast_mut()),
            marker: core::marker::PhantomData,
        };
        unsafe { ptr.prefix() }
            .strong
            .fetch_add(1, Ordering::Relaxed)
    }
    /// Returns the weak count. Note that all Arcs to a same value share a Weak, so the weak count can never be 0.
    pub fn weak_count(this: &Self) -> usize {
        unsafe { this.ptr.prefix() }.weak.load(Ordering::Relaxed)
    }
    /// Increments the weak count, returning its previous value.
    pub fn increment_weak_count(this: &Self) -> usize {
        unsafe { this.ptr.prefix() }
            .weak
            .fetch_add(1, Ordering::Relaxed)
    }

    /// Returns a mutable reference to this `Arc`'s value, cloning that value into a new `Arc` if [`Self::get_mut`] would have failed.
    pub fn make_mut(&mut self) -> &mut T
    where
        T: Clone,
        Alloc: Clone,
    {
        if !Self::is_unique(self) {
            *self = Self::new_in(
                T::clone(self),
                unsafe { self.ptr.prefix().alloc.assume_init_ref() }.clone(),
            );
        }
        unsafe { Self::get_mut_unchecked(self) }
    }

    /// Whether or not `this` is the sole owner of its data, including weak owners.
    pub fn is_unique(this: &Self) -> bool {
        Self::strong_count(this) == 1 && Self::weak_count(this) == 1
    }
    /// Attempts the value from the allocation, freeing said allocation.
    /// # Errors
    /// Returns `this` if it's not the sole owner of its value.
    pub fn try_into_inner(this: Self) -> Result<T, Self> {
        if !Self::is_unique(&this) {
            Err(this)
        } else {
            let ret = unsafe { core::ptr::read(&*this) };
            _ = unsafe { Weak::<T, Alloc>::from_raw(Arc::into_raw(this)) };
            Ok(ret)
        }
    }

    /// Constructs an additional [`Weak`] pointer to `this`.
    pub fn downgrade(this: &Self) -> Weak<T, Alloc> {
        this.into()
    }
    #[rustversion::since(1.73)]
    /// Returns a reference to the allocator used to construct `this`
    pub const fn allocator(this: &Self) -> &Alloc {
        unsafe { this.ptr.prefix().alloc.assume_init_ref() }
    }
    #[rustversion::before(1.73)]
    /// Returns a reference to the allocator used to construct `this`
    pub fn allocator(this: &Self) -> &Alloc {
        unsafe { this.ptr.prefix().alloc.assume_init_ref() }
    }
}
impl<T, Alloc: IAlloc> Drop for Arc<T, Alloc> {
    fn drop(&mut self) {
        if unsafe { self.ptr.prefix() }
            .strong
            .fetch_sub(1, Ordering::Relaxed)
            != 1
        {
            return;
        }
        unsafe {
            core::ptr::drop_in_place(self.ptr.as_mut());
            _ = Weak::<T, Alloc>::from_raw(self.ptr);
        }
    }
}
impl<T, Alloc: IAlloc> Clone for Arc<T, Alloc> {
    fn clone(&self) -> Self {
        unsafe { self.ptr.prefix() }
            .strong
            .fetch_add(1, Ordering::Relaxed);
        Self { ptr: self.ptr }
    }
}
impl<T, Alloc: IAlloc> core::ops::Deref for Arc<T, Alloc> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        unsafe { self.ptr.as_ref() }
    }
}

/// [`alloc::sync::Weak`](https://doc.rust-lang.org/stable/alloc/sync/struct.Weak.html), but ABI-stable.
#[crate::stabby]
pub struct Weak<T, Alloc: IAlloc = super::DefaultAllocator> {
    ptr: AllocPtr<T, Alloc>,
}
unsafe impl<T: Send + Sync, Alloc: IAlloc + Send + Sync> Send for Weak<T, Alloc> {}
unsafe impl<T: Send + Sync, Alloc: IAlloc + Send + Sync> Sync for Weak<T, Alloc> {}
impl<T, Alloc: IAlloc> From<&Arc<T, Alloc>> for Weak<T, Alloc> {
    fn from(value: &Arc<T, Alloc>) -> Self {
        unsafe { value.ptr.prefix() }
            .weak
            .fetch_add(1, Ordering::Relaxed);
        Self { ptr: value.ptr }
    }
}
impl<T, Alloc: IAlloc> Weak<T, Alloc> {
    /// Returns the pointer to the inner raw allocation, leaking `this`.
    ///
    /// Note that the pointer may be dangling if `T` is zero-sized.
    pub const fn into_raw(this: Self) -> AllocPtr<T, Alloc> {
        let inner = this.ptr;
        core::mem::forget(this);
        inner
    }
    /// Constructs `Self` from a raw allocation.
    /// # Safety
    /// `this` MUST not be dangling, and have been obtained through [`Self::into_raw`].
    pub const unsafe fn from_raw(this: AllocPtr<T, Alloc>) -> Self {
        Self { ptr: this }
    }
    /// Attempts to upgrade self into an Arc.
    pub fn upgrade(&self) -> Option<Arc<T, Alloc>> {
        let strong = &unsafe { self.ptr.prefix() }.strong;
        let count = strong.fetch_or(USIZE_TOP_BIT, Ordering::Acquire);
        match count {
            0 | USIZE_TOP_BIT => {
                strong.store(0, Ordering::Release);
                None
            }
            _ => {
                strong.fetch_add(1, Ordering::Release);
                strong.fetch_and(!USIZE_TOP_BIT, Ordering::Release);
                Some(Arc { ptr: self.ptr })
            }
        }
    }
}
impl<T, Alloc: IAlloc> Clone for Weak<T, Alloc> {
    fn clone(&self) -> Self {
        unsafe { self.ptr.prefix() }
            .weak
            .fetch_add(1, Ordering::Relaxed);
        Self { ptr: self.ptr }
    }
}
impl<T, Alloc: IAlloc> Drop for Weak<T, Alloc> {
    fn drop(&mut self) {
        if unsafe { self.ptr.prefix() }
            .weak
            .fetch_sub(1, Ordering::Relaxed)
            != 1
        {
            return;
        }
        unsafe {
            let mut alloc = self.ptr.prefix().alloc.assume_init_read();
            self.ptr.free(&mut alloc)
        }
    }
}

/// A strong reference to a fixed size slice of elements.
///
/// Equivalent to `alloc::sync::Arc<[T]>`
#[crate::stabby]
pub struct ArcSlice<T, Alloc: IAlloc = super::DefaultAllocator> {
    pub(crate) inner: AllocSlice<T, Alloc>,
}
unsafe impl<T: Send + Sync, Alloc: IAlloc + Send + Sync> Send for ArcSlice<T, Alloc> {}
unsafe impl<T: Send + Sync, Alloc: IAlloc + Send + Sync> Sync for ArcSlice<T, Alloc> {}
unsafe impl<T: Send + Sync, Alloc: IAlloc + Send + Sync> Send for WeakSlice<T, Alloc> {}
unsafe impl<T: Send + Sync, Alloc: IAlloc + Send + Sync> Sync for WeakSlice<T, Alloc> {}

impl<T, Alloc: IAlloc> ArcSlice<T, Alloc> {
    /// Returns the number of elements in the slice.
    pub const fn len(&self) -> usize {
        ptr_diff(self.inner.end, self.inner.start.ptr)
    }
    /// Returns true if the slice is empty.
    pub const fn is_empty(&self) -> bool {
        self.len() == 0
    }
    /// Returns a borrow to the slice.
    pub fn as_slice(&self) -> &[T] {
        let start = self.inner.start;
        unsafe { core::slice::from_raw_parts(start.as_ptr(), self.len()) }
    }
    /// Returns a mutable borrow to the slice if no other references to it may exist.
    pub fn as_slice_mut(&mut self) -> Option<&mut [T]> {
        (ArcSlice::strong_count(self) == 1 && ArcSlice::weak_count(self) == 1)
            .then(|| unsafe { self.as_slice_mut_unchecked() })
    }
    /// Returns a mutable borrow to the slice.
    /// # Safety
    /// This can easily create aliased mutable references, which would be undefined behaviour.
    pub unsafe fn as_slice_mut_unchecked(&mut self) -> &mut [T] {
        let start = self.inner.start;
        unsafe { core::slice::from_raw_parts_mut(start.as_ptr(), self.len()) }
    }
    /// Returns the strong count to the slice.
    pub fn strong_count(this: &Self) -> usize {
        unsafe { this.inner.start.prefix().strong.load(Ordering::Relaxed) }
    }
    /// Returns the weak count to the slice.
    pub fn weak_count(this: &Self) -> usize {
        unsafe { this.inner.start.prefix().weak.load(Ordering::Relaxed) }
    }
    /// Whether or not `this` is the sole owner of its data, including weak owners.
    pub fn is_unique(this: &Self) -> bool {
        Self::strong_count(this) == 1 && Self::weak_count(this) == 1
    }
    /// Returns the slice's raw representation, without altering the associated reference counts.
    ///
    /// Failing to reconstruct the `this` using [`Self::from_raw`] will result in the associated `this` being effectively leaked.
    pub const fn into_raw(this: Self) -> AllocSlice<T, Alloc> {
        let inner = this.inner;
        core::mem::forget(this);
        inner
    }
    /// Reconstructs an [`ArcSlice`] from its raw representation, without altering the associated reference counts.
    ///
    /// # Safety
    /// `this` MUST have been obtained using [`Self::into_raw`], and not have been previously used to reconstruct an [`ArcSlice`].
    pub const unsafe fn from_raw(this: AllocSlice<T, Alloc>) -> Self {
        Self { inner: this }
    }
}
impl<T, Alloc: IAlloc> core::ops::Deref for ArcSlice<T, Alloc> {
    type Target = [T];
    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}
impl<T, Alloc: IAlloc> Clone for ArcSlice<T, Alloc> {
    fn clone(&self) -> Self {
        unsafe { self.inner.start.prefix() }
            .strong
            .fetch_add(1, Ordering::Relaxed);
        Self { inner: self.inner }
    }
}
impl<T, Alloc: IAlloc> From<Arc<T, Alloc>> for ArcSlice<T, Alloc> {
    fn from(mut value: Arc<T, Alloc>) -> Self {
        unsafe { value.ptr.prefix_mut() }.capacity = AtomicUsize::new(1);
        Self {
            inner: AllocSlice {
                start: value.ptr,
                end: ptr_add(value.ptr.ptr, 1),
            },
        }
    }
}
impl<T: Copy, Alloc: IAlloc + Default> From<&[T]> for ArcSlice<T, Alloc> {
    fn from(value: &[T]) -> Self {
        Vec::from(value).into()
    }
}
impl<T, Alloc: IAlloc> From<Vec<T, Alloc>> for ArcSlice<T, Alloc> {
    fn from(value: Vec<T, Alloc>) -> Self {
        let (mut slice, capacity, mut alloc) = value.into_raw_components();
        if capacity != 0 {
            unsafe {
                slice.start.prefix_mut().strong = AtomicUsize::new(1);
                slice.start.prefix_mut().weak = AtomicUsize::new(1);
                slice.start.prefix_mut().capacity = AtomicUsize::new(capacity);
                slice.start.prefix_mut().alloc.write(alloc);
            }
            Self {
                inner: AllocSlice {
                    start: slice.start,
                    end: slice.end,
                },
            }
        } else {
            let mut start = AllocPtr::alloc_array(&mut alloc, 0).expect("Allocation failed");
            unsafe {
                start.prefix_mut().strong = AtomicUsize::new(1);
                start.prefix_mut().weak = AtomicUsize::new(1);
                start.prefix_mut().capacity = if core::mem::size_of::<T>() != 0 {
                    AtomicUsize::new(0)
                } else {
                    AtomicUsize::new(ptr_diff(
                        core::mem::transmute::<usize, NonNull<u8>>(usize::MAX),
                        start.ptr.cast::<u8>(),
                    ))
                };
                slice.start.prefix_mut().alloc.write(alloc);
            }
            Self {
                inner: AllocSlice {
                    start,
                    end: ptr_add(start.ptr.cast::<u8>(), slice.len()).cast(),
                },
            }
        }
    }
}
impl<T, Alloc: IAlloc> TryFrom<ArcSlice<T, Alloc>> for Vec<T, Alloc> {
    type Error = ArcSlice<T, Alloc>;
    fn try_from(value: ArcSlice<T, Alloc>) -> Result<Self, Self::Error> {
        if core::mem::size_of::<T>() == 0 || !ArcSlice::is_unique(&value) {
            Err(value)
        } else {
            unsafe {
                let ret = Vec {
                    inner: VecInner {
                        start: value.inner.start,
                        end: value.inner.end,
                        capacity: ptr_add(
                            value.inner.start.ptr,
                            value.inner.start.prefix().capacity.load(Ordering::Relaxed),
                        ),
                        alloc: value.inner.start.prefix().alloc.assume_init_read(),
                    },
                };
                core::mem::forget(value);
                Ok(ret)
            }
        }
    }
}
impl<T: Eq, Alloc: IAlloc> Eq for ArcSlice<T, Alloc> {}
impl<T: PartialEq, Alloc: IAlloc> PartialEq for ArcSlice<T, Alloc> {
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}
impl<T: Ord, Alloc: IAlloc> Ord for ArcSlice<T, Alloc> {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.as_slice().cmp(other.as_slice())
    }
}
impl<T: PartialOrd, Alloc: IAlloc> PartialOrd for ArcSlice<T, Alloc> {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        self.as_slice().partial_cmp(other.as_slice())
    }
}
impl<T: Hash, Alloc: IAlloc> Hash for ArcSlice<T, Alloc> {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.as_slice().hash(state)
    }
}
impl<T, Alloc: IAlloc> Drop for ArcSlice<T, Alloc> {
    fn drop(&mut self) {
        if unsafe { self.inner.start.prefix() }
            .strong
            .fetch_sub(1, Ordering::Relaxed)
            != 1
        {
            return;
        }
        unsafe { core::ptr::drop_in_place(self.as_slice_mut_unchecked()) }
        _ = WeakSlice { inner: self.inner };
    }
}
impl<T: Debug, Alloc: IAlloc> Debug for ArcSlice<T, Alloc> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.as_slice().fmt(f)
    }
}
impl<T: core::fmt::LowerHex, Alloc: IAlloc> core::fmt::LowerHex for ArcSlice<T, Alloc> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut first = true;
        for item in self {
            if !first {
                f.write_str(":")?;
            }
            first = false;
            core::fmt::LowerHex::fmt(item, f)?;
        }
        Ok(())
    }
}
impl<T: core::fmt::UpperHex, Alloc: IAlloc> core::fmt::UpperHex for ArcSlice<T, Alloc> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut first = true;
        for item in self {
            if !first {
                f.write_str(":")?;
            }
            first = false;
            core::fmt::UpperHex::fmt(item, f)?;
        }
        Ok(())
    }
}
impl<'a, T, Alloc: IAlloc> IntoIterator for &'a ArcSlice<T, Alloc> {
    type Item = &'a T;
    type IntoIter = core::slice::Iter<'a, T>;
    fn into_iter(self) -> Self::IntoIter {
        self.as_slice().iter()
    }
}

/// A weak reference counted slice.
#[crate::stabby]
pub struct WeakSlice<T, Alloc: IAlloc = super::DefaultAllocator> {
    pub(crate) inner: AllocSlice<T, Alloc>,
}

impl<T, Alloc: IAlloc> WeakSlice<T, Alloc> {
    /// Return a strong reference to the slice if it hasn't been destroyed yet.
    pub fn upgrade(&self) -> Option<ArcSlice<T, Alloc>> {
        let strong = &unsafe { self.inner.start.prefix() }.strong;
        let count = strong.fetch_or(USIZE_TOP_BIT, Ordering::Acquire);
        match count {
            0 | USIZE_TOP_BIT => {
                strong.store(0, Ordering::Release);
                None
            }
            _ => {
                strong.fetch_add(1, Ordering::Release);
                strong.fetch_and(!USIZE_TOP_BIT, Ordering::Release);
                Some(ArcSlice { inner: self.inner })
            }
        }
    }
    /// For types that are [`Copy`], the slice actually remains valid even after all strong references
    /// have been dropped as long as at least a weak reference lives on.
    ///
    /// If you're using this, there are probably design issues in your program...
    pub fn force_upgrade(&self) -> ArcSlice<T, Alloc>
    where
        T: Copy,
    {
        let strong = &unsafe { self.inner.start.prefix() }.strong;
        match strong.fetch_add(1, Ordering::Release) {
            0 | USIZE_TOP_BIT => {
                unsafe { self.inner.start.prefix() }
                    .weak
                    .fetch_add(1, Ordering::Relaxed);
            }
            _ => {}
        }
        ArcSlice { inner: self.inner }
    }
}
impl<T, Alloc: IAlloc> Clone for WeakSlice<T, Alloc> {
    fn clone(&self) -> Self {
        unsafe { self.inner.start.prefix() }
            .weak
            .fetch_add(1, Ordering::Relaxed);
        Self { inner: self.inner }
    }
}
impl<T, Alloc: IAlloc> From<&ArcSlice<T, Alloc>> for WeakSlice<T, Alloc> {
    fn from(value: &ArcSlice<T, Alloc>) -> Self {
        unsafe { value.inner.start.prefix() }
            .weak
            .fetch_add(1, Ordering::Relaxed);
        Self { inner: value.inner }
    }
}
impl<T, Alloc: IAlloc> Drop for WeakSlice<T, Alloc> {
    fn drop(&mut self) {
        if unsafe { self.inner.start.prefix() }
            .weak
            .fetch_sub(1, Ordering::Relaxed)
            != 1
        {
            return;
        }
        let mut alloc = unsafe { self.inner.start.prefix().alloc.assume_init_read() };
        unsafe { self.inner.start.free(&mut alloc) }
    }
}
pub use super::string::{ArcStr, WeakStr};

impl<T, Alloc: IAlloc> crate::IPtr for Arc<T, Alloc> {
    unsafe fn as_ref<U: Sized>(&self) -> &U {
        self.ptr.cast().as_ref()
    }
}
impl<T, Alloc: IAlloc> crate::IPtrClone for Arc<T, Alloc> {
    fn clone(this: &Self) -> Self {
        this.clone()
    }
}

impl<T, Alloc: IAlloc> crate::IPtrTryAsMut for Arc<T, Alloc> {
    unsafe fn try_as_mut<U: Sized>(&mut self) -> Option<&mut U> {
        Self::get_mut(self).map(|r| unsafe { core::mem::transmute::<&mut T, &mut U>(r) })
    }
}
impl<T, Alloc: IAlloc> crate::IPtrOwned for Arc<T, Alloc> {
    fn drop(this: &mut core::mem::ManuallyDrop<Self>, drop: unsafe extern "C" fn(&mut ())) {
        if unsafe { this.ptr.prefix() }
            .strong
            .fetch_sub(1, Ordering::Relaxed)
            != 1
        {
            return;
        }
        unsafe {
            drop(this.ptr.cast().as_mut());
            _ = Weak::<T, Alloc>::from_raw(this.ptr);
        }
    }
}

impl<T, Alloc: IAlloc> IntoDyn for Arc<T, Alloc> {
    type Anonymized = Arc<(), Alloc>;
    type Target = T;
    fn anonimize(self) -> Self::Anonymized {
        let original_prefix = self.ptr.prefix_ptr();
        let anonymized = unsafe { core::mem::transmute::<Self, Self::Anonymized>(self) };
        let anonymized_prefix = anonymized.ptr.prefix_ptr();
        assert_eq!(anonymized_prefix, original_prefix, "The allocation prefix was lost in anonimization, this is definitely a bug, please report it.");
        anonymized
    }
}

impl<T, Alloc: IAlloc> crate::IPtrOwned for Weak<T, Alloc> {
    fn drop(this: &mut core::mem::ManuallyDrop<Self>, drop: unsafe extern "C" fn(&mut ())) {
        if unsafe { this.ptr.prefix() }
            .strong
            .fetch_sub(1, Ordering::Relaxed)
            != 1
        {
            return;
        }
        unsafe {
            drop(this.ptr.cast().as_mut());
            _ = Weak::<T, Alloc>::from_raw(this.ptr);
        }
    }
}

impl<T, Alloc: IAlloc> crate::IPtrClone for Weak<T, Alloc> {
    fn clone(this: &Self) -> Self {
        this.clone()
    }
}

impl<T, Alloc: IAlloc> IntoDyn for Weak<T, Alloc> {
    type Anonymized = Weak<(), Alloc>;
    type Target = T;
    fn anonimize(self) -> Self::Anonymized {
        let original_prefix = self.ptr.prefix_ptr();
        let anonymized = unsafe { core::mem::transmute::<Self, Self::Anonymized>(self) };
        let anonymized_prefix = anonymized.ptr.prefix_ptr();
        assert_eq!(anonymized_prefix, original_prefix, "The allocation prefix was lost in anonimization, this is definitely a bug, please report it.");
        anonymized
    }
}

impl<'a, Vt: HasDropVt, Alloc: IAlloc> From<&'a Dyn<'a, Arc<(), Alloc>, Vt>>
    for Dyn<'a, Weak<(), Alloc>, Vt>
{
    fn from(value: &'a Dyn<'a, Arc<(), Alloc>, Vt>) -> Self {
        Self {
            ptr: ManuallyDrop::new(Arc::downgrade(&value.ptr)),
            vtable: value.vtable,
            unsend: core::marker::PhantomData,
        }
    }
}
impl<'a, Vt: HasDropVt + IStable, Alloc: IAlloc> Dyn<'a, Weak<(), Alloc>, Vt> {
    /// Attempts to upgrade a weak trait object to a strong one.
    pub fn upgrade(self) -> crate::option::Option<Dyn<'a, Arc<(), Alloc>, Vt>> {
        let Some(ptr) = self.ptr.upgrade() else {
            return crate::option::Option::None();
        };
        crate::option::Option::Some(Dyn {
            ptr: ManuallyDrop::new(ptr),
            vtable: self.vtable,
            unsend: core::marker::PhantomData,
        })
    }
}

#[crate::stabby]
/// An owner of an [`Arc<T, Alloc>`] whose pointee can be atomically changed.
pub struct AtomicArc<T, Alloc: IAlloc> {
    ptr: AtomicPtr<T>,
    alloc: core::marker::PhantomData<*const Alloc>,
}
unsafe impl<T: Send + Sync, Alloc: IAlloc + Send + Sync> Send for AtomicArc<T, Alloc> {}
unsafe impl<T: Send + Sync, Alloc: IAlloc + Send + Sync> Sync for AtomicArc<T, Alloc> {}

impl<T, Alloc: IAlloc> Drop for AtomicArc<T, Alloc> {
    fn drop(&mut self) {
        let ptr = self.ptr.load(Ordering::Relaxed);
        if let Some(ptr) = NonNull::new(ptr) {
            unsafe {
                Arc::<T, Alloc>::from_raw(AllocPtr {
                    ptr,
                    marker: PhantomData,
                })
            };
        }
    }
}

type MaybeArc<T, Alloc> = Option<Arc<T, Alloc>>;
impl<T, Alloc: IAlloc> AtomicArc<T, Alloc> {
    /// Constructs a new [`AtomicArc`] set to the provided value.
    pub const fn new(value: MaybeArc<T, Alloc>) -> Self {
        Self {
            ptr: AtomicPtr::new(unsafe {
                core::mem::transmute::<Option<Arc<T, Alloc>>, *mut T>(value)
            }),
            alloc: PhantomData,
        }
    }
    /// Atomically load the current value.
    pub fn load(&self, order: Ordering) -> MaybeArc<T, Alloc> {
        let ptr = NonNull::new(self.ptr.load(order))?;
        unsafe {
            Arc::<T, Alloc>::increment_strong_count(ptr.as_ptr());
            Some(Arc::from_raw(AllocPtr {
                ptr,
                marker: PhantomData,
            }))
        }
    }
    /// Atomically store a new value.
    pub fn store(&self, value: MaybeArc<T, Alloc>, order: Ordering) {
        let ptr = value.map_or(core::ptr::null_mut(), |value| Arc::into_raw(value).as_ptr());
        self.ptr.store(ptr, order)
    }
    /// Compares `self` with `current` by pointer.
    /// # Errors
    /// Returns the new value of `self` if it differs from `current`.
    pub fn is(
        &self,
        current: Option<&Arc<T, Alloc>>,
        order: Ordering,
    ) -> Result<(), MaybeArc<T, Alloc>> {
        let ptr = NonNull::new(self.ptr.load(order));
        match (ptr, current) {
            (None, None) => Ok(()),
            (None, _) => Err(None),
            (Some(ptr), Some(current)) if core::ptr::eq(ptr.as_ptr(), current.ptr.as_ptr()) => {
                Ok(())
            }
            (Some(ptr), _) => unsafe {
                Arc::<T, Alloc>::increment_strong_count(ptr.as_ptr());
                Err(Some(Arc::from_raw(AllocPtr {
                    ptr,
                    marker: PhantomData,
                })))
            },
        }
    }
    /// Replace the current value with the new value.
    /// # Errors
    /// If `current` no longer points to the same value as `self`, it
    pub fn compare_exchange(
        &self,
        current: Option<&Arc<T, Alloc>>,
        new: MaybeArc<T, Alloc>,
        success: Ordering,
        failure: Ordering,
    ) -> Result<MaybeArc<T, Alloc>, MaybeArc<T, Alloc>> {
        let current = current.map_or(core::ptr::null_mut(), |value| value.ptr.ptr.as_ptr());
        let new = new.map_or(core::ptr::null_mut(), |value| Arc::into_raw(value).as_ptr());
        match self.ptr.compare_exchange(current, new, success, failure) {
            Ok(ptr) => Ok(NonNull::new(ptr).map(|ptr| unsafe {
                Arc::from_raw(AllocPtr {
                    ptr,
                    marker: PhantomData,
                })
            })),
            Err(ptr) => Err(NonNull::new(ptr).map(|ptr| unsafe {
                Arc::<T, Alloc>::increment_strong_count(ptr.as_ptr());
                Arc::from_raw(AllocPtr {
                    ptr,
                    marker: PhantomData,
                })
            })),
        }
    }
}

#[cfg(feature = "serde")]
mod serde_impl {
    use super::*;
    use crate::alloc::IAlloc;
    use serde::{Deserialize, Serialize};
    impl<T: Serialize, Alloc: IAlloc> Serialize for ArcSlice<T, Alloc> {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: serde::Serializer,
        {
            let slice: &[T] = self;
            slice.serialize(serializer)
        }
    }
    impl<'a, T: Deserialize<'a>, Alloc: IAlloc + Default> Deserialize<'a> for ArcSlice<T, Alloc> {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'a>,
        {
            crate::alloc::vec::Vec::deserialize(deserializer).map(Into::into)
        }
    }
    impl<Alloc: IAlloc> Serialize for ArcStr<Alloc> {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: serde::Serializer,
        {
            let slice: &str = self;
            slice.serialize(serializer)
        }
    }
    impl<'a, Alloc: IAlloc + Default> Deserialize<'a> for ArcStr<Alloc> {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'a>,
        {
            crate::alloc::string::String::deserialize(deserializer).map(Into::into)
        }
    }
}