-
-
Save suirad/7ff1a942a2ff1f0cef846d67fb4a7d9c to your computer and use it in GitHub Desktop.
Attempting to make something similiar to a unique_ptr that causes a compile error if a deinit isnt handled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
pub fn main() void { | |
var a = Uniq(u32).init(1); | |
a.wrapped = 2; | |
a.used(); | |
a.deinit(); | |
var b = Uniq(u32).init(2); | |
//b.used(); | |
b.deinit(); | |
} | |
inline fn Uniq(comptime T: type) type { | |
comptime var used = false; | |
return struct { | |
wrapped: T, | |
const Self = @This(); | |
pub inline fn init(val: T) Self { | |
var self = Self{.wrapped = val}; | |
return self; | |
} | |
pub inline fn used(self: *Self) void { | |
comptime {used = true;} | |
} | |
pub inline fn deinit(self: *Self) void { | |
comptime {if (!used){@compileError("Did not deinit");}} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment