Files
aji/examples/std/heap.aji
dev cfece6080c cli: add safe owning binary heaps
Add std.heap with an exact explicit pure ordering operation, bounded
push, Copy-only peek, moving pop and empty-only free. Sift Copy indices
while complete owners remain in dense initialized storage; repair holes
through complete exchange. Reserve preserves layout on returned failure
and accounts for both old/new buffers and separately owned payloads.

Integrate normative contracts, RFC, references, bundle measurements,
independent heapq/permutation oracles and CLI/package/profile checks.

Validation: 1007 conformance cases; 34 heap GC fixtures and scheduled
stress; unit/Quick Reference, formatting, hosted smoke, 70 CLI checks,
109 source coverage constructs and unchanged AIR reference bundle.
2026-10-07 11:41:35 +03:00

33 lines
1.3 KiB
Rust

module demo.heap
import std.heap;
import std.option;
struct Item { priority: u64, owner: own<u64> }
fn before(a: ref<Item>, b: ref<Item>) -> bool effects [] { return a.priority < b.priority; }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let value = match std.heap.new::<Item,fn before>(1,heap) {
std.heap.NewResult::<Item,fn before>::ok(value) => { move value },
std.heap.NewResult::<Item,fn before>::err(error) => { trap; }
};
let item = Item { priority: 7, owner: own.new(42u64,heap) or trap };
{ mut q = value;
match std.heap.push::<Item,fn before>(q,move item) {
std.heap.PushResult::<Item>::ok => {}
std.heap.PushResult::<Item>::full(returned) => { trap; }
}
match std.heap.pop::<Item,fn before>(q) {
std.option.Option::<Item>::some(item) => {
let (priority,owner) = struct.unpack(move item);
assert(priority == 7u64); assert(own.take(move owner) == 42u64);
}
std.option.Option::<Item>::none => { trap; }
}
}
match std.heap.free::<Item,fn before>(move value) {
std.heap.FreeResult::<Item,fn before>::ok => {}
std.heap.FreeResult::<Item,fn before>::not_empty(returned) => { trap; }
}
return 42;
}