authorgravatar for git@l4.pmluna <git@l4.pm> 2020-12-22 18:47:11-03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-12-22 16:47:11-05:00
log6a75cfd0f6d45de3b4d1ad74eaec892dc1cd6e30
treeafdcfa48b211d5ea9ebde4b09f9868f403c72765
parent43dbe86226fe89c6364fa0261297f1a9d8eb2a58
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

cast sendto to SendError inside send (#7481)

* cast sendto to SendError inside send * remove WouldBlock from SendToError * add missing ENOTCONN mapping * remove SystemResources duplicate * move NetworkUnreachable to SendError * add NetworkSubsystemFailed to SendError * Use zig's implicit error set casting Co-authored-by: Andrew Kelley <andrew@ziglang.org>

2 files changed, 22 insertions(+), 10 deletions(-)

lib/std/fs/file.zig+2
...@@ -698,6 +698,8 @@ pub const File = struct {...@@ -698,6 +698,8 @@ pub const File = struct {
698 error.FastOpenAlreadyInProgress,698 error.FastOpenAlreadyInProgress,
699 error.MessageTooBig,699 error.MessageTooBig,
700 error.FileDescriptorNotASocket,700 error.FileDescriptorNotASocket,
701 error.NetworkUnreachable,
702 error.NetworkSubsystemFailed,
701 => return self.writeFileAllUnseekable(in_file, args),703 => return self.writeFileAllUnseekable(in_file, args),
702704
703 else => |e| return e,705 else => |e| return e,
lib/std/os.zig+20-10
...@@ -4774,6 +4774,12 @@ pub const SendError = error{...@@ -4774,6 +4774,12 @@ pub const SendError = error{
4774 BrokenPipe,4774 BrokenPipe,
47754775
4776 FileDescriptorNotASocket,4776 FileDescriptorNotASocket,
4777
4778 /// Network is unreachable.
4779 NetworkUnreachable,
4780
4781 /// The local network interface used to reach the destination is down.
4782 NetworkSubsystemFailed,
4777} || UnexpectedError;4783} || UnexpectedError;
47784784
4779pub const SendToError = SendError || error{4785pub const SendToError = SendError || error{
...@@ -4790,15 +4796,8 @@ pub const SendToError = SendError || error{...@@ -4790,15 +4796,8 @@ pub const SendToError = SendError || error{
4790 FileNotFound,4796 FileNotFound,
4791 NotDir,4797 NotDir,
47924798
4793 /// Network is unreachable.
4794 NetworkUnreachable,
4795
4796 /// Insufficient memory was available to fulfill the request.
4797 SystemResources,
4798
4799 /// The socket is not connected (connection-oriented sockets only).4799 /// The socket is not connected (connection-oriented sockets only).
4800 SocketNotConnected,4800 SocketNotConnected,
4801 WouldBlock,
4802 AddressNotAvailable,4801 AddressNotAvailable,
4803};4802};
48044803
...@@ -4853,7 +4852,7 @@ pub fn sendto(...@@ -4853,7 +4852,7 @@ pub fn sendto(
4853 .WSAEHOSTUNREACH => return error.NetworkUnreachable,4852 .WSAEHOSTUNREACH => return error.NetworkUnreachable,
4854 // TODO: WSAEINPROGRESS, WSAEINTR4853 // TODO: WSAEINPROGRESS, WSAEINTR
4855 .WSAEINVAL => unreachable,4854 .WSAEINVAL => unreachable,
4856 .WSAENETDOWN => return error.NetworkUnreachable,4855 .WSAENETDOWN => return error.NetworkSubsystemFailed,
4857 .WSAENETRESET => return error.ConnectionResetByPeer,4856 .WSAENETRESET => return error.ConnectionResetByPeer,
4858 .WSAENETUNREACH => return error.NetworkUnreachable,4857 .WSAENETUNREACH => return error.NetworkUnreachable,
4859 .WSAENOTCONN => return error.SocketNotConnected,4858 .WSAENOTCONN => return error.SocketNotConnected,
...@@ -4882,7 +4881,6 @@ pub fn sendto(...@@ -4882,7 +4881,6 @@ pub fn sendto(
4882 EMSGSIZE => return error.MessageTooBig,4881 EMSGSIZE => return error.MessageTooBig,
4883 ENOBUFS => return error.SystemResources,4882 ENOBUFS => return error.SystemResources,
4884 ENOMEM => return error.SystemResources,4883 ENOMEM => return error.SystemResources,
4885 ENOTCONN => unreachable, // The socket is not connected, and no target has been given.
4886 ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket.4884 ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket.
4887 EOPNOTSUPP => unreachable, // Some bit in the flags argument is inappropriate for the socket type.4885 EOPNOTSUPP => unreachable, // Some bit in the flags argument is inappropriate for the socket type.
4888 EPIPE => return error.BrokenPipe,4886 EPIPE => return error.BrokenPipe,
...@@ -4892,6 +4890,9 @@ pub fn sendto(...@@ -4892,6 +4890,9 @@ pub fn sendto(
4892 ENOENT => return error.FileNotFound,4890 ENOENT => return error.FileNotFound,
4893 ENOTDIR => return error.NotDir,4891 ENOTDIR => return error.NotDir,
4894 EHOSTUNREACH => return error.NetworkUnreachable,4892 EHOSTUNREACH => return error.NetworkUnreachable,
4893 ENETUNREACH => return error.NetworkUnreachable,
4894 ENOTCONN => return error.SocketNotConnected,
4895 ENETDOWN => return error.NetworkSubsystemFailed,
4895 else => |err| return unexpectedErrno(err),4896 else => |err| return unexpectedErrno(err),
4896 }4897 }
4897 }4898 }
...@@ -4923,7 +4924,16 @@ pub fn send(...@@ -4923,7 +4924,16 @@ pub fn send(
4923 buf: []const u8,4924 buf: []const u8,
4924 flags: u32,4925 flags: u32,
4925) SendError!usize {4926) SendError!usize {
4926 return sendto(sockfd, buf, flags, null, 0);4927 return sendto(sockfd, buf, flags, null, 0) catch |err| switch (err) {
4928 error.AddressFamilyNotSupported => unreachable,
4929 error.SymLinkLoop => unreachable,
4930 error.NameTooLong => unreachable,
4931 error.FileNotFound => unreachable,
4932 error.NotDir => unreachable,
4933 error.NetworkUnreachable => unreachable,
4934 error.AddressNotAvailable => unreachable,
4935 else => |e| return e,
4936 };
4927}4937}
49284938
4929pub const SendFileError = PReadError || WriteError || SendError;4939pub const SendFileError = PReadError || WriteError || SendError;