Add new functionality

This commit is contained in:
Lyssieth 2024-12-06 08:28:27 +02:00
parent 1d8259b88b
commit 519c09aef6
Signed by untrusted user who does not match committer: lyssieth
GPG key ID: 200268854934CFAB
3 changed files with 47 additions and 0 deletions

View file

@ -2,6 +2,7 @@ const std = @import("std");
pub const args = @import("./args/args.zig");
pub const log = @import("./log/logging.zig");
pub const util = @import("./util/utils.zig");
comptime {
// A hack to prevent the compiler from optimizing tests and "exports" away.
@ -16,5 +17,7 @@ comptime {
std.mem.doNotOptimizeAway(log.init);
std.mem.doNotOptimizeAway(log.deinit);
std.mem.doNotOptimizeAway(log.logFn);
std.mem.doNotOptimizeAway(util);
}
}

View file

@ -0,0 +1,43 @@
const std = @import("std");
pub const TrackedString = struct {
data: []const u8,
kind: AllocKind,
pub fn initAlloc(value: []const u8, allocator: std.mem.Allocator) !TrackedString {
return .{
.data = try allocator.dupe(value),
.kind = .{ .Allocated = allocator },
};
}
pub fn initConst(comptime value: []const u8) TrackedString {
return .{
.data = value,
.kind = .{ .Constant = {} },
};
}
pub fn deinit(self: TrackedString) void {
switch (self.kind) {
.Constant => {
self.value = undefined;
self.kind = .{ .Dead = {} };
},
.Allocated => |allocator| {
allocator.free(self.value);
self.value = undefined;
self.kind = .{ .Dead = {} };
},
.Dead => {},
}
}
};
pub const AllocKind = union(enum) {
Constant: void,
Allocated: std.mem.Allocator,
Dead: void,
};

1
src/util/utils.zig Normal file
View file

@ -0,0 +1 @@
pub const str = @import("trackedString.zig");