Followup from #31571.
Most callers should already know these values, which means that they don't need to be redundantly stored in std.Io.net.Server.
--- a/lib/std/Io/Threaded.zig
+++ b/lib/std/Io/Threaded.zig
@@ -12450,7 +12450,7 @@ fn netAcceptPosix(userdata: ?*anyopaque, listen_fd: net.Socket.Handle, options:
if (!have_networking) return error.NetworkDown;
const t: *Threaded = @ptrCast(@alignCast(userdata));
_ = t;
- options;
+ _ = options;
var storage: PosixAddress = undefined;
var addr_len: posix.socklen_t = @sizeOf(PosixAddress);
const syscall: Syscall = try .start();
@@ -12514,10 +12514,10 @@ fn netAcceptWindows(userdata: ?*anyopaque, listen_handle: net.Socket.Handle, opt
else => |status| return windows.unexpectedStatus(status),
}
errdefer t.deferAcceptAfd(listen_handle, storage.Info);
- const accept_handle = openSocketAfd(
- storage.RemoteAddress.posix.any.family,
- .{ .mode = options.mode, .protocol = options.protocol },
- ) catch |err| switch (err) {
+ const accept_handle = openSocketAfd(options.family, .{
+ .mode = options.mode,
+ .protocol = options.protocol,
+ }) catch |err| switch (err) {
error.AddressFamilyUnsupported => return error.Unexpected,
error.ProtocolUnsupportedByAddressFamily => return error.Unexpected,
else => |e| return e,
--- a/lib/std/Io/net.zig
+++ b/lib/std/Io/net.zig
@@ -244,13 +244,7 @@ pub const IpAddress = union(enum) {
/// Waits for a TCP connection. When using this API, `bind` does not need
/// to be called. The returned `Server` has an open `stream`.
pub fn listen(address: *const IpAddress, io: Io, options: ListenOptions) ListenError!Server {
- return .{
- .socket = try io.vtable.netListenIp(io.userdata, address, options),
- .options = if (Server.AcceptOptions != void) .{
- .mode = options.mode,
- .protocol = options.protocol,
- },
- };
+ return .{ .socket = try io.vtable.netListenIp(io.userdata, address, options) };
}
pub const BindError = error{
@@ -1397,7 +1391,6 @@ pub const Stream = struct {
pub const Server = struct {
socket: Socket,
- options: AcceptOptions,
pub fn deinit(s: *Server, io: Io) void {
s.socket.close(io);
@@ -1429,14 +1422,15 @@ pub const Server = struct {
ProtocolFailure,
} || Io.UnexpectedError || Io.Cancelable;
- pub const AcceptOptions = switch (native_os) {
- .windows => struct { mode: Socket.Mode, protocol: ?Protocol },
- else => void,
+ pub const AcceptOptions = struct {
+ family: IpAddress.Family,
+ mode: Socket.Mode,
+ protocol: ?Protocol = null,
};
/// Blocks until a client connects to the server.
- pub fn accept(s: *Server, io: Io) AcceptError!Stream {
- return .{ .socket = try io.vtable.netAccept(io.userdata, s.socket.handle, s.options) };
+ pub fn accept(s: *Server, io: Io, options: AcceptOptions) AcceptError!Stream {
+ return .{ .socket = try io.vtable.netAccept(io.userdata, s.socket.handle, options) };
}
};
Followup from #31571.
Most callers should already know these values, which means that they don't need to be redundantly stored in `std.Io.net.Server`.
```diff
--- a/lib/std/Io/Threaded.zig
+++ b/lib/std/Io/Threaded.zig
@@ -12450,7 +12450,7 @@ fn netAcceptPosix(userdata: ?*anyopaque, listen_fd: net.Socket.Handle, options:
if (!have_networking) return error.NetworkDown;
const t: *Threaded = @ptrCast(@alignCast(userdata));
_ = t;
- options;
+ _ = options;
var storage: PosixAddress = undefined;
var addr_len: posix.socklen_t = @sizeOf(PosixAddress);
const syscall: Syscall = try .start();
@@ -12514,10 +12514,10 @@ fn netAcceptWindows(userdata: ?*anyopaque, listen_handle: net.Socket.Handle, opt
else => |status| return windows.unexpectedStatus(status),
}
errdefer t.deferAcceptAfd(listen_handle, storage.Info);
- const accept_handle = openSocketAfd(
- storage.RemoteAddress.posix.any.family,
- .{ .mode = options.mode, .protocol = options.protocol },
- ) catch |err| switch (err) {
+ const accept_handle = openSocketAfd(options.family, .{
+ .mode = options.mode,
+ .protocol = options.protocol,
+ }) catch |err| switch (err) {
error.AddressFamilyUnsupported => return error.Unexpected,
error.ProtocolUnsupportedByAddressFamily => return error.Unexpected,
else => |e| return e,
--- a/lib/std/Io/net.zig
+++ b/lib/std/Io/net.zig
@@ -244,13 +244,7 @@ pub const IpAddress = union(enum) {
/// Waits for a TCP connection. When using this API, `bind` does not need
/// to be called. The returned `Server` has an open `stream`.
pub fn listen(address: *const IpAddress, io: Io, options: ListenOptions) ListenError!Server {
- return .{
- .socket = try io.vtable.netListenIp(io.userdata, address, options),
- .options = if (Server.AcceptOptions != void) .{
- .mode = options.mode,
- .protocol = options.protocol,
- },
- };
+ return .{ .socket = try io.vtable.netListenIp(io.userdata, address, options) };
}
pub const BindError = error{
@@ -1397,7 +1391,6 @@ pub const Stream = struct {
pub const Server = struct {
socket: Socket,
- options: AcceptOptions,
pub fn deinit(s: *Server, io: Io) void {
s.socket.close(io);
@@ -1429,14 +1422,15 @@ pub const Server = struct {
ProtocolFailure,
} || Io.UnexpectedError || Io.Cancelable;
- pub const AcceptOptions = switch (native_os) {
- .windows => struct { mode: Socket.Mode, protocol: ?Protocol },
- else => void,
+ pub const AcceptOptions = struct {
+ family: IpAddress.Family,
+ mode: Socket.Mode,
+ protocol: ?Protocol = null,
};
/// Blocks until a client connects to the server.
- pub fn accept(s: *Server, io: Io) AcceptError!Stream {
- return .{ .socket = try io.vtable.netAccept(io.userdata, s.socket.handle, s.options) };
+ pub fn accept(s: *Server, io: Io, options: AcceptOptions) AcceptError!Stream {
+ return .{ .socket = try io.vtable.netAccept(io.userdata, s.socket.handle, options) };
}
};
```