(breaking) rework stream abstractions

The main goal here is to make the function pointers comptime, so that we
don't have to do the crazy stuff with async function frames.

Since InStream, OutStream, and SeekableStream are already generic
across error sets, it's not really worse to make them generic across the
vtable as well.

See #764 for the open issue acknowledging that using generics for these
abstractions is a design flaw.

See #130 for the efforts to make these abstractions non-generic.

This commit also changes the OutStream API so that `write` returns
number of bytes written, and `writeAll` is the one that loops until the
whole buffer is written.
This commit is contained in:
Andrew Kelley
2020-03-10 15:27:45 -04:00
parent 1ad831a0ef
commit ba0e3be5cf
24 changed files with 748 additions and 772 deletions

View File

@@ -30,11 +30,11 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
/// The string used as spacing.
space: []const u8 = " ",
stream: *OutStream,
stream: OutStream,
state_index: usize,
state: [max_depth]State,
pub fn init(stream: *OutStream) Self {
pub fn init(stream: OutStream) Self {
var self = Self{
.stream = stream,
.state_index = 1,
@@ -90,8 +90,8 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
self.pushState(.Value);
try self.indent();
try self.writeEscapedString(name);
try self.stream.write(":");
try self.stream.write(self.space);
try self.stream.writeAll(":");
try self.stream.writeAll(self.space);
},
}
}
@@ -134,16 +134,16 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
pub fn emitNull(self: *Self) !void {
assert(self.state[self.state_index] == State.Value);
try self.stream.write("null");
try self.stream.writeAll("null");
self.popState();
}
pub fn emitBool(self: *Self, value: bool) !void {
assert(self.state[self.state_index] == State.Value);
if (value) {
try self.stream.write("true");
try self.stream.writeAll("true");
} else {
try self.stream.write("false");
try self.stream.writeAll("false");
}
self.popState();
}
@@ -188,13 +188,13 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
try self.stream.writeByte('"');
for (string) |s| {
switch (s) {
'"' => try self.stream.write("\\\""),
'\t' => try self.stream.write("\\t"),
'\r' => try self.stream.write("\\r"),
'\n' => try self.stream.write("\\n"),
8 => try self.stream.write("\\b"),
12 => try self.stream.write("\\f"),
'\\' => try self.stream.write("\\\\"),
'"' => try self.stream.writeAll("\\\""),
'\t' => try self.stream.writeAll("\\t"),
'\r' => try self.stream.writeAll("\\r"),
'\n' => try self.stream.writeAll("\\n"),
8 => try self.stream.writeAll("\\b"),
12 => try self.stream.writeAll("\\f"),
'\\' => try self.stream.writeAll("\\\\"),
else => try self.stream.writeByte(s),
}
}
@@ -231,10 +231,10 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
fn indent(self: *Self) !void {
assert(self.state_index >= 1);
try self.stream.write(self.newline);
try self.stream.writeAll(self.newline);
var i: usize = 0;
while (i < self.state_index - 1) : (i += 1) {
try self.stream.write(self.one_indent);
try self.stream.writeAll(self.one_indent);
}
}