Skip to content

Commit ea0ae04

Browse files
committed
Add RawUniqueRc methods for sized values
1 parent 8c293c1 commit ea0ae04

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
@@ -31,6 +31,8 @@ use crate::boxed::Box;
3131
use crate::raw_rc::MakeMutStrategy;
3232
use crate::raw_rc::RefCounter;
3333
#[cfg(not(no_global_oom_handling))]
34+
use crate::raw_rc::raw_unique_rc::RawUniqueRc;
35+
#[cfg(not(no_global_oom_handling))]
3436
use crate::raw_rc::raw_weak;
3537
use crate::raw_rc::raw_weak::RawWeak;
3638
#[cfg(not(no_global_oom_handling))]
@@ -410,6 +412,45 @@ impl<T, A> RawRc<T, A> {
410412
unsafe { Self::from_raw_parts(ptr.as_ptr().cast(), alloc) }
411413
}
412414

415+
#[cfg(not(no_global_oom_handling))]
416+
unsafe fn new_cyclic_impl<F, R>(mut weak: RawWeak<T, A>, data_fn: F) -> Self
417+
where
418+
A: Allocator,
419+
F: FnOnce(&RawWeak<T, A>) -> T,
420+
R: RefCounter,
421+
{
422+
let guard = unsafe { raw_weak::new_weak_guard::<T, A, R>(&mut weak) };
423+
let data = data_fn(&guard);
424+
425+
mem::forget(guard);
426+
427+
unsafe { RawUniqueRc::from_weak_with_value(weak, data).into_rc::<R>() }
428+
}
429+
430+
#[cfg(not(no_global_oom_handling))]
431+
pub(crate) unsafe fn new_cyclic<F, R>(data_fn: F) -> Self
432+
where
433+
A: Allocator + Default,
434+
F: FnOnce(&RawWeak<T, A>) -> T,
435+
R: RefCounter,
436+
{
437+
let weak = RawWeak::new_uninit::<0>();
438+
439+
unsafe { Self::new_cyclic_impl::<F, R>(weak, data_fn) }
440+
}
441+
442+
#[cfg(not(no_global_oom_handling))]
443+
pub(crate) unsafe fn new_cyclic_in<F, R>(data_fn: F, alloc: A) -> Self
444+
where
445+
A: Allocator,
446+
F: FnOnce(&RawWeak<T, A>) -> T,
447+
R: RefCounter,
448+
{
449+
let weak = RawWeak::new_uninit_in::<0>(alloc);
450+
451+
unsafe { Self::new_cyclic_impl::<F, R>(weak, data_fn) }
452+
}
453+
413454
/// Maps the value in an `RawRc`, reusing the allocation if possible.
414455
#[cfg(not(no_global_oom_handling))]
415456
pub(crate) fn map<R, U>(self, f: impl FnOnce(&T) -> U) -> RawRc<U, A>

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)