Skip to content

Commit dac2f07

Browse files
Switched parameter order for Impl to improve clarity
1 parent ef377d0 commit dac2f07

8 files changed

Lines changed: 44 additions & 49 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The `zimpl` module is ~30 lines of code and exposes one public
88
declaration: `Impl`.
99

1010
```Zig
11-
pub fn Impl(comptime T: type, comptime Ifc: fn (type) type) type { ... }
11+
pub fn Impl(comptime Ifc: fn (type) type, comptime T: type) type { ... }
1212
```
1313

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

2626
## Return value
2727

28-
The type `Impl(T, Ifc)` is a struct type with the same fields
28+
The type `Impl(Ifc, T)` is a struct type with the same fields
2929
as `Ifc(T)`, but with the default value of each field set equal to
3030
the declaration of `Unwrap(T)` of the same name, if such a declaration
3131
exists.
@@ -45,23 +45,23 @@ pub fn Reader(comptime T: type) type {
4545
pub const io = struct {
4646
pub inline fn read(
4747
reader_ctx: anytype,
48-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
48+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
4949
buffer: []u8,
5050
) reader_impl.ReadError!usize {
5151
return @errorCast(reader_impl.read(reader_ctx, buffer));
5252
}
5353
5454
pub inline fn readAll(
5555
reader_ctx: anytype,
56-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
56+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
5757
buffer: []u8,
5858
) reader_impl.ReadError!usize {
5959
return readAtLeast(reader_ctx, reader_impl, buffer, buffer.len);
6060
}
6161
6262
pub inline fn readAtLeast(
6363
reader_ctx: anytype,
64-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
64+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
6565
buffer: []u8,
6666
len: usize,
6767
) reader_impl.ReadError!usize {

examples/count.zig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
const std = @import("std");
22
const testing = std.testing;
33

4-
const zimpl = @import("zimpl");
5-
const Impl = zimpl.Impl;
6-
const PtrChild = zimpl.PtrChild;
4+
const Impl = @import("zimpl").Impl;
75

86
fn Counter(comptime T: type) type {
97
return struct {
@@ -14,7 +12,7 @@ fn Counter(comptime T: type) type {
1412

1513
pub fn countToTen(
1614
ctr_ctx: anytype,
17-
comptime ctr_impl: Impl(@TypeOf(ctr_ctx), Counter),
15+
ctr_impl: Impl(Counter, @TypeOf(ctr_ctx)),
1816
) void {
1917
while (ctr_impl.read(ctr_ctx) < 10) {
2018
ctr_impl.increment(ctr_ctx);

examples/io.zig

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const native_endian = @import("builtin").target.cpu.arch.endian();
33
const mem = std.mem;
44
const assert = std.debug.assert;
55

6-
const zimpl = @import("zimpl");
7-
const Impl = zimpl.Impl;
6+
const Impl = @import("zimpl").Impl;
87

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

3332
pub inline fn read(
3433
reader_ctx: anytype,
35-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
34+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
3635
buffer: []u8,
3736
) reader_impl.ReadError!usize {
3837
return @errorCast(reader_impl.read(reader_ctx, buffer));
3938
}
4039

4140
pub inline fn readAll(
4241
reader_ctx: anytype,
43-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
42+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
4443
buffer: []u8,
4544
) reader_impl.ReadError!usize {
4645
return readAtLeast(reader_ctx, reader_impl, buffer, buffer.len);
4746
}
4847

4948
pub inline fn readAtLeast(
5049
reader_ctx: anytype,
51-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
50+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
5251
buffer: []u8,
5352
len: usize,
5453
) reader_impl.ReadError!usize {
@@ -64,7 +63,7 @@ pub inline fn readAtLeast(
6463

6564
pub inline fn readNoEof(
6665
reader_ctx: anytype,
67-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
66+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
6867
buf: []u8,
6968
) (reader_impl.ReadError || error{EndOfStream})!void {
7069
const amt_read = try readAll(reader_ctx, reader_impl, buf);
@@ -73,9 +72,9 @@ pub inline fn readNoEof(
7372

7473
pub inline fn streamUntilDelimiter(
7574
reader_ctx: anytype,
76-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
75+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
7776
writer_ctx: anytype,
78-
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
77+
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
7978
delimiter: u8,
8079
optional_max_size: ?usize,
8180
) (reader_impl.ReadError || writer_impl.WriteError || error{
@@ -102,7 +101,7 @@ pub inline fn streamUntilDelimiter(
102101

103102
pub inline fn skipUntilDelimiterOrEof(
104103
reader_ctx: anytype,
105-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
104+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
106105
delimiter: u8,
107106
) reader_impl.ReadError!void {
108107
while (true) {
@@ -116,7 +115,7 @@ pub inline fn skipUntilDelimiterOrEof(
116115

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

127126
pub inline fn readByteSigned(
128127
reader_ctx: anytype,
129-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
128+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
130129
) (reader_impl.ReadError || error{EndOfStream})!i8 {
131130
return @as(i8, @bitCast(try readByte(reader_ctx, reader_impl)));
132131
}
133132

134133
pub inline fn readBytesNoEof(
135134
reader_ctx: anytype,
136-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
135+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
137136
comptime num_bytes: usize,
138137
) (reader_impl.ReadError || error{EndOfStream})![num_bytes]u8 {
139138
var bytes: [num_bytes]u8 = undefined;
@@ -143,7 +142,7 @@ pub inline fn readBytesNoEof(
143142

144143
pub inline fn readInt(
145144
reader_ctx: anytype,
146-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
145+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
147146
comptime T: type,
148147
endian: std.builtin.Endian,
149148
) (reader_impl.ReadError || error{EndOfStream})!T {
@@ -157,7 +156,7 @@ pub inline fn readInt(
157156

158157
pub inline fn readVarInt(
159158
reader_ctx: anytype,
160-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
159+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
161160
comptime ReturnType: type,
162161
endian: std.builtin.Endian,
163162
size: usize,
@@ -171,7 +170,7 @@ pub inline fn readVarInt(
171170

172171
pub inline fn skipBytes(
173172
reader_ctx: anytype,
174-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
173+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
175174
num_bytes: u64,
176175
comptime options: struct {
177176
buf_size: usize = 512,
@@ -189,7 +188,7 @@ pub inline fn skipBytes(
189188

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

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

217216
pub inline fn readStructBig(
218217
reader_ctx: anytype,
219-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
218+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
220219
comptime T: type,
221220
) (reader_impl.ReadError || error{EndOfStream})!T {
222221
var res = try readStruct(reader_ctx, reader_impl, T);
@@ -228,7 +227,7 @@ pub inline fn readStructBig(
228227

229228
pub inline fn readEnum(
230229
reader_ctx: anytype,
231-
reader_impl: Impl(@TypeOf(reader_ctx), Reader),
230+
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
232231
comptime Enum: type,
233232
endian: std.builtin.Endian,
234233
) (reader_impl.ReadError || error{ EndOfStream, InvalidValue })!Enum {
@@ -263,15 +262,15 @@ pub fn Writer(comptime T: type) type {
263262

264263
pub fn write(
265264
writer_ctx: anytype,
266-
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
265+
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
267266
bytes: []const u8,
268267
) writer_impl.WriteError!usize {
269268
return @errorCast(writer_impl.write(writer_ctx, bytes));
270269
}
271270

272271
pub fn writeAll(
273272
writer_ctx: anytype,
274-
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
273+
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
275274
bytes: []const u8,
276275
) writer_impl.WriteError!void {
277276
var index: usize = 0;
@@ -282,7 +281,7 @@ pub fn writeAll(
282281

283282
pub fn writeByte(
284283
writer_ctx: anytype,
285-
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
284+
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
286285
byte: u8,
287286
) writer_impl.WriteError!void {
288287
const array = [1]u8{byte};
@@ -291,7 +290,7 @@ pub fn writeByte(
291290

292291
pub fn writeByteNTimes(
293292
writer_ctx: anytype,
294-
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
293+
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
295294
byte: u8,
296295
n: usize,
297296
) writer_impl.WriteError!void {
@@ -308,7 +307,7 @@ pub fn writeByteNTimes(
308307

309308
pub inline fn writeInt(
310309
writer_ctx: anytype,
311-
writer_impl: Impl(@TypeOf(writer_ctx), Writer),
310+
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
312311
comptime T: type,
313312
value: T,
314313
endian: std.builtin.Endian,
@@ -325,7 +324,7 @@ pub inline fn writeInt(
325324

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

350349
pub fn seekTo(
351350
seek_ctx: anytype,
352-
seek_impl: Impl(@TypeOf(seek_ctx), Seekable),
351+
seek_impl: Impl(Seekable, @TypeOf(seek_ctx)),
353352
pos: u64,
354353
) seek_impl.SeekError!void {
355354
return @errorCast(seek_impl.seekTo(seek_ctx, pos));
356355
}
357356

358357
pub fn seekBy(
359358
seek_ctx: anytype,
360-
seek_impl: Impl(@TypeOf(seek_ctx), Seekable),
359+
seek_impl: Impl(Seekable, @TypeOf(seek_ctx)),
361360
amt: i64,
362361
) seek_impl.SeekError!void {
363362
return @errorCast(seek_impl.seekBy(seek_ctx, amt));
364363
}
365364

366365
pub fn getPos(
367366
seek_ctx: anytype,
368-
seek_impl: Impl(@TypeOf(seek_ctx), Seekable),
367+
seek_impl: Impl(Seekable, @TypeOf(seek_ctx)),
369368
) seek_impl.GetSeekPosError!u64 {
370369
return @errorCast(seek_impl.getPos(seek_ctx));
371370
}
372371

373372
pub fn getEndPos(
374373
seek_ctx: anytype,
375-
seek_impl: Impl(@TypeOf(seek_ctx), Seekable),
374+
seek_impl: Impl(Seekable, @TypeOf(seek_ctx)),
376375
) seek_impl.GetSeekPosError!u64 {
377376
return @errorCast(seek_impl.getEndPos(seek_ctx));
378377
}

examples/io/counting_writer.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
const std = @import("std");
22
const testing = std.testing;
33

4-
const zimpl = @import("zimpl");
5-
const Impl = zimpl.Impl;
4+
const Impl = @import("zimpl").Impl;
65

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

98
pub fn CountingWriter(
109
comptime ChildCtx: type,
11-
comptime child_impl: Impl(ChildCtx, io.Writer),
10+
comptime child_impl: Impl(io.Writer, ChildCtx),
1211
) type {
1312
return struct {
1413
child_ctx: ChildCtx,
@@ -26,7 +25,7 @@ pub fn CountingWriter(
2625

2726
pub fn countingWriter(
2827
child_ctx: anytype,
29-
child_impl: Impl(@TypeOf(child_ctx), io.Writer),
28+
child_impl: Impl(io.Writer, @TypeOf(child_ctx)),
3029
) CountingWriter(@TypeOf(child_ctx), child_impl) {
3130
return .{ .child_ctx = child_ctx };
3231
}

examples/iterator.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const std = @import("std");
22
const testing = std.testing;
33

4-
const zimpl = @import("zimpl");
5-
const Impl = zimpl.Impl;
4+
const Impl = @import("zimpl").Impl;
65

76
fn Iterator(comptime Data: type) fn (type) type {
87
return struct {
@@ -18,7 +17,7 @@ pub fn apply(
1817
comptime T: type,
1918
comptime f: fn (*T) void,
2019
iter: anytype,
21-
impl: Impl(@TypeOf(iter), Iterator(T)),
20+
impl: Impl(Iterator(T), @TypeOf(iter)),
2221
) void {
2322
var mut_iter = iter;
2423
while (impl.next(&mut_iter)) |t| {

examples/read_file.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test "read file with std.os.fd_t" {
2020
std.os.O.RDONLY,
2121
0,
2222
);
23-
const fd_reader: Impl(std.os.fd_t, io.Reader) = .{
23+
const fd_reader: Impl(io.Reader, std.os.fd_t) = .{
2424
.read = std.os.read,
2525
.ReadError = std.os.ReadError,
2626
};

src/zimpl.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Type = @import("std").builtin.Type;
22

3-
pub fn Impl(comptime T: type, comptime Ifc: fn (type) type) type {
3+
pub fn Impl(comptime Ifc: fn (type) type, comptime T: type) type {
44
switch (@typeInfo(Unwrap(T))) {
55
.Struct, .Union, .Enum, .Opaque => {},
66
else => return Ifc(T),

why.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const Server = struct {
140140
pub fn poll(
141141
self: *Self,
142142
handler_ctx: anytype,
143-
handler_impl: Impl(@TypeOf(handler_ctx), Handler)
143+
handler_impl: Impl(Handler, @TypeOf(handler_ctx))
144144
) void {
145145
try self.pollSockets();
146146
while (self.getEvent()) |evt| {
@@ -159,7 +159,7 @@ var server = Server{};
159159
var handler = MyHandler{};
160160
try server.listen(8080);
161161
while (true) {
162-
// Impl(*MyHandler, Handler) can be default constructed because MyHandler
162+
// Impl(Handler, *MyHandler) can be default constructed because MyHandler
163163
// has onOpen, onMessage, and onClose member functions
164164
try server.poll(&handler, .{});
165165
}

0 commit comments

Comments
 (0)