Skip to content

Commit 72d5c81

Browse files
committed
fix(examples): improve ArrayList compatibility for Zig 0.16+
- add version checks to support managed and unmanaged ArrayList initialization - update user append logic to handle allocator parameter for Zig 0.16+ - ensure event handling example works across Zig 0.14, 0.15, and 0.16+
1 parent c7380f8 commit 72d5c81

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

examples/event_handling/main.zig

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! This example demonstrates advanced event handling, context management, and multi-client support
33
const std = @import("std");
44
const webui = @import("webui");
5+
const builtin = @import("builtin");
56

67
const html = @embedFile("index.html");
78

@@ -25,7 +26,14 @@ fn ensureContextsInitialized() void {
2526
global_user_contexts = std.AutoHashMap(usize, *UserContext).init(allocator);
2627
}
2728
if (online_users == null) {
28-
online_users = std.ArrayList(OnlineUser).init(allocator);
29+
// Version compatibility: Zig 0.14/0.15 use managed ArrayList, 0.16+ use unmanaged
30+
if (comptime builtin.zig_version.minor >= 16) {
31+
// Zig 0.16+ - ArrayList is unmanaged by default
32+
online_users = std.ArrayList(OnlineUser){};
33+
} else {
34+
// Zig 0.14/0.15 - ArrayList is managed
35+
online_users = std.ArrayList(OnlineUser).init(allocator);
36+
}
2937
}
3038
}
3139

@@ -214,9 +222,18 @@ fn userLogin(e: *webui.Event) void {
214222
// Add user to online list
215223
ensureContextsInitialized();
216224
if (online_users) |*users| {
217-
users.append(OnlineUser{ .client_id = e.client_id, .username = context.username }) catch {
218-
std.debug.print("Failed to add user to online list\n", .{});
219-
};
225+
// Version compatibility for append method
226+
if (comptime builtin.zig_version.minor >= 16) {
227+
// Zig 0.16+ - unmanaged ArrayList needs allocator parameter
228+
users.append(allocator, OnlineUser{ .client_id = e.client_id, .username = context.username }) catch {
229+
std.debug.print("Failed to add user to online list\n", .{});
230+
};
231+
} else {
232+
// Zig 0.14/0.15 - managed ArrayList doesn't need allocator parameter
233+
users.append(OnlineUser{ .client_id = e.client_id, .username = context.username }) catch {
234+
std.debug.print("Failed to add user to online list\n", .{});
235+
};
236+
}
220237
}
221238

222239
// Broadcast user list update to all clients

0 commit comments

Comments
 (0)