Skip to content

Commit 3f4ae88

Browse files
committed
Implement alloc::sync::{Arc,Weak,UniqueArc} with alloc::raw_rc types
1 parent e7da363 commit 3f4ae88

File tree

9 files changed

+617
-1514
lines changed

9 files changed

+617
-1514
lines changed

library/alloc/src/raw_rc/raw_rc.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ where
221221
self.weak.into_raw_parts()
222222
}
223223

224-
#[cfg(not(no_global_oom_handling))]
225224
pub(crate) unsafe fn is_unique<R>(&self) -> bool
226225
where
227226
R: RefCounter,
@@ -795,6 +794,23 @@ impl<A> RawRc<dyn Any, A> {
795794
}
796795
}
797796

797+
#[cfg(not(no_sync))]
798+
impl<A> RawRc<dyn Any + Send + Sync, A> {
799+
pub(crate) fn downcast<T>(self) -> Result<RawRc<T, A>, Self>
800+
where
801+
T: Any,
802+
{
803+
if self.as_ref().is::<T>() { Ok(unsafe { self.downcast_unchecked() }) } else { Err(self) }
804+
}
805+
806+
pub(crate) unsafe fn downcast_unchecked<T>(self) -> RawRc<T, A>
807+
where
808+
T: Any,
809+
{
810+
unsafe { self.cast() }
811+
}
812+
}
813+
798814
impl<T, A> AsRef<T> for RawRc<T, A>
799815
where
800816
T: ?Sized,

library/alloc/src/raw_rc/raw_weak.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,19 @@ where
192192
!ptr::addr_eq(self.ptr.as_ptr(), other.ptr.as_ptr())
193193
}
194194

195+
/// Returns the `RefCounts` object inside the reference-counted allocation if `self` is
196+
/// non-dangling.
197+
#[cfg(not(no_sync))]
198+
pub(crate) fn ref_counts(&self) -> Option<&RefCounts> {
199+
(!is_dangling(self.ptr.cast())).then(|| unsafe { self.ref_counts_unchecked() })
200+
}
201+
195202
/// Returns the `RefCounts` object inside the reference-counted allocation, assume `self` is
196203
/// non-dangling.
197204
///
198205
/// # Safety
199206
///
200207
/// `self` is non-dangling.
201-
#[cfg(not(no_global_oom_handling))]
202208
pub(super) unsafe fn ref_counts_unchecked(&self) -> &RefCounts {
203209
unsafe { self.value_ptr_unchecked().ref_counts_ptr().as_ref() }
204210
}

0 commit comments

Comments
 (0)