Skip to content

Commit 1b1e24f

Browse files
committed
Implement alloc::sync::{Arc,Weak,UniqueArc} with alloc::raw_rc types
1 parent c8438de commit 1b1e24f

File tree

9 files changed

+621
-1515
lines changed

9 files changed

+621
-1515
lines changed

library/alloc/src/raw_rc/raw_rc.rs

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

222-
#[cfg(not(no_global_oom_handling))]
223222
pub(crate) unsafe fn is_unique<R>(&self) -> bool
224223
where
225224
R: RefCounter,
@@ -796,6 +795,23 @@ impl<A> RawRc<dyn Any, A> {
796795
}
797796
}
798797

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