| ... | @@ -0,0 +1,46 @@ |
| 1 | //! `std.Io.Writer.Hashing` is recommended to use over this but that type owns the hasher so this allows you to print into an existing Hash |
| 2 | |
| 3 | const std = @import("std"); |
| 4 | const nio = @import("./nio.zig"); |
| 5 | const builtin = @import("builtin"); |
| 6 | |
| 7 | const sys = switch (builtin.target.os.tag) { |
| 8 | .linux => @import("sys-linux"), |
| 9 | .macos => @import("sys-darwin"), |
| 10 | else => unreachable, |
| 11 | }; |
| 12 | |
| 13 | pub fn HashWriter(comptime Hash: type) type { |
| 14 | return struct { |
| 15 | hasher: *Hash, |
| 16 | |
| 17 | pub fn init(hasher: *Hash) @This() { |
| 18 | return .{ |
| 19 | .hasher = hasher, |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | const W = nio.Writable(@This(), ._const); |
| 24 | pub const writeAll = W.writeAll; |
| 25 | pub const writevAll = W.writevAll; |
| 26 | pub const writeByteNTimes = W.writeByteNTimes; |
| 27 | pub const writeNTimes = W.writeNTimes; |
| 28 | pub const writeInt = W.writeInt; |
| 29 | pub const writeStruct = W.writeStruct; |
| 30 | pub const writeIntPretty = W.writeIntPretty; |
| 31 | pub const print = W.print; |
| 32 | |
| 33 | pub const WriteError = error{}; |
| 34 | |
| 35 | pub fn write(self: *const @This(), bytes: []const u8) WriteError!usize { |
| 36 | self.hasher.update(bytes); |
| 37 | return bytes.len; |
| 38 | } |
| 39 | |
| 40 | pub fn writev(self: *const @This(), iovec: []const sys.struct_iovec) WriteError!usize { |
| 41 | var count: usize = 0; |
| 42 | for (iovec) |vec| count += try write(self, vec.base[0..vec.len]); |
| 43 | return count; |
| 44 | } |
| 45 | }; |
| 46 | } |