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.
34 lines
2.6 KiB
Rust
34 lines
2.6 KiB
Rust
// expect: run
|
|
// result: 42
|
|
// phase: 6
|
|
// spec: docs/spec/16-standard-library.md §2.15
|
|
// grant: heap=mem.alloc:75
|
|
module test.hashset
|
|
import std.hashset;
|
|
import std.option;
|
|
fn opened<K, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>,ref<K>) -> bool effects []>(capacity: usize,heap: cap<mem.alloc>) -> std.hashset.HashSet<K,fn H,fn E> effects [mem.alloc] {
|
|
match std.hashset.new::<K,fn H,fn E>(capacity,heap) { std.hashset.NewResult::<K,fn H,fn E>::ok(set) => { return move set; }, std.hashset.NewResult::<K,fn H,fn E>::err(error) => { trap; } }
|
|
}
|
|
fn inserted<K>(result: std.hashset.InsertResult<K>) -> void effects [] {
|
|
match move result { std.hashset.InsertResult::<K>::inserted => {}, std.hashset.InsertResult::<K>::present(key) => { trap; }, std.hashset.InsertResult::<K>::full(key) => { trap; } }
|
|
}
|
|
fn extracted<K>(result: std.option.Option<K>) -> K effects [] { match move result { std.option.Option::<K>::some(key) => { return move key; }, std.option.Option::<K>::none => { trap; } } }
|
|
fn released<K, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>,ref<K>) -> bool effects []>(set: std.hashset.HashSet<K,fn H,fn E>) -> void effects [] {
|
|
match std.hashset.free::<K,fn H,fn E>(move set) { std.hashset.FreeResult::<K,fn H,fn E>::ok => {}, std.hashset.FreeResult::<K,fn H,fn E>::not_empty(set) => { trap; } }
|
|
}
|
|
struct Key { id: u64, owner: own<u64> }
|
|
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
|
|
fn equal(a: ref<Key>,b: ref<Key>) -> bool effects [] { return a.id == b.id; }
|
|
fn consumed(key: Key,expected: u64) -> void effects [] { let (id,owner) = struct.unpack(move key); assert(own.take(move owner) == expected); }
|
|
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
|
|
let set = opened::<Key,fn hash,fn equal>(1,heap);
|
|
{ mut q = set;
|
|
let first = Key { id: 1,owner: own.new(11u64,heap) or trap }; inserted::<Key>(std.hashset.insert::<Key,fn hash,fn equal>(q,move first));
|
|
let duplicate = Key { id: 1,owner: own.new(99u64,heap) or trap };
|
|
match std.hashset.insert::<Key,fn hash,fn equal>(q,move duplicate) { std.hashset.InsertResult::<Key>::present(key) => { consumed(move key,99); }, std.hashset.InsertResult::<Key>::inserted => { trap; }, std.hashset.InsertResult::<Key>::full(key) => { trap; } }
|
|
let full = Key { id: 2,owner: own.new(22u64,heap) or trap };
|
|
match std.hashset.insert::<Key,fn hash,fn equal>(q,move full) { std.hashset.InsertResult::<Key>::full(key) => { consumed(move key,22); }, std.hashset.InsertResult::<Key>::inserted => { trap; }, std.hashset.InsertResult::<Key>::present(key) => { trap; } }
|
|
consumed(extracted::<Key>(std.hashset.pop::<Key,fn hash,fn equal>(q)),11);
|
|
} released::<Key,fn hash,fn equal>(move set); return 42;
|
|
}
|