@@ -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) ) ]
@@ -430,6 +432,45 @@ impl<T, A> RawRc<T, A> {
430
432
unsafe { Self :: from_raw_parts ( ptr. as_ptr ( ) . cast ( ) , alloc) }
431
433
}
432
434
435
+ #[ cfg( not( no_global_oom_handling) ) ]
436
+ unsafe fn new_cyclic_impl < F , R > ( mut weak : RawWeak < T , A > , data_fn : F ) -> Self
437
+ where
438
+ A : Allocator ,
439
+ F : FnOnce ( & RawWeak < T , A > ) -> T ,
440
+ R : RefCounter ,
441
+ {
442
+ let guard = unsafe { raw_weak:: new_weak_guard :: < T , A , R > ( & mut weak) } ;
443
+ let data = data_fn ( & guard) ;
444
+
445
+ mem:: forget ( guard) ;
446
+
447
+ unsafe { RawUniqueRc :: from_weak_with_value ( weak, data) . into_rc :: < R > ( ) }
448
+ }
449
+
450
+ #[ cfg( not( no_global_oom_handling) ) ]
451
+ pub ( crate ) unsafe fn new_cyclic < F , R > ( data_fn : F ) -> Self
452
+ where
453
+ A : Allocator + Default ,
454
+ F : FnOnce ( & RawWeak < T , A > ) -> T ,
455
+ R : RefCounter ,
456
+ {
457
+ let weak = RawWeak :: new_uninit :: < 0 > ( ) ;
458
+
459
+ unsafe { Self :: new_cyclic_impl :: < F , R > ( weak, data_fn) }
460
+ }
461
+
462
+ #[ cfg( not( no_global_oom_handling) ) ]
463
+ pub ( crate ) unsafe fn new_cyclic_in < F , R > ( data_fn : F , alloc : A ) -> Self
464
+ where
465
+ A : Allocator ,
466
+ F : FnOnce ( & RawWeak < T , A > ) -> T ,
467
+ R : RefCounter ,
468
+ {
469
+ let weak = RawWeak :: new_uninit_in :: < 0 > ( alloc) ;
470
+
471
+ unsafe { Self :: new_cyclic_impl :: < F , R > ( weak, data_fn) }
472
+ }
473
+
433
474
/// # Safety
434
475
///
435
476
/// All accesses to `self` must use the same `RefCounter` implementation for `R`.
0 commit comments