Skip to content

Instantly share code, notes, and snippets.

@suirad

suirad/thing.rs Secret

Last active November 25, 2018 09:16
Show Gist options
  • Save suirad/7ff1a942a2ff1f0cef846d67fb4a7d9c to your computer and use it in GitHub Desktop.
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
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