Commit ecdc00466c

Andrew Kelley <andrew@ziglang.org>
2025-10-24 05:33:34
std.Io.net: make it easier to use netReceiveMany correctly
1 parent c87fbd5
Changed files (2)
lib/std/Io/net.zig
@@ -904,10 +904,18 @@ pub const IncomingMessage = struct {
     data: []u8,
     /// Supplied by caller before calling receive functions; mutated by receive
     /// functions.
-    control: []u8 = &.{},
+    control: []u8,
     /// Populated by receive functions.
     flags: Flags,
 
+    /// Useful for initializing before calling `receiveManyTimeout`.
+    pub const init: IncomingMessage = .{
+        .from = undefined,
+        .data = undefined,
+        .control = &.{},
+        .flags = undefined,
+    };
+
     pub const Flags = packed struct(u8) {
         /// indicates end-of-record; the data returned completed a record
         /// (generally used with sockets of type SOCK_SEQPACKET).
@@ -1146,6 +1154,8 @@ pub const Socket = struct {
     pub fn receiveManyTimeout(
         s: *const Socket,
         io: Io,
+        /// Function assumes each element has initialized `control` field.
+        /// Initializing with `IncomingMessage.init` may be helpful.
         message_buffer: []IncomingMessage,
         data_buffer: []u8,
         flags: ReceiveFlags,
lib/std/Io/Threaded.zig
@@ -5009,7 +5009,7 @@ fn lookupDns(
         } };
 
         while (true) {
-            var message_buffer: [max_messages]Io.net.IncomingMessage = undefined;
+            var message_buffer: [max_messages]Io.net.IncomingMessage = @splat(.init);
             const buf = answer_buffer[answer_buffer_i..];
             const recv_err, const recv_n = socket.receiveManyTimeout(t_io, &message_buffer, buf, .{}, timeout);
             for (message_buffer[0..recv_n]) |*received_message| {