x/net/tcp, x/os/socket: tcp.Client.{Reader, Writer}

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.
This commit is contained in:
lithdew 2021-05-08 23:13:43 +09:00
parent d7b601b35e
commit d3871af724
2 changed files with 39 additions and 39 deletions

View File

@ -6,6 +6,7 @@
const std = @import("../../std.zig");
const io = std.io;
const os = std.os;
const ip = std.x.net.ip;
@ -58,6 +59,28 @@ pub const Domain = extern enum(u16) {
pub const Client = struct {
socket: Socket,
/// Implements `std.io.Reader`.
pub const Reader = struct {
client: Client,
flags: u32,
/// Implements `readFn` for `std.io.Reader`.
pub fn read(self: Client.Reader, buffer: []u8) !usize {
return self.client.read(buffer, self.flags);
}
};
/// Implements `std.io.Writer`.
pub const Writer = struct {
client: Client,
flags: u32,
/// Implements `writeFn` for `std.io.Writer`.
pub fn write(self: Client.Writer, buffer: []const u8) !usize {
return self.client.write(buffer, self.flags);
}
};
/// Opens a new client.
pub fn init(domain: tcp.Domain, flags: u32) !Client {
return Client{
@ -89,6 +112,22 @@ pub const Client = struct {
return self.socket.connect(address.into());
}
/// Extracts the error set of a function.
/// TODO: remove after Socket.{read, write} error unions are well-defined across different platforms
fn ErrorSetOf(comptime Function: anytype) type {
return @typeInfo(@typeInfo(@TypeOf(Function)).Fn.return_type.?).ErrorUnion.error_set;
}
/// Wrap `tcp.Client` into `std.io.Reader`.
pub fn reader(self: Client, flags: u32) io.Reader(Client.Reader, ErrorSetOf(Client.Reader.read), Client.Reader.read) {
return .{ .context = .{ .client = self, .flags = flags } };
}
/// Wrap `tcp.Client` into `std.io.Writer`.
pub fn writer(self: Client, flags: u32) io.Writer(Client.Writer, ErrorSetOf(Client.Writer.write), Client.Writer.write) {
return .{ .context = .{ .client = self, .flags = flags } };
}
/// Read data from the socket into the buffer provided with a set of flags
/// specified. It returns the number of bytes read into the buffer provided.
pub fn read(self: Client, buf: []u8, flags: u32) !usize {

View File

@ -7,7 +7,6 @@
const std = @import("../../std.zig");
const net = @import("net.zig");
const io = std.io;
const os = std.os;
const fmt = std.fmt;
const mem = std.mem;
@ -114,43 +113,5 @@ pub fn Mixin(comptime Self: type) type {
}
}
};
/// Implements `std.io.Reader`.
pub const Reader = struct {
socket: Self,
flags: u32,
/// Implements `readFn` for `std.io.Reader`.
pub fn read(self: Self.Reader, buffer: []u8) !usize {
return self.socket.read(buffer, self.flags);
}
};
/// Implements `std.io.Writer`.
pub const Writer = struct {
socket: Self,
flags: u32,
/// Implements `writeFn` for `std.io.Writer`.
pub fn write(self: Self.Writer, buffer: []const u8) !usize {
return self.socket.write(buffer, self.flags);
}
};
/// Extracts the error set of a function.
/// TODO: remove after Socket.{read, write} error unions are well-defined across different platforms
fn ErrorSetOf(comptime Function: anytype) type {
return @typeInfo(@typeInfo(@TypeOf(Function)).Fn.return_type.?).ErrorUnion.error_set;
}
/// Wrap `Socket` into `std.io.Reader`.
pub fn reader(self: Self, flags: u32) io.Reader(Self.Reader, ErrorSetOf(Self.Reader.read), Self.Reader.read) {
return .{ .context = .{ .socket = self, .flags = flags } };
}
/// Wrap `Socket` into `std.io.Writer`.
pub fn writer(self: Self, flags: u32) io.Writer(Self.Writer, ErrorSetOf(Self.Writer.write), Self.Writer.write) {
return .{ .context = .{ .socket = self, .flags = flags } };
}
};
}