Skip to content

Commit 17fab92

Browse files
committed
Add RawUniqueRc methods for sized values
1 parent 74f3386 commit 17fab92

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
@@ -29,6 +29,8 @@ use crate::boxed::Box;
2929
use crate::raw_rc::MakeMutStrategy;
3030
use crate::raw_rc::RefCounter;
3131
#[cfg(not(no_global_oom_handling))]
32+
use crate::raw_rc::raw_unique_rc::RawUniqueRc;
33+
#[cfg(not(no_global_oom_handling))]
3234
use crate::raw_rc::raw_weak;
3335
use crate::raw_rc::raw_weak::RawWeak;
3436
#[cfg(not(no_global_oom_handling))]
@@ -432,6 +434,45 @@ impl<T, A> RawRc<T, A> {
432434
unsafe { Self::from_raw_parts(ptr.as_ptr().cast(), alloc) }
433435
}
434436

437+
#[cfg(not(no_global_oom_handling))]
438+
unsafe fn new_cyclic_impl<F, R>(mut weak: RawWeak<T, A>, data_fn: F) -> Self
439+
where
440+
A: Allocator,
441+
F: FnOnce(&RawWeak<T, A>) -> T,
442+
R: RefCounter,
443+
{
444+
let guard = unsafe { raw_weak::new_weak_guard::<T, A, R>(&mut weak) };
445+
let data = data_fn(&guard);
446+
447+
mem::forget(guard);
448+
449+
unsafe { RawUniqueRc::from_weak_with_value(weak, data).into_rc::<R>() }
450+
}
451+
452+
#[cfg(not(no_global_oom_handling))]
453+
pub(crate) unsafe fn new_cyclic<F, R>(data_fn: F) -> Self
454+
where
455+
A: Allocator + Default,
456+
F: FnOnce(&RawWeak<T, A>) -> T,
457+
R: RefCounter,
458+
{
459+
let weak = RawWeak::new_uninit::<0>();
460+
461+
unsafe { Self::new_cyclic_impl::<F, R>(weak, data_fn) }
462+
}
463+
464+
#[cfg(not(no_global_oom_handling))]
465+
pub(crate) unsafe fn new_cyclic_in<F, R>(data_fn: F, alloc: A) -> Self
466+
where
467+
A: Allocator,
468+
F: FnOnce(&RawWeak<T, A>) -> T,
469+
R: RefCounter,
470+
{
471+
let weak = RawWeak::new_uninit_in::<0>(alloc);
472+
473+
unsafe { Self::new_cyclic_impl::<F, R>(weak, data_fn) }
474+
}
475+
435476
/// # Safety
436477
///
437478
/// 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)