ziglang/zig
147
2.9k
Fork
You've already forked zig
271

add FlexibleStruct/FlexibleArray for runtime sized data structures #30823

Open
tristanpemble wants to merge 3 commits from tristanpemble/zig:flexible_struct into master
pull from: tristanpemble/zig:flexible_struct
merge into: ziglang:master
ziglang:master
ziglang:one-mutex-per-mutex
ziglang:mmap
ziglang:riscv-ci
ziglang:test-no-bin
ziglang:poll
ziglang:io-uring-update
ziglang:llvm22
ziglang:poll-ring
ziglang:debug-file-leaks-differently
ziglang:debug-file-leaks
ziglang:hate-letter-to-std.os
ziglang:i-am-a-foolish-fool
ziglang:ProcessPrng
ziglang:elfv2-dyn
ziglang:jobserver
ziglang:threadtheft
ziglang:io-threaded-no-queue
ziglang:0.15.x
ziglang:Io.net
ziglang:comptime-allocator
ziglang:restricted-function-pointers
ziglang:cli
ziglang:wasm-linker-writer
ziglang:wrangle-writer-buffering
ziglang:sha1-stream
ziglang:async-await-demo
ziglang:fixes
ziglang:0.14.x
ziglang:ast-node-methods
ziglang:spork8
ziglang:macos-debug-info
ziglang:make-vs-configure
ziglang:fuzz-macos
ziglang:main
ziglang:sans-aro
ziglang:ArrayList-reserve
ziglang:incr-bug
ziglang:llvm-ir-nosanitize-metadata
ziglang:ci-tarballs
ziglang:ci-scripts
ziglang:threadpool
ziglang:0.12.x
ziglang:new-pkg-hash
ziglang:json-diagnostics
ziglang:more-doctests
ziglang:rework-comptime-mutation
ziglang:0.11.x
ziglang:ci-perf-comment
ziglang:stage2-async
ziglang:0.10.x
ziglang:autofix
ziglang:0.9.x
ziglang:aro
ziglang:hcs
ziglang:0.8.x
ziglang:0.7.x
First-time contributor
Copy link

this is the result of my blog post titled Resizable structs in Zig. as it were, these structs are not resizable.

I'm told there's several places in the standard library where a helper utility like this would make the code easier to maintain, so I am contributing this upstream.

Creating a flexible struct

Declare a type using FlexibleStruct by passing in a struct that has fields of type FlexibleArray:

constPacket=FlexibleStruct(struct{host_len:usize,host:FlexibleArray(u8,.host_len),buf_lens:u32,read_buf:FlexibleArray(u8,.buf_lens),write_buf:FlexibleArray(u8,.buf_lens),});

the FlexibleArray fields are zero-sized markers. they accept an element type and an enum literal pointing toward the field of the parent struct that contains its length. the type of that length field can be any integer type.

length fields can also be shared between flexible array members.

there are two ways to work with this meta type: via an allocation on the heap, or via a stack held buffer.

Allocation

There is a create/destroy method pair that allocates a buffer on the heap, casts its pointer to *YourType, and sets the length fields:

constpacket:*Packet=try.create(testing.allocator,.{.host_len=host.len,.buf_lens=10,});deferpacket.destroy(testing.allocator);

Stack Buffer

You can either just use a [some_size]u8 buf, or use the helper type that sizes an array such that the buffer can hold the desired capacity:

varbuf:Packet.Buf(.{.host_len=1024,.buf_lens=128,})=undefined;constpacket:*Packet=.initBuffer(&buf,.{.host_len=host.len,.buf_lens=10,});

Using a flexible struct

Once you have one, whether on the stack or heap, the API is quite minimal:

  • ptr(field_tag) returns a pointer to a field
  • len(field_tag) returns the array length of a field
  • slice(field_tag) returns the slice of that field

Notes

Alignment

I've done my best to make sure fields/allocations are properly aligned, but might have missed something.

Layout

The underlying layout of the buffer is well defined when you provide a layout struct that is well defined (it is extern). When you provide an auto layout struct, the layout is not well defined (they are sorted by alignment).

Concretely, this is demonstrated in the test case:

test"layout"{constwell_defined=FlexibleStruct(externstruct{len:u8,arr:FlexibleArray(u64,.len),});constnot_well_defined=FlexibleStruct(struct{len:u8,arr:FlexibleArray(u64,.len),});trytesting.expectEqual(16,well_defined.calcSize(.{.len=1}));trytesting.expectEqual(9,not_well_defined.calcSize(.{.len=1}));}
this is the result of my blog post titled [Resizable structs in Zig](https://tristanpemble.com/resizable-structs-in-zig/). as it were, these structs are not resizable. I'm told there's several places in the standard library where a helper utility like this would make the code easier to maintain, so I am contributing this upstream. ## Creating a flexible struct Declare a type using `FlexibleStruct` by passing in a struct that has fields of type `FlexibleArray`: ```zig const Packet = FlexibleStruct(struct { host_len: usize, host: FlexibleArray(u8, .host_len), buf_lens: u32, read_buf: FlexibleArray(u8, .buf_lens), write_buf: FlexibleArray(u8, .buf_lens), }); ``` the `FlexibleArray` fields are zero-sized markers. they accept an element type and an enum literal pointing toward the field of the parent struct that contains its length. the type of that length field can be any integer type. length fields can also be shared between flexible array members. there are two ways to work with this meta type: via an allocation on the heap, or via a stack held buffer. ### Allocation There is a `create`/`destroy` method pair that allocates a buffer on the heap, casts its pointer to `*YourType`, and sets the length fields: ```zig const packet: *Packet = try .create(testing.allocator, .{ .host_len = host.len, .buf_lens = 10, }); defer packet.destroy(testing.allocator); ``` ### Stack Buffer You can either just use a `[some_size]u8` buf, or use the helper type that sizes an array such that the buffer can hold the desired capacity: ```zig var buf: Packet.Buf(.{ .host_len = 1024, .buf_lens = 128, }) = undefined; const packet: *Packet = .initBuffer(&buf, .{ .host_len = host.len, .buf_lens = 10, }); ``` ### Using a flexible struct Once you have one, whether on the stack or heap, the API is quite minimal: - `ptr(field_tag)` returns a pointer to a field - `len(field_tag)` returns the array length of a field - `slice(field_tag)` returns the slice of that field ## Notes ### Alignment I've done my best to make sure fields/allocations are properly aligned, but might have missed something. ### Layout The underlying layout of the buffer is well defined when you provide a layout struct that is well defined (it is extern). When you provide an auto layout struct, the layout is not well defined (they are sorted by alignment). Concretely, this is demonstrated in the test case: ```zig test "layout" { const well_defined = FlexibleStruct(extern struct { len: u8, arr: FlexibleArray(u64, .len), }); const not_well_defined = FlexibleStruct(struct { len: u8, arr: FlexibleArray(u64, .len), }); try testing.expectEqual(16, well_defined.calcSize(.{ .len = 1 })); try testing.expectEqual(9, not_well_defined.calcSize(.{ .len = 1 })); } ```
add FlexibleStruct/FlexibleArray for runtime sized data structures
Some checks failed
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
d36dee31c3
tristanpemble force-pushed flexible_struct from d36dee31c3
Some checks failed
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
to 6617105fad
Some checks failed
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
2026年01月14日 01:58:33 +01:00
Compare
tristanpemble force-pushed flexible_struct from 6617105fad
Some checks failed
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
to a5fdc7f5c2
Some checks failed
ci / x86_64-freebsd-release (pull_request) Successful in 33m1s
ci / aarch64-macos-release (pull_request) Successful in 38m15s
ci / x86_64-windows-release (pull_request) Successful in 42m9s
ci / x86_64-freebsd-debug (pull_request) Successful in 44m42s
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
2026年01月14日 02:00:34 +01:00
Compare
andrewrk left a comment
Owner
Copy link

Thanks for sending this!

I'm requesting changes but, recognizing that I asked for this code, so don't feel compelled to work on this any more than you want to - I'm happy to take responsibility for seeing this landed.

Thanks for sending this! I'm requesting changes but, recognizing that I asked for this code, so don't feel compelled to work on this any more than you want to - I'm happy to take responsibility for seeing this landed.
@ -0,0 +1,325 @@
///A`FlexibleStruct`isadatastructurewhereoneormorefieldsareruntimesizedarrays.
///
///Example:
Owner
Copy link

Pro tip: if you move your example code from doc comment to test FlexibleStruct { ... } it will show up in autodocs as "example code". You should be able to preview this with zig std locally. also you can preview

Pro tip: if you move your example code from doc comment to `test FlexibleStruct { ... }` it will show up in autodocs as "example code". You should be able to preview this with `zig std` locally. also you can preview
Author
First-time contributor
Copy link

cool! never knew how to do this. I updated the Buf test as an example as well

cool! never knew how to do this. I updated the Buf test as an example as well
tristanpemble marked this conversation as resolved
@ -0,0 +82,4 @@
constaligned=std.mem.alignInBytes(buf,alignment.toByteUnits()).?;
constbytes=aligned[0..size];
constself:*@This()=@ptrFromInt(@intFromPtr(bytes.ptr));
Owner
Copy link

In general, @alignCast(@ptrCast(...)) is a much more preferable operation than @ptrFromInt(@intFromPtr(...)):

  • safer
  • less opaque to the optimizer
  • does not prevent comptime usage

@ptrFromInt should almost never be used outside of embedded programming (i.e. address 0x1234 is how you access a sensor or something like that).

In general, `@alignCast(@ptrCast(...))` is a much more preferable operation than `@ptrFromInt(@intFromPtr(...))`: * safer * less opaque to the optimizer * does not prevent comptime usage `@ptrFromInt` should almost never be used outside of embedded programming (i.e. address 0x1234 is how you access a sensor or something like that).
tristanpemble marked this conversation as resolved
tristanpemble force-pushed flexible_struct from a5fdc7f5c2
Some checks failed
ci / x86_64-freebsd-release (pull_request) Successful in 33m1s
ci / aarch64-macos-release (pull_request) Successful in 38m15s
ci / x86_64-windows-release (pull_request) Successful in 42m9s
ci / x86_64-freebsd-debug (pull_request) Successful in 44m42s
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
to 8936101c5b
Some checks failed
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
2026年01月14日 02:46:33 +01:00
Compare
fix pointer casting
Some checks failed
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
66d1f3d752
tristanpemble force-pushed flexible_struct from 66d1f3d752
Some checks failed
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
to cc6cc68800
Some checks failed
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
2026年01月14日 02:49:01 +01:00
Compare
Author
First-time contributor
Copy link

@andrewrk no problem, updated with those fixes. while I have you here, do you know why I always have to use if (comptime ..) syntax? conditions never seem to know when they are comptime evaluable

@andrewrk no problem, updated with those fixes. while I have you here, do you know why I always have to use `if (comptime ..)` syntax? conditions never seem to know when they are comptime evaluable
First-time contributor
Copy link

If you mark the function inline, you won't need the comptime prefix at the call site. This is one of the times when inline is fine and doesn't effect the optimizer since this is a comptime only function.

If you mark the function inline, you won't need the comptime prefix at the call site. This is one of the times when inline is fine and doesn't effect the optimizer since this is a comptime only function.
adjust test for portability
All checks were successful
ci / aarch64-macos-release (pull_request) Successful in 27m49s
ci / x86_64-freebsd-release (pull_request) Successful in 34m13s
ci / x86_64-windows-release (pull_request) Successful in 42m8s
ci / x86_64-freebsd-debug (pull_request) Successful in 43m13s
ci / x86_64-linux-debug (pull_request) Successful in 48m9s
ci / x86_64-openbsd-release (pull_request) Successful in 49m27s
ci / x86_64-windows-debug (pull_request) Successful in 53m4s
ci / aarch64-macos-debug (pull_request) Successful in 58m11s
ci / x86_64-openbsd-debug (pull_request) Successful in 58m0s
ci / aarch64-linux-release (pull_request) Successful in 1h34m44s
ci / x86_64-linux-release (pull_request) Successful in 2h12m10s
ci / aarch64-linux-debug (pull_request) Successful in 2h17m35s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h35m47s
ci / powerpc64le-linux-release (pull_request) Successful in 1h42m51s
ci / powerpc64le-linux-debug (pull_request) Successful in 3h43m38s
ci / s390x-linux-debug (pull_request) Successful in 3h35m23s
ci / s390x-linux-release (pull_request) Successful in 2h47m3s
ci / loongarch64-linux-release (pull_request) Successful in 2h29m9s
ci / loongarch64-linux-debug (pull_request) Successful in 2h51m10s
492099b7b8
@ -71,6 +71,7 @@ pub const debug = @import("debug.zig");
pubconstdwarf=@import("dwarf.zig");
pubconstelf=@import("elf.zig");
pubconstenums=@import("enums.zig");
pubconstflexible_struct=@import("flexible_struct.zig");
Contributor
Copy link

bikeshed but where it's only two, i'd recommend std.FlexibleStruct/std.FlexibleArray or std.flexible.{Struct,Array}

bikeshed but where it's only two, i'd recommend `std.FlexibleStruct`/`std.FlexibleArray` or `std.flexible.{Struct,Array}`
Author
First-time contributor
Copy link

@andrewrk I just wanted to get this into a working/passing state before handing it off. looks like tests pass now. feel free to commandeer and make it your own. ping me when you've integrated into your work, I would love to see it in action!

@andrewrk I just wanted to get this into a working/passing state before handing it off. looks like tests pass now. feel free to commandeer and make it your own. ping me when you've integrated into your work, I would love to see it in action!
Author
First-time contributor
Copy link

it has occurred to me that initBuffer might be better renamed createBuffer, given it returns a pointer -- congruent with create/destroy

it has occurred to me that `initBuffer` might be better renamed `createBuffer`, given it returns a pointer -- congruent with create/destroy
All checks were successful
ci / aarch64-macos-release (pull_request) Successful in 27m49s
Required
Details
ci / x86_64-freebsd-release (pull_request) Successful in 34m13s
Required
Details
ci / x86_64-windows-release (pull_request) Successful in 42m8s
Required
Details
ci / x86_64-freebsd-debug (pull_request) Successful in 43m13s
Required
Details
ci / x86_64-linux-debug (pull_request) Successful in 48m9s
Required
Details
ci / x86_64-openbsd-release (pull_request) Successful in 49m27s
Required
Details
ci / x86_64-windows-debug (pull_request) Successful in 53m4s
Required
Details
ci / aarch64-macos-debug (pull_request) Successful in 58m11s
Required
Details
ci / x86_64-openbsd-debug (pull_request) Successful in 58m0s
Required
Details
ci / aarch64-linux-release (pull_request) Successful in 1h34m44s
Required
Details
ci / x86_64-linux-release (pull_request) Successful in 2h12m10s
Required
Details
ci / aarch64-linux-debug (pull_request) Successful in 2h17m35s
Required
Details
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h35m47s
Required
Details
ci / powerpc64le-linux-release (pull_request) Successful in 1h42m51s
ci / powerpc64le-linux-debug (pull_request) Successful in 3h43m38s
ci / s390x-linux-debug (pull_request) Successful in 3h35m23s
ci / s390x-linux-release (pull_request) Successful in 2h47m3s
ci / loongarch64-linux-release (pull_request) Successful in 2h29m9s
ci / loongarch64-linux-debug (pull_request) Successful in 2h51m10s
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u flexible_struct:tristanpemble-flexible_struct
git switch tristanpemble-flexible_struct
Sign in to join this conversation.
No reviewers
Labels
Clear labels
abi/f32
abi/ilp32
abi/n32
abi/sf
abi/x32
accepted

This proposal is planned.
arch/1750a
arch/21k
arch/6502
arch/a29k
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/avr32
arch/bfin
arch/bpf
arch/clipper
arch/colossus
arch/cr16
arch/cris
arch/csky
arch/dlx
arch/dsp16xx
arch/elxsi
arch/epiphany
arch/fr30
arch/frv
arch/h8300
arch/h8500
arch/hexagon
arch/hppa
arch/hppa64
arch/i370
arch/i860
arch/i960
arch/ia64
arch/ip2k
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/maxq
arch/mcore
arch/metag
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/mn10200
arch/mn10300
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/nios2
arch/ns32k
arch/nvptx
arch/or1k
arch/pdp10
arch/pdp11
arch/pj
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390
arch/s390x
arch/sh
arch/sh64
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/st200
arch/starcore
arch/tilegx
arch/tilepro
arch/tricore
arch/ts
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/we32k
arch/x86
arch/x86_16
arch/x86_64
arch/xcore
arch/xgate
arch/xstormy16
arch/xtensa
autodoc

The web application for interactive documentation and generation of its assets.
backend/c

The C backend outputs C source code.
backend/llvm

The LLVM backend outputs an LLVM bitcode module.
backend/self-hosted

The self-hosted backends produce machine code directly.
binutils

Zig's included binary utilities: zig ar, zig dlltool, zig lib, zig ranlib, zig objcopy, and zig rc.
breaking

Implementing this issue could cause existing code to no longer compile or have different behavior.
build system

The Zig build system - zig build, std.Build, the build runner, and package management.
debug info

An issue related to debug information (e.g. DWARF) produced by the Zig compiler.
docs

An issue with documentation, e.g. the language reference or standard library doc comments.
error message

This issue points out an error message that is unhelpful and should be improved.
frontend

Tokenization, parsing, AstGen, ZonGen, Sema, Legalize, and Liveness.
fuzzing

An issue related to Zig's integrated fuzz testing.
incremental

Reuse of internal compiler state for faster compilation.
lib/c

This issue relates to Zig's libc implementation and/or vendored libcs.
lib/compiler-rt

This issue relates to Zig's compiler-rt library.
lib/cxx

This issue relates to Zig's vendored libc++ and/or libc++abi.
lib/std

This issue relates to Zig's standard library.
lib/tsan

This issue relates to Zig's vendored libtsan.
lib/ubsan-rt

This issue relates to Zig's ubsan-rt library.
lib/unwind

This issue relates to Zig's vendored libunwind.
linking

Zig's integrated object file and incremental linker.
miscompilation

The compiler reports success but produces semantically incorrect code.
os/aix
os/android
os/bridgeos
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/kfreebsd
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/solaris
os/tvos
os/uefi
os/visionos
os/wali
os/wasi
os/watchos
os/windows
os/zos
proposal

This issue suggests modifications. If it also has the "accepted" label then it is planned.
release notes

This issue or pull request should be mentioned in the release notes.
testing

This issue is related to testing the compiler, standard library, or other parts of Zig.
tier system

This issue tracks the support tier for a target.
zig cc

Zig as a drop-in C-family compiler.
zig fmt

The Zig source code formatter.
bounty

https://ziglang.org/news/announcing-donor-bounties
bug

Observed behavior contradicts documented or intended behavior.
contributor-friendly

This issue is limited in scope and/or knowledge of project internals.
downstream

An issue with a third-party project that uses this project.
enhancement

Solving this issue will likely involve adding new logic or components to the codebase.
infra

An issue related to project infrastructure, e.g. continuous integration.
optimization

A task to improve performance and/or resource usage.
question

No questions on the issue tracker; use a community space instead.
regression

A bug that did not occur in a previous version.
upstream

An issue with a third-party project that this project uses.
No labels
abi/f32
abi/ilp32
abi/n32
abi/sf
abi/x32
accepted
arch/1750a
arch/21k
arch/6502
arch/a29k
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/avr32
arch/bfin
arch/bpf
arch/clipper
arch/colossus
arch/cr16
arch/cris
arch/csky
arch/dlx
arch/dsp16xx
arch/elxsi
arch/epiphany
arch/fr30
arch/frv
arch/h8300
arch/h8500
arch/hexagon
arch/hppa
arch/hppa64
arch/i370
arch/i860
arch/i960
arch/ia64
arch/ip2k
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/maxq
arch/mcore
arch/metag
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/mn10200
arch/mn10300
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/nios2
arch/ns32k
arch/nvptx
arch/or1k
arch/pdp10
arch/pdp11
arch/pj
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390
arch/s390x
arch/sh
arch/sh64
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/st200
arch/starcore
arch/tilegx
arch/tilepro
arch/tricore
arch/ts
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/we32k
arch/x86
arch/x86_16
arch/x86_64
arch/xcore
arch/xgate
arch/xstormy16
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/aix
os/android
os/bridgeos
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/kfreebsd
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/solaris
os/tvos
os/uefi
os/visionos
os/wali
os/wasi
os/watchos
os/windows
os/zos
proposal
release notes
testing
tier system
zig cc
zig fmt
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ziglang/zig!30823
Reference in a new issue
ziglang/zig
No description provided.
Delete branch "tristanpemble/zig:flexible_struct"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?