Skip to content

Commit f208b66

Browse files
switched default error sets to anyerror
1 parent 17aaeed commit f208b66

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ exists.
3636
// An interface
3737
pub fn Reader(comptime T: type) type {
3838
return struct {
39-
ReadError: type = error{},
39+
ReadError: type = anyerror,
4040
read: fn (reader_ctx: T, buffer: []u8) anyerror!usize,
4141
};
4242
}
@@ -81,7 +81,9 @@ test "define and use a reader" {
8181
buffer: []const u8,
8282
pos: usize = 0,
8383
84-
pub fn read(self: *@This(), out_buffer: []u8) error{}!usize {
84+
pub const ReadError = error{};
85+
86+
pub fn read(self: *@This(), out_buffer: []u8) ReadError!usize {
8587
const len = @min(self.buffer[self.pos..].len, out_buffer.len);
8688
@memcpy(out_buffer[0..len], self.buffer[self.pos..][0..len]);
8789
self.pos += len;

examples/io.zig

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ pub const countingWriter = @import("io/counting_writer.zig").countingWriter;
1313
pub const null_writer = NullWriter{};
1414

1515
pub const NullWriter = struct {
16-
pub fn write(_: NullWriter, data: []const u8) error{}!usize {
16+
pub const WriteError = error{};
17+
pub fn write(_: NullWriter, data: []const u8) WriteError!usize {
1718
return data.len;
1819
}
1920
};
@@ -24,7 +25,7 @@ test "null_writer" {
2425

2526
pub fn Reader(comptime T: type) type {
2627
return struct {
27-
ReadError: type = error{},
28+
ReadError: type = anyerror,
2829
read: fn (reader_ctx: T, buffer: []u8) anyerror!usize,
2930
};
3031
}
@@ -94,8 +95,6 @@ pub inline fn streamUntilDelimiter(
9495
if (byte == delimiter) return;
9596
try writeByte(writer_ctx, writer_impl, byte);
9697
}
97-
// Can not throw `error.StreamTooLong` since there are no
98-
// boundary.
9998
}
10099
}
101100

@@ -206,7 +205,6 @@ pub inline fn readStruct(
206205
reader_impl: Impl(Reader, @TypeOf(reader_ctx)),
207206
comptime T: type,
208207
) (reader_impl.ReadError || error{EndOfStream})!T {
209-
// Only extern and packed structs have defined in-memory layout.
210208
comptime assert(@typeInfo(T).Struct.layout != .Auto);
211209
var res: [1]T = undefined;
212210
try readNoEof(reader_ctx, reader_impl, mem.sliceAsBytes(res[0..]));
@@ -231,11 +229,6 @@ pub inline fn readEnum(
231229
comptime Enum: type,
232230
endian: std.builtin.Endian,
233231
) (reader_impl.ReadError || error{ EndOfStream, InvalidValue })!Enum {
234-
const E = error{
235-
/// An integer was read, but it did not match any of the tags
236-
/// in the supplied enum.
237-
InvalidValue,
238-
};
239232
const type_info = @typeInfo(Enum).Enum;
240233
const tag = try readInt(
241234
reader_ctx,
@@ -250,12 +243,12 @@ pub inline fn readEnum(
250243
}
251244
}
252245

253-
return E.InvalidValue;
246+
return error.InvalidValue;
254247
}
255248

256249
pub fn Writer(comptime T: type) type {
257250
return struct {
258-
WriteError: type = error{},
251+
WriteError: type = anyerror,
259252
write: fn (writer_ctx: T, bytes: []const u8) anyerror!usize,
260253
};
261254
}
@@ -327,19 +320,18 @@ pub fn writeStruct(
327320
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
328321
value: anytype,
329322
) writer_impl.WriteError!void {
330-
// Only extern and packed structs have defined in-memory layout.
331323
comptime assert(@typeInfo(@TypeOf(value)).Struct.layout != .Auto);
332324
return writeAll(writer_ctx, writer_impl, mem.asBytes(&value));
333325
}
334326

335327
pub fn Seekable(comptime T: type) type {
336328
return struct {
337-
SeekError: type = error{},
329+
SeekError: type = anyerror,
338330

339331
seekTo: fn (seek_ctx: T, pos: u64) anyerror!void,
340332
seekBy: fn (seek_ctx: T, amt: i64) anyerror!void,
341333

342-
GetSeekPosError: type = error{},
334+
GetSeekPosError: type = anyerror,
343335

344336
getPos: fn (seek_ctx: T) anyerror!u64,
345337
getEndPos: fn (seek_ctx: T) anyerror!u64,

examples/io/FixedBufferReader.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const io = @import("../io.zig");
66
buffer: []const u8,
77
pos: usize = 0,
88

9-
pub fn read(self: *@This(), out_buffer: []u8) error{}!usize {
9+
pub const ReadError = error{};
10+
11+
pub fn read(self: *@This(), out_buffer: []u8) ReadError!usize {
1012
const len = @min(self.buffer[self.pos..].len, out_buffer.len);
1113
@memcpy(
1214
out_buffer[0..len],

0 commit comments

Comments
 (0)