Type Function Hashing [src]
Provides a Writer implementation based on calling Hasher.update,
discarding all data.
This implementation makes suboptimal buffering decisions due to being
generic. A better solution will involve creating a writer for each hash
function, where the splat buffer can be tailored to the hash implementation
details.
The total number of bytes written is stored in hasher.
Contrast with Hashed which also passes the data to an underlying stream.
Prototype
pub fn Hashing(comptime Hasher: type) type
Parameters
Hasher: type
Source
pub fn Hashing(comptime Hasher: type) type {
return struct {
hasher: Hasher,
writer: Writer,
pub fn init(buffer: []u8) @This() {
return .initHasher(.init(.{}), buffer);
}
pub fn initHasher(hasher: Hasher, buffer: []u8) @This() {
return .{
.hasher = hasher,
.writer = .{
.buffer = buffer,
.vtable = &.{ .drain = @This().drain },
},
};
}
fn drain(w: *Writer, data: []const []const u8, splat: usize) Error!usize {
const this: *@This() = @alignCast(@fieldParentPtr("writer", w));
const hasher = &this.hasher;
hasher.update(w.buffered());
w.end = 0;
var n: usize = 0;
for (data[0 .. data.len - 1]) |slice| {
hasher.update(slice);
n += slice.len;
}
for (0..splat) |_| hasher.update(data[data.len - 1]);
return n + splat * data[data.len - 1].len;
}
};
}