Skip to content

Commit ee6b40e

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

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,
@@ -794,6 +793,23 @@ impl<A> RawRc<dyn Any, A> {
794793
}
795794
}
796795

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