Skip to content

Commit 82525d0

Browse files
committed
Add RawUniqueRc methods for sized values
1 parent 20e0aba commit 82525d0

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

‎library/alloc/src/raw_rc/raw_rc.rs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,48 @@ impl<T, A> RawRc<T, A> {
420420
unsafe { Self::from_raw_parts(ptr.as_ptr().cast(), alloc) }
421421
}
422422

423+
#[cfg(not(no_global_oom_handling))]
424+
unsafe fn new_cyclic_impl<F, R>(mut weak: RawWeak<T, A>, data_fn: F) -> Self
425+
where
426+
A: Allocator,
427+
F: FnOnce(&RawWeak<T, A>) -> T,
428+
R: RefCounter,
429+
{
430+
use crate::raw_rc::raw_unique_rc::RawUniqueRc;
431+
use crate::raw_rc::raw_weak::WeakGuard;
432+
433+
let guard = unsafe { WeakGuard::<T, A, R>::new(&mut weak) };
434+
let data = data_fn(&guard);
435+
436+
mem::forget(guard);
437+
438+
unsafe { RawUniqueRc::from_weak_with_value(weak, data).into_rc::<R>() }
439+
}
440+
441+
#[cfg(not(no_global_oom_handling))]
442+
pub(crate) unsafe fn new_cyclic<F, R>(data_fn: F) -> Self
443+
where
444+
A: Allocator + Default,
445+
F: FnOnce(&RawWeak<T, A>) -> T,
446+
R: RefCounter,
447+
{
448+
let weak = RawWeak::new_uninit::<0>();
449+
450+
unsafe { Self::new_cyclic_impl::<F, R>(weak, data_fn) }
451+
}
452+
453+
#[cfg(not(no_global_oom_handling))]
454+
pub(crate) unsafe fn new_cyclic_in<F, R>(data_fn: F, alloc: A) -> Self
455+
where
456+
A: Allocator,
457+
F: FnOnce(&RawWeak<T, A>) -> T,
458+
R: RefCounter,
459+
{
460+
let weak = RawWeak::new_uninit_in::<0>(alloc);
461+
462+
unsafe { Self::new_cyclic_impl::<F, R>(weak, data_fn) }
463+
}
464+
423465
pub(crate) unsafe fn into_inner<R>(self) -> Option<T>
424466
where
425467
A: Allocator,

‎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)