Skip to content

Commit

Permalink
switched default error sets to anyerror
Browse files Browse the repository at this point in the history
  • Loading branch information
permutationlock committed Nov 29, 2023
1 parent 17aaeed commit f208b66
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ exists.
// An interface
pub fn Reader(comptime T: type) type {
return struct {
ReadError: type = error{},
ReadError: type = anyerror,
read: fn (reader_ctx: T, buffer: []u8) anyerror!usize,
};
}
Expand Down Expand Up @@ -81,7 +81,9 @@ test "define and use a reader" {
buffer: []const u8,
pos: usize = 0,
pub fn read(self: *@This(), out_buffer: []u8) error{}!usize {
pub const ReadError = error{};
pub fn read(self: *@This(), out_buffer: []u8) ReadError!usize {
const len = @min(self.buffer[self.pos..].len, out_buffer.len);
@memcpy(out_buffer[0..len], self.buffer[self.pos..][0..len]);
self.pos += len;
Expand Down
22 changes: 7 additions & 15 deletions examples/io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub const countingWriter = @import("io/counting_writer.zig").countingWriter;
pub const null_writer = NullWriter{};

pub const NullWriter = struct {
pub fn write(_: NullWriter, data: []const u8) error{}!usize {
pub const WriteError = error{};
pub fn write(_: NullWriter, data: []const u8) WriteError!usize {
return data.len;
}
};
Expand All @@ -24,7 +25,7 @@ test "null_writer" {

pub fn Reader(comptime T: type) type {
return struct {
ReadError: type = error{},
ReadError: type = anyerror,
read: fn (reader_ctx: T, buffer: []u8) anyerror!usize,
};
}
Expand Down Expand Up @@ -94,8 +95,6 @@ pub inline fn streamUntilDelimiter(
if (byte == delimiter) return;
try writeByte(writer_ctx, writer_impl, byte);
}
// Can not throw `error.StreamTooLong` since there are no
// boundary.
}
}

Expand Down Expand Up @@ -206,7 +205,6 @@ pub inline fn readStruct(
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.
comptime assert(@typeInfo(T).Struct.layout != .Auto);
var res: [1]T = undefined;
try readNoEof(reader_ctx, reader_impl, mem.sliceAsBytes(res[0..]));
Expand All @@ -231,11 +229,6 @@ pub inline fn readEnum(
comptime Enum: type,
endian: std.builtin.Endian,
) (reader_impl.ReadError || error{ EndOfStream, InvalidValue })!Enum {
const E = error{
/// An integer was read, but it did not match any of the tags
/// in the supplied enum.
InvalidValue,
};
const type_info = @typeInfo(Enum).Enum;
const tag = try readInt(
reader_ctx,
Expand All @@ -250,12 +243,12 @@ pub inline fn readEnum(
}
}

return E.InvalidValue;
return error.InvalidValue;
}

pub fn Writer(comptime T: type) type {
return struct {
WriteError: type = error{},
WriteError: type = anyerror,
write: fn (writer_ctx: T, bytes: []const u8) anyerror!usize,
};
}
Expand Down Expand Up @@ -327,19 +320,18 @@ pub fn writeStruct(
writer_impl: Impl(Writer, @TypeOf(writer_ctx)),
value: anytype,
) writer_impl.WriteError!void {
// Only extern and packed structs have defined in-memory layout.
comptime assert(@typeInfo(@TypeOf(value)).Struct.layout != .Auto);
return writeAll(writer_ctx, writer_impl, mem.asBytes(&value));
}

pub fn Seekable(comptime T: type) type {
return struct {
SeekError: type = error{},
SeekError: type = anyerror,

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

GetSeekPosError: type = error{},
GetSeekPosError: type = anyerror,

getPos: fn (seek_ctx: T) anyerror!u64,
getEndPos: fn (seek_ctx: T) anyerror!u64,
Expand Down
4 changes: 3 additions & 1 deletion examples/io/FixedBufferReader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const io = @import("../io.zig");
buffer: []const u8,
pos: usize = 0,

pub fn read(self: *@This(), out_buffer: []u8) error{}!usize {
pub const ReadError = error{};

pub fn read(self: *@This(), out_buffer: []u8) ReadError!usize {
const len = @min(self.buffer[self.pos..].len, out_buffer.len);
@memcpy(
out_buffer[0..len],
Expand Down

0 comments on commit f208b66

Please sign in to comment.