aboutsummaryrefslogtreecommitdiff
path: root/lib/std/json/write_stream.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-03-10 15:27:45 -0400
committerAndrew Kelley <andrew@ziglang.org>2020-03-10 15:32:32 -0400
commitba0e3be5cfa2f60f2f9d2a4eb319408f972796c2 (patch)
treeda35c17d0067a463b521163f624f3d109b8050f4 /lib/std/json/write_stream.zig
parent1ad831a0ef22f354d4e1dd5073456a0683249846 (diff)
downloadzig-ba0e3be5cfa2f60f2f9d2a4eb319408f972796c2.tar.gz
zig-ba0e3be5cfa2f60f2f9d2a4eb319408f972796c2.zip
(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.
Diffstat (limited to 'lib/std/json/write_stream.zig')
-rw-r--r--lib/std/json/write_stream.zig32
1 files changed, 16 insertions, 16 deletions
diff --git a/lib/std/json/write_stream.zig b/lib/std/json/write_stream.zig
index 213b6768d4..6d88c8b6da 100644
--- a/lib/std/json/write_stream.zig
+++ b/lib/std/json/write_stream.zig
@@ -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);
}
}