Skip to content

Commit b6b629e

Browse files
committed
Add RawUniqueRc methods for sized values
1 parent 663feac commit b6b629e

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

library/alloc/src/raw_rc/raw_rc.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use crate::boxed::Box;
2727
use crate::raw_rc::MakeMutStrategy;
2828
use crate::raw_rc::RefCounter;
2929
#[cfg(not(no_global_oom_handling))]
30+
use crate::raw_rc::raw_unique_rc::RawUniqueRc;
31+
#[cfg(not(no_global_oom_handling))]
3032
use crate::raw_rc::raw_weak;
3133
use crate::raw_rc::raw_weak::RawWeak;
3234
#[cfg(not(no_global_oom_handling))]
@@ -428,6 +430,45 @@ impl<T, A> RawRc<T, A> {
428430
unsafe { Self::from_raw_parts(ptr.as_ptr().cast(), alloc) }
429431
}
430432

433+
#[cfg(not(no_global_oom_handling))]
434+
unsafe fn new_cyclic_impl<F, R>(mut weak: RawWeak<T, A>, data_fn: F) -> Self
435+
where
436+
A: Allocator,
437+
F: FnOnce(&RawWeak<T, A>) -> T,
438+
R: RefCounter,
439+
{
440+
let guard = unsafe { raw_weak::new_weak_guard::<T, A, R>(&mut weak) };
441+
let data = data_fn(&guard);
442+
443+
mem::forget(guard);
444+
445+
unsafe { RawUniqueRc::from_weak_with_value(weak, data).into_rc::<R>() }
446+
}
447+
448+
#[cfg(not(no_global_oom_handling))]
449+
pub(crate) unsafe fn new_cyclic<F, R>(data_fn: F) -> Self
450+
where
451+
A: Allocator + Default,
452+
F: FnOnce(&RawWeak<T, A>) -> T,
453+
R: RefCounter,
454+
{
455+
let weak = RawWeak::new_uninit::<0>();
456+
457+
unsafe { Self::new_cyclic_impl::<F, R>(weak, data_fn) }
458+
}
459+
460+
#[cfg(not(no_global_oom_handling))]
461+
pub(crate) unsafe fn new_cyclic_in<F, R>(data_fn: F, alloc: A) -> Self
462+
where
463+
A: Allocator,
464+
F: FnOnce(&RawWeak<T, A>) -> T,
465+
R: RefCounter,
466+
{
467+
let weak = RawWeak::new_uninit_in::<0>(alloc);
468+
469+
unsafe { Self::new_cyclic_impl::<F, R>(weak, data_fn) }
470+
}
471+
431472
/// # Safety
432473
///
433474
/// All accesses to `self` must use the same `RefCounter` implementation for `R`.

library/alloc/src/raw_rc/raw_unique_rc.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,28 @@ where
7373
}
7474
}
7575
}
76+
77+
impl<T, A> RawUniqueRc<T, A> {
78+
#[cfg(not(no_global_oom_handling))]
79+
pub(super) unsafe fn from_weak_with_value(weak: RawWeak<T, A>, value: T) -> Self {
80+
unsafe { weak.as_ptr().write(value) };
81+
82+
Self { weak, _marker: PhantomData, _marker2: PhantomData }
83+
}
84+
85+
#[cfg(not(no_global_oom_handling))]
86+
pub(crate) fn new(value: T) -> Self
87+
where
88+
A: Allocator + Default,
89+
{
90+
unsafe { Self::from_weak_with_value(RawWeak::new_uninit::<0>(), value) }
91+
}
92+
93+
#[cfg(not(no_global_oom_handling))]
94+
pub(crate) fn new_in(value: T, alloc: A) -> Self
95+
where
96+
A: Allocator,
97+
{
98+
unsafe { Self::from_weak_with_value(RawWeak::new_uninit_in::<0>(alloc), value) }
99+
}
100+
}

0 commit comments

Comments
 (0)