Image missing.
Resizable structs in Zig

rvrb

created: July 26, 2025, 9:43 p.m. | updated: July 27, 2025, 5:39 p.m.

Let’s break down the three tools I outlined above for contiguous storage:Tool Elements Size Arrays Same Comptime Many-item pointers Same Runtime Structs Different Comptime ? We’d use it like this:const Connection = ResizableStruct ( struct { client : Client , host : ResizableArray (u8) read_buffer : ResizableArray (u8), write_buffer : ResizableArray (u8) }); const conn = try Connection. { .host = input.host_len, .read_buffer = input.read_buffer_len, .write_buffer = input.write_buffer_len, }); defer conn. deinit (allocator); const client = conn. get (.client); client . * = input.client; const host = conn. get (.host); @memcpy (host, input.host); const reader = stream. Let’s take a look at the current implementation of the get method:pub fn get ( self : Self , comptime field : FieldEnum ( Layout )) blk: { const Field = @FieldType (Layout, @tagName (field)); break :blk if ( isResizableArray (Field)) []Field.Element else * Field; } { const offset = offsetOf (self.lens, @tagName (field)); const size = sizeOf (self.lens, @tagName (field)); const bytes = self.ptr[offset .. ][0 .. size]; return @ptrCast ( @alignCast (bytes)); }Look familiar?

5 days, 8 hours ago: Hacker News: Front Page