authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-11-15 09:37:39-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-11-15 09:37:39-08:00
log2a9843de953a345cdf74a85a0ce54d97fd7a8a7b
tree1657f0c52f6fb21a3b74ef85f486a3779c2b24df
parentb8b36f3cce2d7391d3b648d7ea0c4104c0a8f0de
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Added NullOutStream and CountingOutStream (#1722)


2 files changed, 73 insertions(+), 11 deletions(-)

src-self-hosted/main.zig+2-2
...@@ -646,7 +646,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {...@@ -646,7 +646,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
646 os.exit(1);646 os.exit(1);
647 }647 }
648 if (flags.present("check")) {648 if (flags.present("check")) {
649 const anything_changed = try std.zig.render(allocator, io.noop_out_stream, &tree);649 const anything_changed = try std.zig.render(allocator, io.null_out_stream, &tree);
650 const code = if (anything_changed) u8(1) else u8(0);650 const code = if (anything_changed) u8(1) else u8(0);
651 os.exit(code);651 os.exit(code);
652 }652 }
...@@ -792,7 +792,7 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro...@@ -792,7 +792,7 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
792 }792 }
793793
794 if (check_mode) {794 if (check_mode) {
795 const anything_changed = try std.zig.render(fmt.loop.allocator, io.noop_out_stream, &tree);795 const anything_changed = try std.zig.render(fmt.loop.allocator, io.null_out_stream, &tree);
796 if (anything_changed) {796 if (anything_changed) {
797 try stderr.print("{}\n", file_path);797 try stderr.print("{}\n", file_path);
798 fmt.any_error = true;798 fmt.any_error = true;
std/io.zig+71-9
...@@ -249,13 +249,6 @@ pub fn OutStream(comptime WriteError: type) type {...@@ -249,13 +249,6 @@ pub fn OutStream(comptime WriteError: type) type {
249 };249 };
250}250}
251251
252pub const noop_out_stream = &noop_out_stream_state;
253const NoopOutStreamError = error{};
254var noop_out_stream_state = OutStream(NoopOutStreamError){
255 .writeFn = noop_out_stream_write,
256};
257fn noop_out_stream_write(self: *OutStream(NoopOutStreamError), bytes: []const u8) error{}!void {}
258
259pub fn writeFile(path: []const u8, data: []const u8) !void {252pub fn writeFile(path: []const u8, data: []const u8) !void {
260 var file = try File.openWrite(path);253 var file = try File.openWrite(path);
261 defer file.close();254 defer file.close();
...@@ -495,6 +488,77 @@ pub const SliceOutStream = struct {...@@ -495,6 +488,77 @@ pub const SliceOutStream = struct {
495 }488 }
496};489};
497490
491test "io.SliceOutStream" {
492 var buf: [255]u8 = undefined;
493 var slice_stream = SliceOutStream.init(buf[0..]);
494 const stream = &slice_stream.stream;
495
496 try stream.print("{}{}!", "Hello", "World");
497 debug.assert(mem.eql(u8, "HelloWorld!", slice_stream.getWritten()));
498}
499
500var null_out_stream_state = NullOutStream.init();
501pub const null_out_stream = &null_out_stream_state.stream;
502
503/// An OutStream that doesn't write to anything.
504pub const NullOutStream = struct {
505 pub const Error = error{};
506 pub const Stream = OutStream(Error);
507
508 pub stream: Stream,
509
510 pub fn init() NullOutStream {
511 return NullOutStream{
512 .stream = Stream{ .writeFn = writeFn },
513 };
514 }
515
516 fn writeFn(out_stream: *Stream, bytes: []const u8) Error!void {}
517};
518
519test "io.NullOutStream" {
520 var null_stream = NullOutStream.init();
521 const stream = &null_stream.stream;
522 stream.write("yay" ** 10000) catch unreachable;
523}
524
525/// An OutStream that counts how many bytes has been written to it.
526pub fn CountingOutStream(comptime OutStreamError: type) type {
527 return struct {
528 const Self = @This();
529 pub const Stream = OutStream(Error);
530 pub const Error = OutStreamError;
531
532 pub stream: Stream,
533 pub bytes_written: usize,
534 child_stream: *Stream,
535
536 pub fn init(child_stream: *Stream) Self {
537 return Self{
538 .stream = Stream{ .writeFn = writeFn },
539 .bytes_written = 0,
540 .child_stream = child_stream,
541 };
542 }
543
544 fn writeFn(out_stream: *Stream, bytes: []const u8) OutStreamError!void {
545 const self = @fieldParentPtr(Self, "stream", out_stream);
546 try self.child_stream.write(bytes);
547 self.bytes_written += bytes.len;
548 }
549 };
550}
551
552test "io.CountingOutStream" {
553 var null_stream = NullOutStream.init();
554 var counting_stream = CountingOutStream(NullOutStream.Error).init(&null_stream.stream);
555 const stream = &counting_stream.stream;
556
557 const bytes = "yay" ** 10000;
558 stream.write(bytes) catch unreachable;
559 debug.assert(counting_stream.bytes_written == bytes.len);
560}
561
498pub fn BufferedOutStream(comptime Error: type) type {562pub fn BufferedOutStream(comptime Error: type) type {
499 return BufferedOutStreamCustom(os.page_size, Error);563 return BufferedOutStreamCustom(os.page_size, Error);
500}564}
...@@ -639,5 +703,3 @@ pub fn readLine(buf: []u8) !usize {...@@ -639,5 +703,3 @@ pub fn readLine(buf: []u8) !usize {
639 }703 }
640 }704 }
641}705}
642
643