@@ -29,6 +29,8 @@ use crate::boxed::Box;
29
29
use crate :: raw_rc:: MakeMutStrategy ;
30
30
use crate :: raw_rc:: RefCounter ;
31
31
#[ cfg( not( no_global_oom_handling) ) ]
32
+ use crate :: raw_rc:: raw_unique_rc:: RawUniqueRc ;
33
+ #[ cfg( not( no_global_oom_handling) ) ]
32
34
use crate :: raw_rc:: raw_weak;
33
35
use crate :: raw_rc:: raw_weak:: RawWeak ;
34
36
#[ cfg( not( no_global_oom_handling) ) ]
@@ -432,6 +434,45 @@ impl<T, A> RawRc<T, A> {
432
434
unsafe { Self :: from_raw_parts ( ptr. as_ptr ( ) . cast ( ) , alloc) }
433
435
}
434
436
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
+
435
476
/// # Safety
436
477
///
437
478
/// All accesses to `self` must use the same `RefCounter` implementation for `R`.
0 commit comments