Add std.hashset with explicit pure hash/equality policies and complete owning keys. Duplicate/full insert returns the original input, while remove/pop and nonempty free preserve explicit resource ownership. Reuse safe HashMap storage and failure/peak/refund accounting. Integrate normative contracts, RFC, references, complexity measurements, CLI/profile checks and independent Copy/owner set traces. Validation: 973 conformance cases; runtime set GC and schedule stress; unit/Quick Reference, formatting, hosted smoke, 53 CLI checks, 106 source coverage constructs and bundle generation.
35 lines
1.4 KiB
Rust
35 lines
1.4 KiB
Rust
module demo.hashset
|
|
import std.hashset;
|
|
import std.option;
|
|
|
|
struct Key { id: u64, owner: own<u64> }
|
|
fn hash(key: ref<Key>) -> u64 effects [] { return key.id; }
|
|
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
|
|
|
|
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
|
|
let set = match std.hashset.new::<Key,fn hash,fn equal>(1,heap) {
|
|
std.hashset.NewResult::<Key,fn hash,fn equal>::ok(value) => { move value },
|
|
std.hashset.NewResult::<Key,fn hash,fn equal>::err(error) => { trap; }
|
|
};
|
|
let key = Key { id: 7, owner: own.new(42u64,heap) or trap };
|
|
{ mut q = set;
|
|
match std.hashset.insert::<Key,fn hash,fn equal>(q,move key) {
|
|
std.hashset.InsertResult::<Key>::inserted => {}
|
|
std.hashset.InsertResult::<Key>::present(returned) => { trap; }
|
|
std.hashset.InsertResult::<Key>::full(returned) => { trap; }
|
|
}
|
|
match std.hashset.pop::<Key,fn hash,fn equal>(q) {
|
|
std.option.Option::<Key>::some(key) => {
|
|
let (id,owner) = struct.unpack(move key);
|
|
assert(id == 7u64); assert(own.take(move owner) == 42u64);
|
|
}
|
|
std.option.Option::<Key>::none => { trap; }
|
|
}
|
|
}
|
|
match std.hashset.free::<Key,fn hash,fn equal>(move set) {
|
|
std.hashset.FreeResult::<Key,fn hash,fn equal>::ok => {}
|
|
std.hashset.FreeResult::<Key,fn hash,fn equal>::not_empty(returned) => { trap; }
|
|
}
|
|
return 42;
|
|
}
|