hashPointer
Helper function to hash a pointer and mutate the strategy if needed.
Function parameters
Parameters
- hasher:anytype
- key:anytype
Describes how pointer types should be hashed.
Types
- HashStrategy
- Describes how pointer types should be hashed.
Helper function to hash a pointer and mutate the strategy if needed.
Functions
- hashPointer
- Helper function to hash a pointer and mutate the strategy if needed.
- hashArray
- Helper function to hash a set of contiguous objects, from an array or slice.
- hash
- Provides generic hashing for any eligible type.
- autoHash
- Provides generic hashing for any eligible type.
Source
Implementation
pub fn hashPointer(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
const info = @typeInfo(@TypeOf(key));
switch (info.pointer.size) {
.one => switch (strat) {
.Shallow => hash(hasher, @intFromPtr(key), .Shallow),
.Deep => hash(hasher, key.*, .Shallow),
.DeepRecursive => hash(hasher, key.*, .DeepRecursive),
},
.slice => {
switch (strat) {
.Shallow => {
hashPointer(hasher, key.ptr, .Shallow);
},
.Deep => hashArray(hasher, key, .Shallow),
.DeepRecursive => hashArray(hasher, key, .DeepRecursive),
}
hash(hasher, key.len, .Shallow);
},
.many,
.c,
=> switch (strat) {
.Shallow => hash(hasher, @intFromPtr(key), .Shallow),
else => @compileError(
\\ unknown-length pointers and C pointers cannot be hashed deeply.
\\ Consider providing your own hash function.
),
},
}
}