authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-08 14:48:16 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-08 14:48:16 -07:00
log181ea08310f97273d8e774a3415987e03ae606f9
tree8f892137a0181d7c0b4da4b21864e6f861fe4515
parentb85800a14c8dd942da21ad8765cd9c991c57b770
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add HashWriter


2 files changed, 48 insertions(+), 0 deletions(-)

hash_writer.zig created+46
...@@ -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
3const std = @import("std");
4const nio = @import("./nio.zig");
5const builtin = @import("builtin");
6
7const sys = switch (builtin.target.os.tag) {
8 .linux => @import("sys-linux"),
9 .macos => @import("sys-darwin"),
10 else => unreachable,
11};
12
13pub 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}
nio.zig+2
...@@ -444,6 +444,8 @@ pub const CountingReader = @import("./counting_reader.zig").CountingReader;...@@ -444,6 +444,8 @@ pub const CountingReader = @import("./counting_reader.zig").CountingReader;
444444
445pub const LimitedReader = @import("./limited_reader.zig").LimitedReader;445pub const LimitedReader = @import("./limited_reader.zig").LimitedReader;
446446
447pub const HashWriter = @import("./hash_writer.zig").HashWriter;
448
447pub const crypto_random: std.Random = .{449pub const crypto_random: std.Random = .{
448 .ptr = undefined,450 .ptr = undefined,
449 .fillFn = getrandomFill,451 .fillFn = getrandomFill,