Refactor deinitIfLive: adjust return type and parameter naming; update allocator usage in main.zig

This commit is contained in:
2026-08-02 23:13:19 +02:00
parent d70a051a57
commit 88d312e713
2 changed files with 17 additions and 23 deletions

View File

@@ -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();
}