1use core::fmt;
2use core::marker::PhantomData;
3use core::panic::{RefUnwindSafe, UnwindSafe};
4use core::ptr;
5
6use super::{Arc, ArcBorrow};
7
8pub struct ArcUnion<A, B> {
18 p: ptr::NonNull<()>,
19 phantom_a: PhantomData<A>,
20 phantom_b: PhantomData<B>,
21}
22
23unsafe impl<A: Sync + Send, B: Send + Sync> Send for ArcUnion<A, B> {}
24unsafe impl<A: Sync + Send, B: Send + Sync> Sync for ArcUnion<A, B> {}
25
26impl<A: RefUnwindSafe, B: RefUnwindSafe> UnwindSafe for ArcUnion<A, B> {}
27
28impl<A: PartialEq, B: PartialEq> PartialEq for ArcUnion<A, B> {
29 fn eq(&self, other: &Self) -> bool {
30 use crate::ArcUnionBorrow::*;
31 match (self.borrow(), other.borrow()) {
32 (First(x), First(y)) => x == y,
33 (Second(x), Second(y)) => x == y,
34 (_, _) => false,
35 }
36 }
37}
38
39#[derive(Debug)]
41pub enum ArcUnionBorrow<'a, A: 'a, B: 'a> {
42 First(ArcBorrow<'a, A>),
43 Second(ArcBorrow<'a, B>),
44}
45
46impl<A, B> ArcUnion<A, B> {
47 unsafe fn new(ptr: *mut ()) -> Self {
48 ArcUnion {
49 p: ptr::NonNull::new_unchecked(ptr),
50 phantom_a: PhantomData,
51 phantom_b: PhantomData,
52 }
53 }
54
55 #[inline]
57 pub fn ptr_eq(this: &Self, other: &Self) -> bool {
58 this.p == other.p
59 }
60
61 #[inline]
63 pub fn strong_count(this: &Self) -> usize {
64 ArcUnionBorrow::strong_count(&this.borrow())
65 }
66
67 pub fn borrow(&self) -> ArcUnionBorrow<'_, A, B> {
69 if self.is_first() {
70 let ptr = self.p.as_ptr() as *const A;
71 let borrow = unsafe { ArcBorrow::from_ptr(ptr) };
72 ArcUnionBorrow::First(borrow)
73 } else {
74 let ptr = ((self.p.as_ptr() as usize) & !0x1) as *const B;
75 let borrow = unsafe { ArcBorrow::from_ptr(ptr) };
76 ArcUnionBorrow::Second(borrow)
77 }
78 }
79
80 #[inline]
82 pub fn from_first(other: Arc<A>) -> Self {
83 unsafe { Self::new(Arc::into_raw(other) as *mut _) }
84 }
85
86 #[inline]
88 pub fn from_second(other: Arc<B>) -> Self {
89 unsafe { Self::new(((Arc::into_raw(other) as usize) | 0x1) as *mut _) }
90 }
91
92 #[inline]
94 pub fn is_first(&self) -> bool {
95 self.p.as_ptr() as usize & 0x1 == 0
96 }
97
98 #[inline]
100 pub fn is_second(&self) -> bool {
101 !self.is_first()
102 }
103
104 pub fn as_first(&self) -> Option<ArcBorrow<'_, A>> {
106 match self.borrow() {
107 ArcUnionBorrow::First(x) => Some(x),
108 ArcUnionBorrow::Second(_) => None,
109 }
110 }
111
112 pub fn as_second(&self) -> Option<ArcBorrow<'_, B>> {
114 match self.borrow() {
115 ArcUnionBorrow::First(_) => None,
116 ArcUnionBorrow::Second(x) => Some(x),
117 }
118 }
119}
120
121impl<A, B> Clone for ArcUnion<A, B> {
122 fn clone(&self) -> Self {
123 match self.borrow() {
124 ArcUnionBorrow::First(x) => ArcUnion::from_first(x.clone_arc()),
125 ArcUnionBorrow::Second(x) => ArcUnion::from_second(x.clone_arc()),
126 }
127 }
128}
129
130impl<A, B> Drop for ArcUnion<A, B> {
131 fn drop(&mut self) {
132 match self.borrow() {
133 ArcUnionBorrow::First(x) => unsafe {
134 let _ = Arc::from_raw(&*x);
135 },
136 ArcUnionBorrow::Second(x) => unsafe {
137 let _ = Arc::from_raw(&*x);
138 },
139 }
140 }
141}
142
143impl<A: fmt::Debug, B: fmt::Debug> fmt::Debug for ArcUnion<A, B> {
144 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
145 fmt::Debug::fmt(&self.borrow(), f)
146 }
147}
148
149impl<'a, A, B> ArcUnionBorrow<'a, A, B> {
150 #[inline]
159 pub fn strong_count(this: &Self) -> usize {
160 match this {
161 ArcUnionBorrow::First(arc) => ArcBorrow::strong_count(arc),
162 ArcUnionBorrow::Second(arc) => ArcBorrow::strong_count(arc),
163 }
164 }
165}