Created
August 27, 2025 03:05
-
-
Save jeremytregunna/8ba4068fcf34b85e0f93a3c360c278f3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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"); | |
| 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