Skip to content

Commit

Permalink
Switched parameter order for Impl to improve clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
permutationlock committed Nov 28, 2023
1 parent ef377d0 commit dac2f07
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 49 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The `zimpl` module is ~30 lines of code and exposes one public
declaration: `Impl`.

```Zig
pub fn Impl(comptime T: type, comptime Ifc: fn (type) type) type { ... }
pub fn Impl(comptime Ifc: fn (type) type, comptime T: type) type { ... }
```

## Arguments
Expand All @@ -25,7 +25,7 @@ wrapping a type, e.g. `Unwrap(!?*u32)` is `u32`.

## Return value

The type `Impl(T, Ifc)` is a struct type with the same fields
The type `Impl(Ifc, T)` is a struct type with the same fields
as `Ifc(T)`, but with the default value of each field set equal to
the declaration of `Unwrap(T)` of the same name, if such a declaration
exists.
Expand All @@ -45,23 +45,23 @@ pub fn Reader(comptime T: type) type {
pub const io = struct {
pub inline fn read(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
buffer: []u8,
) reader_impl.ReadError!usize {
return @errorCast(reader_impl.read(reader_ctx, buffer));
}
pub inline fn readAll(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
buffer: []u8,
) reader_impl.ReadError!usize {
return readAtLeast(reader_ctx, reader_impl, buffer, buffer.len);
}
pub inline fn readAtLeast(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
buffer: []u8,
len: usize,
) reader_impl.ReadError!usize {
Expand Down
6 changes: 2 additions & 4 deletions examples/count.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const std = @import("std");
const testing = std.testing;

const zimpl = @import("zimpl");
const Impl = zimpl.Impl;
const PtrChild = zimpl.PtrChild;
const Impl = @import("zimpl").Impl;

fn Counter(comptime T: type) type {
return struct {
Expand All @@ -14,7 +12,7 @@ fn Counter(comptime T: type) type {

pub fn countToTen(
ctr_ctx: anytype,
comptime ctr_impl: Impl(@TypeOf(ctr_ctx), Counter),
ctr_impl: Impl(Counter, @TypeOf(ctr_ctx)),
) void {
while (ctr_impl.read(ctr_ctx) < 10) {
ctr_impl.increment(ctr_ctx);
Expand Down
57 changes: 28 additions & 29 deletions examples/io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const native_endian = @import("builtin").target.cpu.arch.endian();
const mem = std.mem;
const assert = std.debug.assert;

const zimpl = @import("zimpl");
const Impl = zimpl.Impl;
const Impl = @import("zimpl").Impl;

pub const FixedBufferReader = @import("io/FixedBufferReader.zig");
pub const FixedBufferStream = @import("io/FixedBufferStream.zig");
Expand Down Expand Up @@ -32,23 +31,23 @@ pub fn Reader(comptime T: type) type {

pub inline fn read(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
buffer: []u8,
) reader_impl.ReadError!usize {
return @errorCast(reader_impl.read(reader_ctx, buffer));
}

pub inline fn readAll(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
buffer: []u8,
) reader_impl.ReadError!usize {
return readAtLeast(reader_ctx, reader_impl, buffer, buffer.len);
}

pub inline fn readAtLeast(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
buffer: []u8,
len: usize,
) reader_impl.ReadError!usize {
Expand All @@ -64,7 +63,7 @@ pub inline fn readAtLeast(

pub inline fn readNoEof(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
buf: []u8,
) (reader_impl.ReadError || error{EndOfStream})!void {
const amt_read = try readAll(reader_ctx, reader_impl, buf);
Expand All @@ -73,9 +72,9 @@ pub inline fn readNoEof(

pub inline fn streamUntilDelimiter(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
writer_ctx: anytype,
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
delimiter: u8,
optional_max_size: ?usize,
) (reader_impl.ReadError || writer_impl.WriteError || error{
Expand All @@ -102,7 +101,7 @@ pub inline fn streamUntilDelimiter(

pub inline fn skipUntilDelimiterOrEof(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
delimiter: u8,
) reader_impl.ReadError!void {
while (true) {
Expand All @@ -116,7 +115,7 @@ pub inline fn skipUntilDelimiterOrEof(

pub inline fn readByte(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
) (reader_impl.ReadError || error{EndOfStream})!u8 {
var result: [1]u8 = undefined;
const amt_read = try read(reader_ctx, reader_impl, result[0..]);
Expand All @@ -126,14 +125,14 @@ pub inline fn readByte(

pub inline fn readByteSigned(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
) (reader_impl.ReadError || error{EndOfStream})!i8 {
return @as(i8, @bitCast(try readByte(reader_ctx, reader_impl)));
}

pub inline fn readBytesNoEof(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
comptime num_bytes: usize,
) (reader_impl.ReadError || error{EndOfStream})![num_bytes]u8 {
var bytes: [num_bytes]u8 = undefined;
Expand All @@ -143,7 +142,7 @@ pub inline fn readBytesNoEof(

pub inline fn readInt(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
comptime T: type,
endian: std.builtin.Endian,
) (reader_impl.ReadError || error{EndOfStream})!T {
Expand All @@ -157,7 +156,7 @@ pub inline fn readInt(

pub inline fn readVarInt(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
comptime ReturnType: type,
endian: std.builtin.Endian,
size: usize,
Expand All @@ -171,7 +170,7 @@ pub inline fn readVarInt(

pub inline fn skipBytes(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
num_bytes: u64,
comptime options: struct {
buf_size: usize = 512,
Expand All @@ -189,7 +188,7 @@ pub inline fn skipBytes(

pub inline fn isBytes(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
slice: []const u8,
) (reader_impl.ReadError || error{EndOfStream})!bool {
var i: usize = 0;
Expand All @@ -204,7 +203,7 @@ pub inline fn isBytes(

pub inline fn readStruct(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
comptime T: type,
) (reader_impl.ReadError || error{EndOfStream})!T {
// Only extern and packed structs have defined in-memory layout.
Expand All @@ -216,7 +215,7 @@ pub inline fn readStruct(

pub inline fn readStructBig(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
comptime T: type,
) (reader_impl.ReadError || error{EndOfStream})!T {
var res = try readStruct(reader_ctx, reader_impl, T);
Expand All @@ -228,7 +227,7 @@ pub inline fn readStructBig(

pub inline fn readEnum(
reader_ctx: anytype,
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
comptime Enum: type,
endian: std.builtin.Endian,
) (reader_impl.ReadError || error{ EndOfStream, InvalidValue })!Enum {
Expand Down Expand Up @@ -263,15 +262,15 @@ pub fn Writer(comptime T: type) type {

pub fn write(
writer_ctx: anytype,
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
bytes: []const u8,
) writer_impl.WriteError!usize {
return @errorCast(writer_impl.write(writer_ctx, bytes));
}

pub fn writeAll(
writer_ctx: anytype,
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
bytes: []const u8,
) writer_impl.WriteError!void {
var index: usize = 0;
Expand All @@ -282,7 +281,7 @@ pub fn writeAll(

pub fn writeByte(
writer_ctx: anytype,
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
byte: u8,
) writer_impl.WriteError!void {
const array = [1]u8{byte};
Expand All @@ -291,7 +290,7 @@ pub fn writeByte(

pub fn writeByteNTimes(
writer_ctx: anytype,
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
byte: u8,
n: usize,
) writer_impl.WriteError!void {
Expand All @@ -308,7 +307,7 @@ pub fn writeByteNTimes(

pub inline fn writeInt(
writer_ctx: anytype,
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
comptime T: type,
value: T,
endian: std.builtin.Endian,
Expand All @@ -325,7 +324,7 @@ pub inline fn writeInt(

pub fn writeStruct(
writer_ctx: anytype,
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
value: anytype,
) writer_impl.WriteError!void {
// Only extern and packed structs have defined in-memory layout.
Expand All @@ -349,30 +348,30 @@ pub fn Seekable(comptime T: type) type {

pub fn seekTo(
seek_ctx: anytype,
seek_impl: Impl(@TypeOf(seek_ctx), Seekable),
seek_impl: Impl(Seekable, @TypeOf(seek_ctx)),
pos: u64,
) seek_impl.SeekError!void {
return @errorCast(seek_impl.seekTo(seek_ctx, pos));
}

pub fn seekBy(
seek_ctx: anytype,
seek_impl: Impl(@TypeOf(seek_ctx), Seekable),
seek_impl: Impl(Seekable, @TypeOf(seek_ctx)),
amt: i64,
) seek_impl.SeekError!void {
return @errorCast(seek_impl.seekBy(seek_ctx, amt));
}

pub fn getPos(
seek_ctx: anytype,
seek_impl: Impl(@TypeOf(seek_ctx), Seekable),
seek_impl: Impl(Seekable, @TypeOf(seek_ctx)),
) seek_impl.GetSeekPosError!u64 {
return @errorCast(seek_impl.getPos(seek_ctx));
}

pub fn getEndPos(
seek_ctx: anytype,
seek_impl: Impl(@TypeOf(seek_ctx), Seekable),
seek_impl: Impl(Seekable, @TypeOf(seek_ctx)),
) seek_impl.GetSeekPosError!u64 {
return @errorCast(seek_impl.getEndPos(seek_ctx));
}
Expand Down
7 changes: 3 additions & 4 deletions examples/io/counting_writer.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const std = @import("std");
const testing = std.testing;

const zimpl = @import("zimpl");
const Impl = zimpl.Impl;
const Impl = @import("zimpl").Impl;

const io = @import("../io.zig");

pub fn CountingWriter(
comptime ChildCtx: type,
comptime child_impl: Impl(ChildCtx, io.Writer),
comptime child_impl: Impl(io.Writer, ChildCtx),
) type {
return struct {
child_ctx: ChildCtx,
Expand All @@ -26,7 +25,7 @@ pub fn CountingWriter(

pub fn countingWriter(
child_ctx: anytype,
child_impl: Impl(@TypeOf(child_ctx), io.Writer),
child_impl: Impl(io.Writer, @TypeOf(child_ctx)),
) CountingWriter(@TypeOf(child_ctx), child_impl) {
return .{ .child_ctx = child_ctx };
}
Expand Down
5 changes: 2 additions & 3 deletions examples/iterator.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const std = @import("std");
const testing = std.testing;

const zimpl = @import("zimpl");
const Impl = zimpl.Impl;
const Impl = @import("zimpl").Impl;

fn Iterator(comptime Data: type) fn (type) type {
return struct {
Expand All @@ -18,7 +17,7 @@ pub fn apply(
comptime T: type,
comptime f: fn (*T) void,
iter: anytype,
impl: Impl(@TypeOf(iter), Iterator(T)),
impl: Impl(Iterator(T), @TypeOf(iter)),
) void {
var mut_iter = iter;
while (impl.next(&mut_iter)) |t| {
Expand Down
2 changes: 1 addition & 1 deletion examples/read_file.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test "read file with std.os.fd_t" {
std.os.O.RDONLY,
0,
);
const fd_reader: Impl(std.os.fd_t, io.Reader) = .{
const fd_reader: Impl(io.Reader, std.os.fd_t) = .{
.read = std.os.read,
.ReadError = std.os.ReadError,
};
Expand Down
2 changes: 1 addition & 1 deletion src/zimpl.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Type = @import("std").builtin.Type;

pub fn Impl(comptime T: type, comptime Ifc: fn (type) type) type {
pub fn Impl(comptime Ifc: fn (type) type, comptime T: type) type {
switch (@typeInfo(Unwrap(T))) {
.Struct, .Union, .Enum, .Opaque => {},
else => return Ifc(T),
Expand Down
4 changes: 2 additions & 2 deletions why.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const Server = struct {
pub fn poll(
self: *Self,
handler_ctx: anytype,
handler_impl: Impl(@TypeOf(handler_ctx), Handler)
handler_impl: Impl(Handler, @TypeOf(handler_ctx))
) void {
try self.pollSockets();
while (self.getEvent()) |evt| {
Expand All @@ -159,7 +159,7 @@ var server = Server{};
var handler = MyHandler{};
try server.listen(8080);
while (true) {
// Impl(*MyHandler, Handler) can be default constructed because MyHandler
// Impl(Handler, *MyHandler) can be default constructed because MyHandler
// has onOpen, onMessage, and onClose member functions
try server.poll(&handler, .{});
}
Expand Down

0 comments on commit dac2f07

Please sign in to comment.