Skip to content

Commit 684fa38

Browse files
author
Aandreba
committed
deprecate releaseWithFn in favour of releaseUnwrap
1 parent a92ce55 commit 684fa38

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

src/root.zig

+44-35
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,11 @@ pub fn RcAligned(comptime T: type, comptime alignment: ?u29) type {
8383
return self.asUnmanaged().release(self.alloc);
8484
}
8585

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);
9291
}
9392

9493
/// 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 {
9998
return self.asUnmanaged().tryUnwrap(self.alloc);
10099
}
101100

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+
102106
/// Total size (in bytes) of the reference counted value on the heap.
103107
/// This value accounts for the extra memory required to count the references.
104108
pub fn innerSize() comptime_int {
@@ -289,12 +293,16 @@ pub fn ArcAligned(comptime T: type, comptime alignment: ?u29) type {
289293
return self.asUnmanaged().release(self.alloc);
290294
}
291295

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.
296304
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);
298306
}
299307

300308
/// 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 {
517525
}
518526
}
519527

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 {
525532
const ptr = self.innerPtr();
526533

527534
ptr.strong -= 1;
528535
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.*;
535537
ptr.weak -= 1;
536538
if (ptr.weak == 0) {
537539
allocator.destroy(ptr);
538540
}
541+
return value;
539542
}
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.");
540550
}
541551

542552
/// 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 {
757767
/// The continued use of the pointer after calling `release` is undefined behaviour.
758768
pub fn release(self: Self, allocator: Allocator) void {
759769
const ptr = self.innerPtr();
760-
761770
if (@atomicRmw(usize, &ptr.strong, .Sub, 1, .acq_rel) == 1) {
762771
if (@atomicRmw(usize, &ptr.weak, .Sub, 1, .acq_rel) == 1) {
763772
allocator.destroy(ptr);
764773
}
765774
}
766775
}
767776

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 {
773781
const ptr = self.innerPtr();
774-
775782
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.*;
782784
if (@atomicRmw(usize, &ptr.weak, .Sub, 1, .acq_rel) == 1) {
783785
allocator.destroy(ptr);
784786
}
787+
return value;
785788
}
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.");
786795
}
787796

788797
/// Returns the inner value, if the `Arc` has exactly one strong reference.

src/tests.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ test "cyclic" {
8282
};
8383

8484
var gadget = try Gadget.init(alloc);
85-
defer gadget.releaseWithFn(Gadget.deinit, .{});
85+
defer if (gadget.releaseUnwrap()) |val| val.deinit();
8686

8787
try expect(gadget.strongCount() == 1);
8888
try expect(gadget.weakCount() == 1);
@@ -166,7 +166,7 @@ test "cyclic atomic" {
166166
};
167167

168168
var gadget = try Gadget.init(alloc);
169-
defer gadget.releaseWithFn(Gadget.deinit, .{});
169+
defer if (gadget.releaseUnwrap()) |val| val.deinit();
170170

171171
try expect(gadget.strongCount() == 1);
172172
try expect(gadget.weakCount() == 1);

0 commit comments

Comments
 (0)