@@ -83,12 +83,11 @@ pub fn RcAligned(comptime T: type, comptime alignment: ?u29) type {
83
83
return self .asUnmanaged ().release (self .alloc );
84
84
}
85
85
86
- /// Decrements the reference count, deallocating the weak count reaches zero,
87
- /// and executing `f` if the strong count reaches zero.
88
- /// The `f` function has a signature of `fn(*T, ...args)` or `fn(T, ...args)`.
89
- /// The continued use of the pointer after calling `release` is undefined behaviour.
90
- pub fn releaseWithFn (self : Self , comptime f : anytype , args : anytype ) void {
91
- return self .asUnmanaged ().releaseWithFn (self .alloc , f , args );
86
+ /// Decrements the reference count, deallocating if the weak count reaches zero,
87
+ /// and returning the underlying value if the strong count reaches zero.
88
+ /// The continued use of the pointer after calling this method is undefined behaviour.
89
+ pub fn releaseUnwrap (self : Self ) ? T {
90
+ return self .asUnmanaged ().releaseUnwrap (self .alloc );
92
91
}
93
92
94
93
/// Returns the inner value, if the `Rc` has exactly one strong reference.
@@ -99,6 +98,11 @@ pub fn RcAligned(comptime T: type, comptime alignment: ?u29) type {
99
98
return self .asUnmanaged ().tryUnwrap (self .alloc );
100
99
}
101
100
101
+ /// DEPRECATED: Use `releaseUnwrap` instead. Will be removed in the next major release.
102
+ pub fn releaseWithFn (self : Self , comptime f : anytype , args : anytype ) void {
103
+ self .asUnmanaged ().releaseWithFn (self .alloc , f , args );
104
+ }
105
+
102
106
/// Total size (in bytes) of the reference counted value on the heap.
103
107
/// This value accounts for the extra memory required to count the references.
104
108
pub fn innerSize () comptime_int {
@@ -289,12 +293,16 @@ pub fn ArcAligned(comptime T: type, comptime alignment: ?u29) type {
289
293
return self .asUnmanaged ().release (self .alloc );
290
294
}
291
295
292
- /// Decrements the reference count, deallocating the weak count reaches zero,
293
- /// and executing `f` if the strong count reaches zero.
294
- /// The `f` function has a signature of `fn(*T, ...args)` or `fn(T, ...args)`.
295
- /// The continued use of the pointer after calling `release` is undefined behaviour.
296
+ /// Decrements the reference count, deallocating if the weak count reaches zero,
297
+ /// and returning the underlying value if the strong count reaches zero.
298
+ /// The continued use of the pointer after calling this method is undefined behaviour.
299
+ pub fn releaseUnwrap (self : Self ) ? T {
300
+ return self .asUnmanaged ().releaseUnwrap (self .alloc );
301
+ }
302
+
303
+ /// DEPRECATED: Use `releaseUnwrap` instead. Will be removed in the next major release.
296
304
pub fn releaseWithFn (self : Self , comptime f : anytype , args : anytype ) void {
297
- return self .asUnmanaged ().releaseWithFn (self .alloc , f , args );
305
+ self .asUnmanaged ().releaseWithFn (self .alloc , f , args );
298
306
}
299
307
300
308
/// Returns the inner value, if the `Arc` has exactly one strong reference.
@@ -517,26 +525,28 @@ pub fn RcAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) type {
517
525
}
518
526
}
519
527
520
- /// Decrements the reference count, deallocating the weak count reaches zero,
521
- /// and executing `f` if the strong count reaches zero.
522
- /// The `f` function has a signature of `fn(*T, ...args)` or `fn(T, ...args)`.
523
- /// The continued use of the pointer after calling `release` is undefined behaviour.
524
- pub fn releaseWithFn (self : Self , allocator : Allocator , comptime f : anytype , args : anytype ) void {
528
+ /// Decrements the reference count, deallocating if the weak count reaches zero,
529
+ /// and returning the underlying value if the strong count reaches zero.
530
+ /// The continued use of the pointer after calling this method is undefined behaviour.
531
+ pub fn releaseUnwrap (self : Self , allocator : Allocator ) ? T {
525
532
const ptr = self .innerPtr ();
526
533
527
534
ptr .strong -= 1 ;
528
535
if (ptr .strong == 0 ) {
529
- if (comptime @typeInfo (@TypeOf (f )).Fn .params [0 ].type == T ) {
530
- @call (.auto , f , .{self .value .* } ++ args );
531
- } else {
532
- @call (.auto , f , .{self .value } ++ args );
533
- }
534
-
536
+ const value = self .value .* ;
535
537
ptr .weak -= 1 ;
536
538
if (ptr .weak == 0 ) {
537
539
allocator .destroy (ptr );
538
540
}
541
+ return value ;
539
542
}
543
+
544
+ return null ;
545
+ }
546
+
547
+ /// DEPRECATED: Use `releaseUnwrap` instead. Will be removed in the next major release.
548
+ pub fn releaseWithFn (_ : Self , _ : Allocator , comptime _ : anytype , _ : anytype ) void {
549
+ @compileError ("DEPRECATED: Use `releaseUnwrap` instead. Will be removed in the next major release." );
540
550
}
541
551
542
552
/// Returns the inner value, if the `Rc` has exactly one strong reference.
@@ -757,32 +767,31 @@ pub fn ArcAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) type {
757
767
/// The continued use of the pointer after calling `release` is undefined behaviour.
758
768
pub fn release (self : Self , allocator : Allocator ) void {
759
769
const ptr = self .innerPtr ();
760
-
761
770
if (@atomicRmw (usize , & ptr .strong , .Sub , 1 , .acq_rel ) == 1 ) {
762
771
if (@atomicRmw (usize , & ptr .weak , .Sub , 1 , .acq_rel ) == 1 ) {
763
772
allocator .destroy (ptr );
764
773
}
765
774
}
766
775
}
767
776
768
- /// Decrements the reference count, deallocating the weak count reaches zero,
769
- /// and executing `f` if the strong count reaches zero.
770
- /// The `f` function has a signature of `fn(*T, ...args)` or `fn(T, ...args)`.
771
- /// The continued use of the pointer after calling `release` is undefined behaviour.
772
- pub fn releaseWithFn (self : Self , allocator : Allocator , comptime f : anytype , args : anytype ) void {
777
+ /// Decrements the reference count, deallocating if the weak count reaches zero,
778
+ /// and returning the underlying value if the strong count reaches zero.
779
+ /// The continued use of the pointer after calling this method is undefined behaviour.
780
+ pub fn releaseUnwrap (self : Self , allocator : Allocator ) ? T {
773
781
const ptr = self .innerPtr ();
774
-
775
782
if (@atomicRmw (usize , & ptr .strong , .Sub , 1 , .acq_rel ) == 1 ) {
776
- if (comptime @typeInfo (@TypeOf (f )).Fn .params [0 ].type == T ) {
777
- @call (.auto , f , .{self .value .* } ++ args );
778
- } else {
779
- @call (.auto , f , .{self .value } ++ args );
780
- }
781
-
783
+ const value = self .value .* ;
782
784
if (@atomicRmw (usize , & ptr .weak , .Sub , 1 , .acq_rel ) == 1 ) {
783
785
allocator .destroy (ptr );
784
786
}
787
+ return value ;
785
788
}
789
+ return null ;
790
+ }
791
+
792
+ /// DEPRECATED: Use `releaseUnwrap` instead. Will be removed in the next major release.
793
+ pub fn releaseWithFn (_ : Self , _ : Allocator , comptime _ : anytype , _ : anytype ) void {
794
+ @compileError ("DEPRECATED: Use `releaseUnwrap` instead. Will be removed in the next major release." );
786
795
}
787
796
788
797
/// Returns the inner value, if the `Arc` has exactly one strong reference.
0 commit comments