Register std.map with bounded U64Map<V>, exact key equality and explicit allocation/growth. Preserve returned ownership, old slots on reserve failure and original-authority refunds; support Copy values, owners and resources. Cover collision/tombstone/wrap behavior, independent dictionary traces, quota/cleanup/borrow/task rules, source/AIR parity and hosted/package CLI. Publish the API, roadmap and measured documentation cost. General key hashing/equality remains Q-037; numeric maps do not satisfy that gate.
52 lines
2.0 KiB
Rust
52 lines
2.0 KiB
Rust
// expect: run
|
|
// result: 42
|
|
// phase: 6
|
|
// spec: docs/spec/16-standard-library.md §2.13
|
|
// grant: heap=mem.alloc:256
|
|
// schedules: 12
|
|
module test.map
|
|
import std.map;
|
|
import std.option;
|
|
fn opened<V>(capacity: usize, heap: cap<mem.alloc>) -> std.map.U64Map<V> effects [mem.alloc] {
|
|
match std.map.new::<V>(capacity, heap) {
|
|
std.map.NewResult::<V>::ok(map) => { return move map; }
|
|
std.map.NewResult::<V>::err(error) => { trap; }
|
|
}
|
|
}
|
|
fn inserted<V>(result: std.map.InsertResult<V>) -> void effects [] {
|
|
match move result {
|
|
std.map.InsertResult::<V>::inserted => {}
|
|
std.map.InsertResult::<V>::replaced(old) => { trap; }
|
|
std.map.InsertResult::<V>::full(value) => { trap; }
|
|
}
|
|
}
|
|
fn released<V>(map: std.map.U64Map<V>) -> void effects [] {
|
|
match std.map.free::<V>(move map) {
|
|
std.map.FreeResult::<V>::ok => {}
|
|
std.map.FreeResult::<V>::not_empty(returned) => { trap; }
|
|
}
|
|
}
|
|
fn extracted<V>(value: std.option.Option<V>) -> V effects [] {
|
|
match move value {
|
|
std.option.Option::<V>::some(item) => { return move item; }
|
|
std.option.Option::<V>::none => { trap; }
|
|
}
|
|
}
|
|
struct Job { number: u64 }
|
|
fn worker(map: std.map.U64Map<sender<Job>>) -> void effects [task.wait] {
|
|
{ mut q = map; let entry = extracted::<std.map.Entry<sender<Job>>>(std.map.pop::<sender<Job>>(q));
|
|
let (key, tx) = struct.unpack(move entry); assert(key == 7u64);
|
|
channel.send(tx, Job { number: 42 }) or trap; channel.close_sender(move tx);
|
|
}
|
|
released::<sender<Job>>(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::<sender<Job>>(1, heap);
|
|
{ mut q = map; inserted::<sender<Job>>(std.map.insert::<sender<Job>>(q, 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;
|
|
}
|
|
|