Skip to content

Commit e2e1c12

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

File tree

10 files changed

+612
-1519
lines changed

10 files changed

+612
-1519
lines changed

library/alloc/src/raw_rc/raw_rc.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ where
216216
self.weak.into_raw_parts()
217217
}
218218

219-
#[cfg(not(no_global_oom_handling))]
220219
pub(crate) unsafe fn is_unique<R>(&self) -> bool
221220
where
222221
R: RefCounter,
@@ -779,6 +778,23 @@ impl<A> RawRc<dyn Any, A> {
779778
}
780779
}
781780

781+
#[cfg(not(no_sync))]
782+
impl<A> RawRc<dyn Any + Send + Sync, A> {
783+
pub(crate) fn downcast<T>(self) -> Result<RawRc<T, A>, Self>
784+
where
785+
T: Any,
786+
{
787+
if self.as_ref().is::<T>() { Ok(unsafe { self.downcast_unchecked() }) } else { Err(self) }
788+
}
789+
790+
pub(crate) unsafe fn downcast_unchecked<T>(self) -> RawRc<T, A>
791+
where
792+
T: Any,
793+
{
794+
unsafe { self.cast() }
795+
}
796+
}
797+
782798
impl<T, A> AsRef<T> for RawRc<T, A>
783799
where
784800
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
}

library/alloc/src/rc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ use crate::vec::Vec;
274274

275275
type RefCounter = Cell<usize>;
276276

277-
unsafe impl raw_rc::RefCounter for RefCounter {
277+
unsafe impl raw_rc::RefCounter for Cell<usize> {
278278
#[inline]
279279
fn increment(&self) {
280280
// NOTE: If you `mem::forget` `Rc`s (or `Weak`s), drop is skipped and the ref-count

0 commit comments

Comments
 (0)