| ... | ... | @@ -249,13 +249,6 @@ pub fn OutStream(comptime WriteError: type) type { |
| 249 | 249 | }; |
| 250 | 250 | } |
| 251 | 251 | |
| 252 | | pub const noop_out_stream = &noop_out_stream_state; |
| 253 | | const NoopOutStreamError = error{}; |
| 254 | | var noop_out_stream_state = OutStream(NoopOutStreamError){ |
| 255 | | .writeFn = noop_out_stream_write, |
| 256 | | }; |
| 257 | | fn noop_out_stream_write(self: *OutStream(NoopOutStreamError), bytes: []const u8) error{}!void {} |
| 258 | | |
| 259 | 252 | pub fn writeFile(path: []const u8, data: []const u8) !void { |
| 260 | 253 | var file = try File.openWrite(path); |
| 261 | 254 | defer file.close(); |
| ... | ... | @@ -495,6 +488,77 @@ pub const SliceOutStream = struct { |
| 495 | 488 | } |
| 496 | 489 | }; |
| 497 | 490 | |
| 491 | test "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 | |
| 500 | var null_out_stream_state = NullOutStream.init(); |
| 501 | pub const null_out_stream = &null_out_stream_state.stream; |
| 502 | |
| 503 | /// An OutStream that doesn't write to anything. |
| 504 | pub 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 | |
| 519 | test "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. |
| 526 | pub 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 | |
| 552 | test "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 | |
| 498 | 562 | pub fn BufferedOutStream(comptime Error: type) type { |
| 499 | 563 | return BufferedOutStreamCustom(os.page_size, Error); |
| 500 | 564 | } |
| ... | ... | @@ -639,5 +703,3 @@ pub fn readLine(buf: []u8) !usize { |
| 639 | 703 | } |
| 640 | 704 | } |
| 641 | 705 | } |
| 642 | | |
| 643 | | |