| author | |
| committer | |
| log | d3871af724f838fdb8b6ef95b38ef1409b46c0b7 |
| tree | c2f24663a7b63b45990d9be4d44588b881ba0a11 |
| parent | d7b601b35e16788000853903dabaa0b10081660e |
Only allow SOCK_STREAM (std.x.net.tcp.Client) sockets to have wrappers
for std.io.Reader and std.io.Writer provided, instead of Socket's in
general.2 files changed, 39 insertions(+), 39 deletions(-)
lib/std/x/net/tcp.zig+39| ... | ... | @@ -6,6 +6,7 @@ |
| 6 | 6 | |
| 7 | 7 | const std = @import("../../std.zig"); |
| 8 | 8 | |
| 9 | const io = std.io; | |
| 9 | 10 | const os = std.os; |
| 10 | 11 | const ip = std.x.net.ip; |
| 11 | 12 | |
| ... | ... | @@ -58,6 +59,28 @@ pub const Domain = extern enum(u16) { |
| 58 | 59 | pub const Client = struct { |
| 59 | 60 | socket: Socket, |
| 60 | 61 | |
| 62 | /// Implements `std.io.Reader`. | |
| 63 | pub const Reader = struct { | |
| 64 | client: Client, | |
| 65 | flags: u32, | |
| 66 | ||
| 67 | /// Implements `readFn` for `std.io.Reader`. | |
| 68 | pub fn read(self: Client.Reader, buffer: []u8) !usize { | |
| 69 | return self.client.read(buffer, self.flags); | |
| 70 | } | |
| 71 | }; | |
| 72 | ||
| 73 | /// Implements `std.io.Writer`. | |
| 74 | pub const Writer = struct { | |
| 75 | client: Client, | |
| 76 | flags: u32, | |
| 77 | ||
| 78 | /// Implements `writeFn` for `std.io.Writer`. | |
| 79 | pub fn write(self: Client.Writer, buffer: []const u8) !usize { | |
| 80 | return self.client.write(buffer, self.flags); | |
| 81 | } | |
| 82 | }; | |
| 83 | ||
| 61 | 84 | /// Opens a new client. |
| 62 | 85 | pub fn init(domain: tcp.Domain, flags: u32) !Client { |
| 63 | 86 | return Client{ |
| ... | ... | @@ -89,6 +112,22 @@ pub const Client = struct { |
| 89 | 112 | return self.socket.connect(address.into()); |
| 90 | 113 | } |
| 91 | 114 | |
| 115 | /// Extracts the error set of a function. | |
| 116 | /// TODO: remove after Socket.{read, write} error unions are well-defined across different platforms | |
| 117 | fn ErrorSetOf(comptime Function: anytype) type { | |
| 118 | return @typeInfo(@typeInfo(@TypeOf(Function)).Fn.return_type.?).ErrorUnion.error_set; | |
| 119 | } | |
| 120 | ||
| 121 | /// Wrap `tcp.Client` into `std.io.Reader`. | |
| 122 | pub fn reader(self: Client, flags: u32) io.Reader(Client.Reader, ErrorSetOf(Client.Reader.read), Client.Reader.read) { | |
| 123 | return .{ .context = .{ .client = self, .flags = flags } }; | |
| 124 | } | |
| 125 | ||
| 126 | /// Wrap `tcp.Client` into `std.io.Writer`. | |
| 127 | pub fn writer(self: Client, flags: u32) io.Writer(Client.Writer, ErrorSetOf(Client.Writer.write), Client.Writer.write) { | |
| 128 | return .{ .context = .{ .client = self, .flags = flags } }; | |
| 129 | } | |
| 130 | ||
| 92 | 131 | /// Read data from the socket into the buffer provided with a set of flags |
| 93 | 132 | /// specified. It returns the number of bytes read into the buffer provided. |
| 94 | 133 | pub fn read(self: Client, buf: []u8, flags: u32) !usize { |
lib/std/x/os/socket.zig-39| ... | ... | @@ -7,7 +7,6 @@ |
| 7 | 7 | const std = @import("../../std.zig"); |
| 8 | 8 | const net = @import("net.zig"); |
| 9 | 9 | |
| 10 | const io = std.io; | |
| 11 | 10 | const os = std.os; |
| 12 | 11 | const fmt = std.fmt; |
| 13 | 12 | const mem = std.mem; |
| ... | ... | @@ -114,43 +113,5 @@ pub fn Mixin(comptime Self: type) type { |
| 114 | 113 | } |
| 115 | 114 | } |
| 116 | 115 | }; |
| 117 | ||
| 118 | /// Implements `std.io.Reader`. | |
| 119 | pub const Reader = struct { | |
| 120 | socket: Self, | |
| 121 | flags: u32, | |
| 122 | ||
| 123 | /// Implements `readFn` for `std.io.Reader`. | |
| 124 | pub fn read(self: Self.Reader, buffer: []u8) !usize { | |
| 125 | return self.socket.read(buffer, self.flags); | |
| 126 | } | |
| 127 | }; | |
| 128 | ||
| 129 | /// Implements `std.io.Writer`. | |
| 130 | pub const Writer = struct { | |
| 131 | socket: Self, | |
| 132 | flags: u32, | |
| 133 | ||
| 134 | /// Implements `writeFn` for `std.io.Writer`. | |
| 135 | pub fn write(self: Self.Writer, buffer: []const u8) !usize { | |
| 136 | return self.socket.write(buffer, self.flags); | |
| 137 | } | |
| 138 | }; | |
| 139 | ||
| 140 | /// Extracts the error set of a function. | |
| 141 | /// TODO: remove after Socket.{read, write} error unions are well-defined across different platforms | |
| 142 | fn ErrorSetOf(comptime Function: anytype) type { | |
| 143 | return @typeInfo(@typeInfo(@TypeOf(Function)).Fn.return_type.?).ErrorUnion.error_set; | |
| 144 | } | |
| 145 | ||
| 146 | /// Wrap `Socket` into `std.io.Reader`. | |
| 147 | pub fn reader(self: Self, flags: u32) io.Reader(Self.Reader, ErrorSetOf(Self.Reader.read), Self.Reader.read) { | |
| 148 | return .{ .context = .{ .socket = self, .flags = flags } }; | |
| 149 | } | |
| 150 | ||
| 151 | /// Wrap `Socket` into `std.io.Writer`. | |
| 152 | pub fn writer(self: Self, flags: u32) io.Writer(Self.Writer, ErrorSetOf(Self.Writer.write), Self.Writer.write) { | |
| 153 | return .{ .context = .{ .socket = self, .flags = flags } }; | |
| 154 | } | |
| 155 | 116 | }; |
| 156 | 117 | } |