Commit 90fdd21df6

Andrew Kelley <andrew@ziglang.org>
2025-10-16 06:08:42
std: move DNS record enum to a better namespace
1 parent bf841bb
Changed files (5)
lib/std/Io/net/HostName.zig
@@ -147,13 +147,20 @@ pub fn expand(noalias packet: []const u8, start_i: usize, noalias dest_buffer: [
     return error.InvalidDnsPacket;
 }
 
+pub const DnsRecord = enum(u8) {
+    A = 1,
+    CNAME = 5,
+    AAAA = 28,
+    _,
+};
+
 pub const DnsResponse = struct {
     bytes: []const u8,
     bytes_index: u32,
     answers_remaining: u16,
 
     pub const Answer = struct {
-        rr: u8,
+        rr: DnsRecord,
         packet: []const u8,
         data_off: u32,
         data_len: u16,
@@ -190,7 +197,7 @@ pub const DnsResponse = struct {
         if (i + 10 + len > r.len) return error.InvalidDnsPacket;
         defer dr.bytes_index = i + 10 + len;
         return .{
-            .rr = r[i + 1],
+            .rr = @enumFromInt(r[i + 1]),
             .packet = r,
             .data_off = i + 10,
             .data_len = len,
lib/std/Io/Threaded.zig
@@ -3130,9 +3130,9 @@ fn lookupDns(
     options: HostName.LookupOptions,
 ) HostName.LookupError!void {
     const t_io = t.io();
-    const family_records: [2]struct { af: IpAddress.Family, rr: u8 } = .{
-        .{ .af = .ip6, .rr = std.posix.RR.A },
-        .{ .af = .ip4, .rr = std.posix.RR.AAAA },
+    const family_records: [2]struct { af: IpAddress.Family, rr: HostName.DnsRecord } = .{
+        .{ .af = .ip6, .rr = .A },
+        .{ .af = .ip4, .rr = .AAAA },
     };
     var query_buffers: [2][280]u8 = undefined;
     var answer_buffer: [2 * 512]u8 = undefined;
@@ -3280,7 +3280,7 @@ fn lookupDns(
             // Here we could potentially add diagnostics to the results queue.
             continue;
         }) |record| switch (record.rr) {
-            std.posix.RR.A => {
+            .A => {
                 const data = record.packet[record.data_off..][0..record.data_len];
                 if (data.len != 4) return error.InvalidDnsARecord;
                 try resolved.putOne(t_io, .{ .address = .{ .ip4 = .{
@@ -3289,7 +3289,7 @@ fn lookupDns(
                 } } });
                 addresses_len += 1;
             },
-            std.posix.RR.AAAA => {
+            .AAAA => {
                 const data = record.packet[record.data_off..][0..record.data_len];
                 if (data.len != 16) return error.InvalidDnsAAAARecord;
                 try resolved.putOne(t_io, .{ .address = .{ .ip6 = .{
@@ -3298,11 +3298,11 @@ fn lookupDns(
                 } } });
                 addresses_len += 1;
             },
-            std.posix.RR.CNAME => {
+            .CNAME => {
                 _, canonical_name = HostName.expand(record.packet, record.data_off, options.canonical_name_buffer) catch
                     return error.InvalidDnsCnameRecord;
             },
-            else => continue,
+            _ => continue,
         };
     }
 
@@ -3413,7 +3413,7 @@ fn lookupHostsReader(
 }
 
 /// Writes DNS resolution query packet data to `w`; at most 280 bytes.
-fn writeResolutionQuery(q: *[280]u8, op: u4, dname: []const u8, class: u8, ty: u8, entropy: [2]u8) usize {
+fn writeResolutionQuery(q: *[280]u8, op: u4, dname: []const u8, class: u8, ty: HostName.DnsRecord, entropy: [2]u8) usize {
     // This implementation is ported from musl libc.
     // A more idiomatic "ziggy" implementation would be welcome.
     var name = dname;
@@ -3437,7 +3437,7 @@ fn writeResolutionQuery(q: *[280]u8, op: u4, dname: []const u8, class: u8, ty: u
         if (j - i - 1 > 62) unreachable;
         q[i - 1] = @intCast(j - i);
     }
-    q[i + 1] = ty;
+    q[i + 1] = @intFromEnum(ty);
     q[i + 3] = class;
     return n;
 }
lib/std/os/linux.zig
@@ -7096,12 +7096,6 @@ pub const IPPROTO = struct {
     pub const MAX = 256;
 };
 
-pub const RR = struct {
-    pub const A = 1;
-    pub const CNAME = 5;
-    pub const AAAA = 28;
-};
-
 pub const tcp_repair_opt = extern struct {
     opt_code: u32,
     opt_val: u32,
lib/std/posix.zig
@@ -98,7 +98,6 @@ pub const POSIX_FADV = system.POSIX_FADV;
 pub const PR = system.PR;
 pub const PROT = system.PROT;
 pub const RLIM = system.RLIM;
-pub const RR = system.RR;
 pub const S = system.S;
 pub const SA = system.SA;
 pub const SC = system.SC;
src/main.zig
@@ -5062,6 +5062,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
     // Prevents bootstrap from depending on a bunch of unnecessary stuff.
     var http_client: if (dev.env.supports(.fetch_command)) std.http.Client else struct {
         allocator: Allocator,
+        io: Io,
         fn deinit(_: @This()) void {}
     } = .{ .allocator = gpa, .io = io };
     defer http_client.deinit();