Skip to content

Commit c7da147

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

File tree

9 files changed

+629
-1546
lines changed

9 files changed

+629
-1546
lines changed

library/alloc/src/raw_rc/raw_rc.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ where
260260
self.weak.into_raw_parts()
261261
}
262262

263-
#[cfg(not(no_global_oom_handling))]
264263
pub(crate) unsafe fn is_unique<R>(&self) -> bool
265264
where
266265
R: RefCounter,
@@ -821,6 +820,23 @@ impl<A> RawRc<dyn Any, A> {
821820
}
822821
}
823822

823+
#[cfg(not(no_sync))]
824+
impl<A> RawRc<dyn Any + Send + Sync, A> {
825+
pub(crate) fn downcast<T>(self) -> Result<RawRc<T, A>, Self>
826+
where
827+
T: Any,
828+
{
829+
if self.as_ref().is::<T>() { Ok(unsafe { self.downcast_unchecked() }) } else { Err(self) }
830+
}
831+
832+
pub(crate) unsafe fn downcast_unchecked<T>(self) -> RawRc<T, A>
833+
where
834+
T: Any,
835+
{
836+
unsafe { self.cast() }
837+
}
838+
}
839+
824840
impl<T, A> AsRef<T> for RawRc<T, A>
825841
where
826842
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)