use hasDecl instead of switch on builtin.os

This commit is contained in:
Luna 2019-11-10 14:04:52 -03:00
parent 25423eb453
commit 2d02920a90

View File

@ -10,14 +10,13 @@ test "" {
_ = @import("net/test.zig");
}
const has_unix_sockets = @hasDecl(os, "sockaddr_un");
pub const Address = extern union {
any: os.sockaddr,
in: os.sockaddr_in,
in6: os.sockaddr_in6,
un: switch (builtin.os) {
.linux, .macosx, .freebsd, .netbsd => os.sockaddr_un,
else => void,
},
un: if (has_unix_sockets) os.sockaddr_un else void,
// TODO this crashed the compiler
//pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0);
@ -239,6 +238,7 @@ pub const Address = extern union {
}
/// Returns the port in native endian.
/// Asserts that the address is ip4 or ip6.
pub fn getPort(self: Address) u16 {
const big_endian_port = switch (self.any.family) {
os.AF_INET => self.in.port,
@ -249,6 +249,7 @@ pub const Address = extern union {
}
/// `port` is native-endian.
/// Asserts that the address is ip4 or ip6.
pub fn setPort(self: *Address, port: u16) void {
const ptr = switch (self.any.family) {
os.AF_INET => &self.in.port,