Files
aji/tests/conformance/source/hashset/file.aji
T
dev 4716cda0f1 cli: add safe owning general-key hash sets
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.
2026-10-07 11:14:53 +03:00

32 lines
2.0 KiB
Rust

// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.15
// grant: heap=mem.alloc:59
// grant: dst=fs.write:/out
// file: /out/hashset.txt=42
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, file: file<write> }
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>,dst: cap<fs.write>) -> u64 effects [mem.alloc,fs.write] {
let set = opened::<Key,fn hash,fn equal>(1,heap);
let key = Key { id: 1,file: fs.create(dst,b"/out/hashset.txt") or trap };
{ mut q = set; inserted::<Key>(std.hashset.insert::<Key,fn hash,fn equal>(q,move key));
let key = extracted::<Key>(std.hashset.pop::<Key,fn hash,fn equal>(q)); let (id,file) = struct.unpack(move key);
fs.write_all(file,b"42") or trap; fs.close(move file) or trap; }
released::<Key,fn hash,fn equal>(move set); return 42;
}