cli: add safe owning general-key hash maps
Use explicit static hash/equality functions, dense complete key/value entries and Copy probe indices. Preserve returned ownership and allocation failure state without unsafe code or new runtime instructions. Add normative contracts, API/reference budgets, 33 conformance cases, independent Copy/owner dict traces and 55 CLI/package/profile checks. Full corpus: 947 passing. Collection-every-step scheduler stress remains in progress, recorded in RFC/plan.
This commit is contained in:
1 parent
f02f4bd55b
commit
7bb5bd7931
52 files changed
+7736
-11
No files matched your search
@@ -0,0 +1,39 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.14
|
||||
// grant: heap=mem.alloc:256
|
||||
// schedules: 12
|
||||
module test.hashmap
|
||||
import std.hashmap;
|
||||
import std.option;
|
||||
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
|
||||
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
|
||||
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
|
||||
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
|
||||
}
|
||||
}
|
||||
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
|
||||
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
|
||||
}
|
||||
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
|
||||
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
|
||||
}
|
||||
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
|
||||
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
|
||||
}
|
||||
struct Key { id: 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; }
|
||||
struct Job { number: u64 }
|
||||
fn worker(map: std.hashmap.HashMap<Key, sender<Job>, fn hash, fn equal>) -> void effects [task.wait] {
|
||||
{ mut q = map; let entry = extracted::<std.hashmap.Entry<Key,sender<Job>>>(std.hashmap.pop::<Key, sender<Job>, fn hash, fn equal>(q));
|
||||
let (key,tx) = struct.unpack(move entry); channel.send(tx, Job { number: 42 }) or trap; channel.close_sender(move tx); }
|
||||
released::<Key, sender<Job>, fn hash, fn equal>(move map);
|
||||
}
|
||||
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc, task.spawn, task.wait] {
|
||||
match channel.make<Job>(1, heap) { ok(tx,rx) => {
|
||||
let map = opened::<Key, sender<Job>, fn hash, fn equal>(1, heap); { mut q = map; inserted::<Key,sender<Job>>(std.hashmap.insert::<Key, sender<Job>, fn hash, fn equal>(q, Key { id: 7 }, move tx)); }
|
||||
scope s { let t = task.spawn(s, worker, move map); let job = channel.recv(rx) or trap; task.join(move t); channel.close_receiver(move rx) or trap; assert(job.number == 42u64); }
|
||||
}, err(error) => { trap; } } return 42;
|
||||
}
|
||||
Reference in new issue
Block a user