Add new functionality
This commit is contained in:
parent
1d8259b88b
commit
519c09aef6
3 changed files with 47 additions and 0 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
43
src/util/trackedString.zig
Normal file
43
src/util/trackedString.zig
Normal 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
1
src/util/utils.zig
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub const str = @import("trackedString.zig");
|
||||
Loading…
Reference in a new issue