diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-06-27 20:05:22 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-07-07 22:43:51 -0700 |
| commit | 0e37ff0d591dd75ceec9208196bec29efaec607a (patch) | |
| tree | c126fa823a1f3864e9c363aac70e3a3db0219957 /lib/std/net.zig | |
| parent | 0b3f0124dc33403d329fb8ee63a93215d9af1f1e (diff) | |
| download | zig-0e37ff0d591dd75ceec9208196bec29efaec607a.tar.gz zig-0e37ff0d591dd75ceec9208196bec29efaec607a.zip | |
std.fmt: breaking API changes
added adapter to AnyWriter and GenericWriter to help bridge the gap
between old and new API
make std.testing.expectFmt work at compile-time
std.fmt no longer has a dependency on std.unicode. Formatted printing
was never properly unicode-aware. Now it no longer pretends to be.
Breakage/deprecations:
* std.fs.File.reader -> std.fs.File.deprecatedReader
* std.fs.File.writer -> std.fs.File.deprecatedWriter
* std.io.GenericReader -> std.io.Reader
* std.io.GenericWriter -> std.io.Writer
* std.io.AnyReader -> std.io.Reader
* std.io.AnyWriter -> std.io.Writer
* std.fmt.format -> std.fmt.deprecatedFormat
* std.fmt.fmtSliceEscapeLower -> std.ascii.hexEscape
* std.fmt.fmtSliceEscapeUpper -> std.ascii.hexEscape
* std.fmt.fmtSliceHexLower -> {x}
* std.fmt.fmtSliceHexUpper -> {X}
* std.fmt.fmtIntSizeDec -> {B}
* std.fmt.fmtIntSizeBin -> {Bi}
* std.fmt.fmtDuration -> {D}
* std.fmt.fmtDurationSigned -> {D}
* {} -> {f} when there is a format method
* format method signature
- anytype -> *std.io.Writer
- inferred error set -> error{WriteFailed}
- options -> (deleted)
* std.fmt.Formatted
- now takes context type explicitly
- no fmt string
Diffstat (limited to 'lib/std/net.zig')
| -rw-r--r-- | lib/std/net.zig | 70 |
1 files changed, 22 insertions, 48 deletions
diff --git a/lib/std/net.zig b/lib/std/net.zig index a2b5fff70a..cfaad090ab 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -161,22 +161,14 @@ pub const Address = extern union { } } - pub fn format( - self: Address, - comptime fmt: []const u8, - options: std.fmt.FormatOptions, - out_stream: anytype, - ) !void { - if (fmt.len != 0) std.fmt.invalidFmtError(fmt, self); + pub fn format(self: Address, w: *std.io.Writer, comptime fmt: []const u8) std.io.Writer.Error!void { + comptime assert(fmt.len == 0); switch (self.any.family) { - posix.AF.INET => try self.in.format(fmt, options, out_stream), - posix.AF.INET6 => try self.in6.format(fmt, options, out_stream), + posix.AF.INET => try self.in.format(w, fmt), + posix.AF.INET6 => try self.in6.format(w, fmt), posix.AF.UNIX => { - if (!has_unix_sockets) { - unreachable; - } - - try std.fmt.format(out_stream, "{s}", .{std.mem.sliceTo(&self.un.path, 0)}); + if (!has_unix_sockets) unreachable; + try w.writeAll(std.mem.sliceTo(&self.un.path, 0)); }, else => unreachable, } @@ -349,22 +341,10 @@ pub const Ip4Address = extern struct { self.sa.port = mem.nativeToBig(u16, port); } - pub fn format( - self: Ip4Address, - comptime fmt: []const u8, - options: std.fmt.FormatOptions, - out_stream: anytype, - ) !void { - if (fmt.len != 0) std.fmt.invalidFmtError(fmt, self); - _ = options; - const bytes = @as(*const [4]u8, @ptrCast(&self.sa.addr)); - try std.fmt.format(out_stream, "{}.{}.{}.{}:{}", .{ - bytes[0], - bytes[1], - bytes[2], - bytes[3], - self.getPort(), - }); + pub fn format(self: Ip4Address, w: *std.io.Writer, comptime fmt: []const u8) std.io.Writer.Error!void { + comptime assert(fmt.len == 0); + const bytes: *const [4]u8 = @ptrCast(&self.sa.addr); + try w.print("{d}.{d}.{d}.{d}:{d}", .{ bytes[0], bytes[1], bytes[2], bytes[3], self.getPort() }); } pub fn getOsSockLen(self: Ip4Address) posix.socklen_t { @@ -653,17 +633,11 @@ pub const Ip6Address = extern struct { self.sa.port = mem.nativeToBig(u16, port); } - pub fn format( - self: Ip6Address, - comptime fmt: []const u8, - options: std.fmt.FormatOptions, - out_stream: anytype, - ) !void { - if (fmt.len != 0) std.fmt.invalidFmtError(fmt, self); - _ = options; + pub fn format(self: Ip6Address, w: *std.io.Writer, comptime fmt: []const u8) std.io.Writer.Error!void { + comptime assert(fmt.len == 0); const port = mem.bigToNative(u16, self.sa.port); if (mem.eql(u8, self.sa.addr[0..12], &[_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff })) { - try std.fmt.format(out_stream, "[::ffff:{}.{}.{}.{}]:{}", .{ + try w.print("[::ffff:{d}.{d}.{d}.{d}]:{d}", .{ self.sa.addr[12], self.sa.addr[13], self.sa.addr[14], @@ -711,14 +685,14 @@ pub const Ip6Address = extern struct { longest_len = 0; } - try out_stream.writeAll("["); + try w.writeAll("["); var i: usize = 0; var abbrv = false; while (i < native_endian_parts.len) : (i += 1) { if (i == longest_start) { // Emit "::" for the longest zero run if (!abbrv) { - try out_stream.writeAll(if (i == 0) "::" else ":"); + try w.writeAll(if (i == 0) "::" else ":"); abbrv = true; } i += longest_len - 1; // Skip the compressed range @@ -727,12 +701,12 @@ pub const Ip6Address = extern struct { if (abbrv) { abbrv = false; } - try std.fmt.format(out_stream, "{x}", .{native_endian_parts[i]}); + try w.print("{x}", .{native_endian_parts[i]}); if (i != native_endian_parts.len - 1) { - try out_stream.writeAll(":"); + try w.writeAll(":"); } } - try std.fmt.format(out_stream, "]:{}", .{port}); + try w.print("]:{}", .{port}); } pub fn getOsSockLen(self: Ip6Address) posix.socklen_t { @@ -894,7 +868,7 @@ pub fn getAddressList(allocator: mem.Allocator, name: []const u8, port: u16) Get const name_c = try allocator.dupeZ(u8, name); defer allocator.free(name_c); - const port_c = try std.fmt.allocPrintZ(allocator, "{}", .{port}); + const port_c = try std.fmt.allocPrintSentinel(allocator, "{}", .{port}, 0); defer allocator.free(port_c); const ws2_32 = windows.ws2_32; @@ -966,7 +940,7 @@ pub fn getAddressList(allocator: mem.Allocator, name: []const u8, port: u16) Get const name_c = try allocator.dupeZ(u8, name); defer allocator.free(name_c); - const port_c = try std.fmt.allocPrintZ(allocator, "{}", .{port}); + const port_c = try std.fmt.allocPrintSentinel(allocator, "{}", .{port}, 0); defer allocator.free(port_c); const hints: posix.addrinfo = .{ @@ -1356,7 +1330,7 @@ fn linuxLookupNameFromHosts( }; defer file.close(); - var buffered_reader = std.io.bufferedReader(file.reader()); + var buffered_reader = std.io.bufferedReader(file.deprecatedReader()); const reader = buffered_reader.reader(); var line_buf: [512]u8 = undefined; while (reader.readUntilDelimiterOrEof(&line_buf, '\n') catch |err| switch (err) { @@ -1557,7 +1531,7 @@ fn getResolvConf(allocator: mem.Allocator, rc: *ResolvConf) !void { }; defer file.close(); - var buf_reader = std.io.bufferedReader(file.reader()); + var buf_reader = std.io.bufferedReader(file.deprecatedReader()); const stream = buf_reader.reader(); var line_buf: [512]u8 = undefined; while (stream.readUntilDelimiterOrEof(&line_buf, '\n') catch |err| switch (err) { |
