Refactor deinitIfLive: adjust return type and parameter naming; update allocator usage in main.zig
This commit is contained in:
@@ -1,27 +1,27 @@
|
||||
const std = @import("std");
|
||||
|
||||
/// Deinitializes `allocator` only if it is still live.
|
||||
/// Deinitializes `resource` only if it is still live.
|
||||
///
|
||||
/// Pass a pointer to the original allocator owner, such as an
|
||||
/// `ArenaAllocator` or `DebugAllocator`.
|
||||
/// Pass a pointer to the original allocator owner,
|
||||
/// such as an `ArenaAllocator`.
|
||||
///
|
||||
/// If `live` is `true`, calls `allocator.deinit()` and then sets
|
||||
/// `live` to `false`, preventing a later cleanup path from attempting
|
||||
/// to deinitialize the same allocator again.
|
||||
///
|
||||
/// The return value of `deinit()` is discarded.
|
||||
pub fn deinitIfLive(allocator: anytype, live: *bool) void
|
||||
/// The return value of `deinit()` is returned from the function.
|
||||
pub fn deinitIfLive(resource: anytype, live: *bool) ?@TypeOf(resource.deinit())
|
||||
{
|
||||
comptime
|
||||
{
|
||||
const T = @TypeOf(allocator);
|
||||
const T = @TypeOf(resource);
|
||||
|
||||
if (@typeInfo(T) != .pointer) @compileError("deinitIfLive expects a pointer!");
|
||||
if (T == std.mem.Allocator) @compileError("deinitIfLive expects a pointer to an allocator owner, not std.mem.Allocator!");
|
||||
}
|
||||
|
||||
if (live.*) {
|
||||
_ = allocator.deinit();
|
||||
live.* = false;
|
||||
}
|
||||
if (!live.*) return null;
|
||||
|
||||
live.* = false;
|
||||
return resource.deinit();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user