@@ -31,6 +31,8 @@ use crate::boxed::Box;
3131use crate :: raw_rc:: MakeMutStrategy ;
3232use 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) ) ]
3436use crate :: raw_rc:: raw_weak;
3537use 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 >
0 commit comments