zig-webgpu-gen

git clone git://git.electrosoup.com/zig-webgpu-gen
Log | Files | Refs

commit 692eb741b4ae33f4f5f5d7c897ee1b95659a8df0
parent 018821fbee627807c5a0f622bf8492679f51547e
Author: Christian Ermann <christianermann@gmail.com>
Date:   Wed,  4 Mar 2026 18:03:29 -0800

Improve string and array types

Diffstat:
Msrc/main.zig | 97++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 72 insertions(+), 25 deletions(-)

diff --git a/src/main.zig b/src/main.zig @@ -129,9 +129,9 @@ const PrimitiveType = enum { return switch (self) { .c_void => "anyopaque", .bool => "bool", - .nullable_string => "?[]const u8", - .string_with_default_empty => "[]const u8", - .out_string => "[]u8", + .nullable_string => "StringView", + .string_with_default_empty => "StringView", + .out_string => "StringView", .uint16 => "u16", .uint32 => "u32", .uint64 => "u64", @@ -142,31 +142,31 @@ const PrimitiveType = enum { .nullable_float32 => "f32", .float64 => "f64", .float64_supertype => "f64", - .@"array<bool>" => "[]bool", - .@"array<string>" => "[][]const u8", - .@"array<uint16>" => "[]u16", - .@"array<uint32>" => "[]u32", - .@"array<uint64>" => "[]u64", - .@"array<usize>" => "[]usize", - .@"array<int16>" => "[]i16", - .@"array<int32>" => "[]i32", - .@"array<float32>" => "[]f32", - .@"array<float64>" => "[]f64", + .@"array<bool>" => "Slice(bool)", + .@"array<string>" => "Slice(StringView)", + .@"array<uint16>" => "Slice(u16)", + .@"array<uint32>" => "Slice(u32)", + .@"array<uint64>" => "Slice(u64)", + .@"array<usize>" => "Slice(usize)", + .@"array<int16>" => "Slice(i16)", + .@"array<int32>" => "Slice(i32)", + .@"array<float32>" => "Slice(f32)", + .@"array<float64>" => "Slice(f64)", }; } fn toStringConst(self: PrimitiveType) ![]const u8 { return switch (self) { - .@"array<bool>" => "[]const bool", - .@"array<string>" => "[]const []const u8", - .@"array<uint16>" => "[]const u16", - .@"array<uint32>" => "[]const u32", - .@"array<uint64>" => "[]const u64", - .@"array<usize>" => "[]const usize", - .@"array<int16>" => "[]const i16", - .@"array<int32>" => "[]const i32", - .@"array<float32>" => "[]const f32", - .@"array<float64>" => "[]const f64", + .@"array<bool>" => "Slice(bool)", + .@"array<string>" => "Slice(StringView)", + .@"array<uint16>" => "Slice(u16)", + .@"array<uint32>" => "Slice(u32)", + .@"array<uint64>" => "Slice(u64)", + .@"array<usize>" => "Slice(usize)", + .@"array<int16>" => "Slice(i16)", + .@"array<int32>" => "Slice(i32)", + .@"array<float32>" => "Slice(f32)", + .@"array<float64>" => "Slice(f64)", else => error.Unimplemented, }; } @@ -227,8 +227,9 @@ const ComplexType = union(enum) { try snakeToPascal(single.name, writer); }, .array => |array| { - try writer.writeAll("[]"); + try writer.writeAll("Slice("); try snakeToPascal(array.name, writer); + try writer.writeAll(")"); }, } } @@ -240,8 +241,9 @@ const ComplexType = union(enum) { try snakeToPascal(single.name, writer); }, .array => |array| { - try writer.writeAll("[]const "); + try writer.writeAll("Slice("); try snakeToPascal(array.name, writer); + try writer.writeAll(")"); }, } } @@ -929,6 +931,51 @@ fn renderHeader(writer: anytype) !void { \\ struct_type: SType, \\}; \\ + \\pub const StringView = extern struct { + \\ data: ?[*]const u8 = null, + \\ length: usize = null_terminated, + \\ + \\ const null_terminated = std.math.maxInt(usize); + \\ + \\ pub fn fromSlice(slice: []const u8) StringView { + \\ return .{ + \\ .data = slice.ptr, + \\ .length = slice.len, + \\ }; + \\ } + \\ + \\ pub fn toSlice(self: StringView) ?[]const u8 { + \\ const data = self.data orelse return null; + \\ if (self.length == null_terminated) { + \\ const slice = std.mem.span( + \\ @as([*:0]const u8, @ptrCast(data)) + \\ ); + \\ return slice[0..slice.len]; + \\ } + \\ return data[0..self.length]; + \\ } + \\}; + \\ + \\pub fn Slice(comptime T: type) type { + \\ return extern struct { + \\ len: usize, + \\ ptr: [*]T, + \\ + \\ const Self = @This(); + \\ + \\ pub fn fromSlice(slice: []T) Self { + \\ return .{ + \\ .ptr = slice.ptr, + \\ .len = slice.len, + \\ }; + \\ } + \\ + \\ pub fn toSlice(self: Self) []T { + \\ return self.ptr[0..self.len]; + \\ } + \\ }; + \\} + \\ ; try writer.writeAll(header); try writer.writeAll("\n");