HashPolicy
Stores safely callable hash/equality functions as one concrete key policy.
Module std.collections
import std.collections;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
HashPolicy
pub final class HashPolicy<K>Stores safely callable hash/equality functions as one concrete key policy.
Construction allocates nothing and neither function is nullable. Signatures preserve readonly key borrows and safe invocation. The policy owns no keys; calls do not retain inputs. Keep hash/equality results stable for stored keys, and synchronize any external state read by the functions. K: Exact key type borrowed during each call.
Example
module example.hash_policy;
from std.collections import HashKeyPolicy, HashPolicy;
struct Key {
pub Identity: u64;
pub Payload: i32;
}
fn HashIdentity(Value: readonly Key&) -> u64 { return Value.Identity; }
fn EqualIdentity(Left: readonly Key&, Right: readonly Key&) -> bool {
return Left.Identity == Right.Identity;
}
fn SameKey<K, P>(Policy: readonly P&, Left: readonly K&, Right: readonly K&) -> bool
where P: HashKeyPolicy<K>
{
if !Policy.Equals(Left, Right) { return false; }
return Policy.Hash(Left) == Policy.Hash(Right);
}
fn Main() -> i32 {
let Policy = HashPolicy<Key>(HashIdentity, EqualIdentity);
var Left = default(Key);
Left.Identity = 42; Left.Payload = 1;
var Right = default(Key);
Right.Identity = 42; Right.Payload = 2;
if !SameKey<Key>(Policy, Left, Right) { return 1; }
Right.Identity = 43;
if Policy.Equals(Left, Right) { return 2; }
if Policy.Hash(Left) != 42 { return 3; }
return 0;
}HashPolicy.init
pub init(HashFunction: fn(readonly K&) -> u64,
EqualityFunction: fn(readonly K&, readonly K&) -> bool)Stores exact safe functions without allocation or boxing. HashFunction: Stable key hash operation. EqualityFunction: Equivalence relation consistent with HashFunction.
HashPolicy.Hash
pub readonly fn Hash(Key: readonly K&) -> u64Computes a key hash through the stored safe function. Key: Readonly input borrowed only during the call. Return: Stable hash; equivalent keys must share it.
HashPolicy.Equals
pub readonly fn Equals(Left: readonly K&, Right: readonly K&) -> boolTests key equivalence through the stored safe function. Left: First readonly input, not retained. Right: Second readonly input, not retained. Return: Whether both inputs identify the same logical key.