Refactor deinitIfLive and deinitIfExists: improve return type handling and pointer validation; comment out unused build options

This commit is contained in:
2026-08-03 12:38:54 +02:00
parent 0a7ad2662c
commit f2904076f3
2 changed files with 26 additions and 27 deletions

View File

@@ -3,7 +3,7 @@ const std = @import("std");
pub fn build(b: *std.Build) void pub fn build(b: *std.Build) void
{ {
const target = b.standardTargetOptions(.{ const target = b.standardTargetOptions(.{
.default_target = .{ .abi = .musl }, // .default_target = .{ .abi = .musl },
}); });
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
@@ -46,7 +46,7 @@ pub fn build(b: *std.Build) void
}), }),
}); });
exe.root_module.link_libc = true; // exe.root_module.link_libc = true;
exe.root_module.addOptions("build_options", options); exe.root_module.addOptions("build_options", options);
// //

View File

@@ -9,19 +9,8 @@ const std = @import("std");
/// to deinitialize the same resource again. /// to deinitialize the same resource again.
/// ///
/// The return value of `resource.deinit()` is returned from the function. /// The return value of `resource.deinit()` is returned from the function.
pub fn deinitIfLive(resource: anytype, live: *bool) ?@TypeOf(resource.deinit()) pub fn deinitIfLive(resource: anytype, live: *bool) ?LiveDeinitReturn(@TypeOf(resource))
{ {
comptime
{
const Pointer = @TypeOf(resource);
if (@typeInfo(Pointer) != .pointer)
@compileError("deinitIfLive expects a pointer!");
if (@typeInfo(Pointer).pointer.size != .one)
@compileError("deinitIfLive expects a single-item pointer!");
}
if (!live.*) return null; if (!live.*) return null;
live.* = false; live.* = false;
@@ -40,7 +29,7 @@ pub fn deinitIfLive(resource: anytype, live: *bool) ?@TypeOf(resource.deinit())
/// ///
/// The return value of `resource.*.?.deinit()` is returned /// The return value of `resource.*.?.deinit()` is returned
/// from the function. /// from the function.
pub fn deinitIfExists(resource: anytype) ?DeinitReturn(@TypeOf(resource)) pub fn deinitIfExists(resource: anytype) ?ExistsDeinitReturn(@TypeOf(resource))
{ {
if (resource.*) |*payload| if (resource.*) |*payload|
{ {
@@ -54,26 +43,36 @@ pub fn deinitIfExists(resource: anytype) ?DeinitReturn(@TypeOf(resource))
// //
fn DeinitReturn(comptime Pointer: type) type {
return @typeInfo(@TypeOf(Payload(Pointer).deinit)).@"fn".return_type.?;
}
fn Payload(comptime Pointer: type) type fn Payload(comptime Pointer: type) type
{ {
if (@typeInfo(Pointer) != .pointer) const Child = ValidatedChild(Pointer);
@compileError("deinitIfExists expects a pointer!");
if (@typeInfo(Pointer).pointer.size != .one)
@compileError("deinitIfExists expects a single-item pointer!");
const Child = @typeInfo(Pointer).pointer.child;
if (@typeInfo(Child) != .optional) if (@typeInfo(Child) != .optional)
@compileError("deinitIfExists expects a pointer to an optional resource (*?T)!"); @compileError("Expected a pointer to an optional resource (*?T)!");
return @typeInfo(Child).optional.child; return @typeInfo(Child).optional.child;
} }
fn ValidatedChild(comptime Pointer: type) type
{
if (@typeInfo(Pointer) != .pointer)
@compileError("Expected a pointer!");
if (@typeInfo(Pointer).pointer.size != .one)
@compileError("Expected a single-item pointer!");
return @typeInfo(Pointer).pointer.child;
}
fn LiveDeinitReturn(comptime Pointer: type) type {
const Child = ValidatedChild(Pointer);
return @typeInfo(@TypeOf(Child.deinit)).@"fn".return_type.?;
}
fn ExistsDeinitReturn(comptime Pointer: type) type {
return @typeInfo(@TypeOf(Payload(Pointer).deinit)).@"fn".return_type.?;
}
// //
test "deinitIfLive deinitializes an ArenaAllocator once" { test "deinitIfLive deinitializes an ArenaAllocator once" {