From dac2f0708f3255cf6bcfd99b02990f8e49721e93 Mon Sep 17 00:00:00 2001 From: Aven Bross Date: Tue, 28 Nov 2023 12:52:37 +0000 Subject: [PATCH] Switched parameter order for Impl to improve clarity --- README.md | 10 +++--- examples/count.zig | 6 ++-- examples/io.zig | 57 ++++++++++++++++----------------- examples/io/counting_writer.zig | 7 ++-- examples/iterator.zig | 5 ++- examples/read_file.zig | 2 +- src/zimpl.zig | 2 +- why.md | 4 +-- 8 files changed, 44 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 8f6fcdc..7a1de0e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. @@ -45,7 +45,7 @@ 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)); @@ -53,7 +53,7 @@ pub const io = struct { 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); @@ -61,7 +61,7 @@ pub const io = struct { 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 { diff --git a/examples/count.zig b/examples/count.zig index 6eafb2b..dec4d55 100644 --- a/examples/count.zig +++ b/examples/count.zig @@ -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 { @@ -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); diff --git a/examples/io.zig b/examples/io.zig index c835c83..8e239a6 100644 --- a/examples/io.zig +++ b/examples/io.zig @@ -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"); @@ -32,7 +31,7 @@ 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)); @@ -40,7 +39,7 @@ pub inline fn read( 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); @@ -48,7 +47,7 @@ pub inline fn readAll( 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 { @@ -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); @@ -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{ @@ -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) { @@ -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..]); @@ -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; @@ -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 { @@ -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, @@ -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, @@ -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; @@ -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. @@ -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); @@ -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 { @@ -263,7 +262,7 @@ 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)); @@ -271,7 +270,7 @@ pub fn write( 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; @@ -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}; @@ -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 { @@ -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, @@ -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. @@ -349,7 +348,7 @@ 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)); @@ -357,7 +356,7 @@ pub fn seekTo( 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)); @@ -365,14 +364,14 @@ pub fn seekBy( 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)); } diff --git a/examples/io/counting_writer.zig b/examples/io/counting_writer.zig index e5b9582..4ded001 100644 --- a/examples/io/counting_writer.zig +++ b/examples/io/counting_writer.zig @@ -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, @@ -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 }; } diff --git a/examples/iterator.zig b/examples/iterator.zig index a83a0e7..4c9e940 100644 --- a/examples/iterator.zig +++ b/examples/iterator.zig @@ -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 { @@ -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| { diff --git a/examples/read_file.zig b/examples/read_file.zig index 8310697..a069d50 100644 --- a/examples/read_file.zig +++ b/examples/read_file.zig @@ -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, }; diff --git a/src/zimpl.zig b/src/zimpl.zig index bf8725f..b185d5a 100644 --- a/src/zimpl.zig +++ b/src/zimpl.zig @@ -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), diff --git a/why.md b/why.md index 4b618de..22e8735 100644 --- a/why.md +++ b/why.md @@ -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| { @@ -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, .{}); }