Skip to content

Instantly share code, notes, and snippets.

@jeremytregunna
Created August 27, 2025 03:05
Show Gist options
  • Select an option

  • Save jeremytregunna/8ba4068fcf34b85e0f93a3c360c278f3 to your computer and use it in GitHub Desktop.

Select an option

Save jeremytregunna/8ba4068fcf34b85e0f93a3c360c278f3 to your computer and use it in GitHub Desktop.
const std = @import("std");
const PrivateImpl = struct {
secret: i32,
};
pub const MyStruct = struct {
public_field: u32,
private_data: *anyopaque,
allocator: std.mem.Allocator,
pub fn init(allocator: std.mem.Allocator, public_val: u32, secret: i32) !MyStruct {
const private = try allocator.create(PrivateImpl);
private.* = PrivateImpl{ .secret = secret };
return MyStruct{
.public_field = public_val,
.private_data = private,
.allocator = allocator,
};
}
pub fn deinit(self: *MyStruct) void {
const private: *PrivateImpl = @ptrCast(@alignCast(self.private_data));
self.allocator.destroy(private);
}
pub fn getSecret(self: *const MyStruct) i32 {
const private: *const PrivateImpl = @ptrCast(@alignCast(self.private_data));
return private.secret;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment