aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-02-05 18:09:13 -0500
committerAndrew Kelley <superjoe30@gmail.com>2018-02-05 18:09:13 -0500
commitf99b8b006fe458ee717acc6c8de6170fc453acb7 (patch)
tree672b663987df03e6b70e384b39b565272c9efbf1 /std
parent6940212ecbef349e449441e1fd813116865d3a5f (diff)
downloadzig-f99b8b006fe458ee717acc6c8de6170fc453acb7.tar.gz
zig-f99b8b006fe458ee717acc6c8de6170fc453acb7.zip
error sets - fix most std lib compile errors
Diffstat (limited to 'std')
-rw-r--r--std/fmt/index.zig8
-rw-r--r--std/io.zig39
-rw-r--r--std/io_test.zig4
3 files changed, 30 insertions, 21 deletions
diff --git a/std/fmt/index.zig b/std/fmt/index.zig
index 50f0013852..407ffd901b 100644
--- a/std/fmt/index.zig
+++ b/std/fmt/index.zig
@@ -439,9 +439,9 @@ pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) !T {
test "fmt.parseInt" {
assert((parseInt(i32, "-10", 10) catch unreachable) == -10);
assert((parseInt(i32, "+10", 10) catch unreachable) == 10);
- assert(if (parseInt(i32, " 10", 10)) |_| false else |err| err == error.InvalidChar);
- assert(if (parseInt(i32, "10 ", 10)) |_| false else |err| err == error.InvalidChar);
- assert(if (parseInt(u32, "-10", 10)) |_| false else |err| err == error.InvalidChar);
+ assert(if (parseInt(i32, " 10", 10)) |_| false else |err| err == error.InvalidCharacter);
+ assert(if (parseInt(i32, "10 ", 10)) |_| false else |err| err == error.InvalidCharacter);
+ assert(if (parseInt(u32, "-10", 10)) |_| false else |err| err == error.InvalidCharacter);
assert((parseInt(u8, "255", 10) catch unreachable) == 255);
assert(if (parseInt(u8, "256", 10)) |_| false else |err| err == error.Overflow);
}
@@ -538,7 +538,7 @@ fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, width: u
test "parse u64 digit too big" {
_ = parseUnsigned(u64, "123a", 10) catch |err| {
- if (err == error.InvalidChar) return;
+ if (err == error.InvalidCharacter) return;
unreachable;
};
unreachable;
diff --git a/std/io.zig b/std/io.zig
index 50b70db645..842c30a0e4 100644
--- a/std/io.zig
+++ b/std/io.zig
@@ -550,21 +550,24 @@ pub fn readFileAllocExtra(path: []const u8, allocator: &mem.Allocator, extra_len
return buf;
}
-pub const BufferedInStream = BufferedInStreamCustom(os.page_size);
+pub fn BufferedInStream(comptime Error: type) type {
+ return BufferedInStreamCustom(os.page_size, Error);
+}
-pub fn BufferedInStreamCustom(comptime buffer_size: usize) type {
+pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type) type {
return struct {
const Self = this;
+ const Stream = InStream(Error);
- pub stream: InStream,
+ pub stream: Stream,
- unbuffered_in_stream: &InStream,
+ unbuffered_in_stream: &Stream,
buffer: [buffer_size]u8,
start_index: usize,
end_index: usize,
- pub fn init(unbuffered_in_stream: &InStream) Self {
+ pub fn init(unbuffered_in_stream: &Stream) Self {
return Self {
.unbuffered_in_stream = unbuffered_in_stream,
.buffer = undefined,
@@ -576,13 +579,13 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize) type {
.start_index = buffer_size,
.end_index = buffer_size,
- .stream = InStream {
+ .stream = Stream {
.readFn = readFn,
},
};
}
- fn readFn(in_stream: &InStream, dest: []u8) !usize {
+ fn readFn(in_stream: &Stream, dest: []u8) !usize {
const self = @fieldParentPtr(Self, "stream", in_stream);
var dest_index: usize = 0;
@@ -621,25 +624,28 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize) type {
};
}
-pub const BufferedOutStream = BufferedOutStreamCustom(os.page_size);
+pub fn BufferedOutStream(comptime Error: type) type {
+ return BufferedOutStreamCustom(os.page_size, Error);
+}
-pub fn BufferedOutStreamCustom(comptime buffer_size: usize) type {
+pub fn BufferedOutStreamCustom(comptime buffer_size: usize, comptime Error: type) type {
return struct {
const Self = this;
+ const Stream = OutStream(Error);
- pub stream: OutStream,
+ pub stream: Stream,
- unbuffered_out_stream: &OutStream,
+ unbuffered_out_stream: &Stream,
buffer: [buffer_size]u8,
index: usize,
- pub fn init(unbuffered_out_stream: &OutStream) Self {
+ pub fn init(unbuffered_out_stream: &Stream) Self {
return Self {
.unbuffered_out_stream = unbuffered_out_stream,
.buffer = undefined,
.index = 0,
- .stream = OutStream {
+ .stream = Stream {
.writeFn = writeFn,
},
};
@@ -653,7 +659,7 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize) type {
self.index = 0;
}
- fn writeFn(out_stream: &OutStream, bytes: []const u8) !void {
+ fn writeFn(out_stream: &Stream, bytes: []const u8) !void {
const self = @fieldParentPtr(Self, "stream", out_stream);
if (bytes.len >= self.buffer.len) {
@@ -680,7 +686,10 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize) type {
/// Implementation of OutStream trait for Buffer
pub const BufferOutStream = struct {
buffer: &Buffer,
- stream: OutStream,
+ stream: Stream,
+
+ pub const Error = error{OutOfMemory};
+ pub const Stream = OutStream(Error);
pub fn init(buffer: &Buffer) BufferOutStream {
return BufferOutStream {
diff --git a/std/io_test.zig b/std/io_test.zig
index 1767a546ea..ea4b816b01 100644
--- a/std/io_test.zig
+++ b/std/io_test.zig
@@ -17,7 +17,7 @@ test "write a file, read it, then delete it" {
defer file.close();
var file_out_stream = io.FileOutStream.init(&file);
- var buf_stream = io.BufferedOutStream.init(&file_out_stream.stream);
+ var buf_stream = io.BufferedOutStream(io.FileOutStream.Error).init(&file_out_stream.stream);
const st = &buf_stream.stream;
try st.print("begin");
try st.write(data[0..]);
@@ -33,7 +33,7 @@ test "write a file, read it, then delete it" {
assert(file_size == expected_file_size);
var file_in_stream = io.FileInStream.init(&file);
- var buf_stream = io.BufferedInStream.init(&file_in_stream.stream);
+ var buf_stream = io.BufferedInStream(io.FileInStream.Error).init(&file_in_stream.stream);
const st = &buf_stream.stream;
const contents = try st.readAllAlloc(allocator, 2 * 1024);
defer allocator.free(contents);