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.
28 lines
2.2 KiB
Rust
28 lines
2.2 KiB
Rust
// expect: run
|
|
// result: 42
|
|
// phase: 6
|
|
// spec: docs/spec/16-standard-library.md §2.16
|
|
// grant: heap=mem.alloc:88
|
|
module test.heap
|
|
import std.heap;
|
|
import std.option;
|
|
fn opened<T, B: fn(ref<T>,ref<T>) -> bool effects []>(capacity: usize,heap: cap<mem.alloc>) -> std.heap.BinaryHeap<T,fn B> effects [mem.alloc] {
|
|
match std.heap.new::<T,fn B>(capacity,heap) { std.heap.NewResult::<T,fn B>::ok(value) => { return move value; }, std.heap.NewResult::<T,fn B>::err(error) => { trap; } }
|
|
}
|
|
fn pushed<T>(result: std.heap.PushResult<T>) -> void effects [] { match move result { std.heap.PushResult::<T>::ok => {}, std.heap.PushResult::<T>::full(item) => { trap; } } }
|
|
fn extracted<T>(result: std.option.Option<T>) -> T effects [] { match move result { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } } }
|
|
fn released<T, B: fn(ref<T>,ref<T>) -> bool effects []>(value: std.heap.BinaryHeap<T,fn B>) -> void effects [] { match std.heap.free::<T,fn B>(move value) { std.heap.FreeResult::<T,fn B>::ok => {}, std.heap.FreeResult::<T,fn B>::not_empty(value) => { trap; } } }
|
|
struct Item { priority: u64, owner: own<u64> }
|
|
fn before(a: ref<Item>,b: ref<Item>) -> bool effects [] { return a.priority < b.priority; }
|
|
fn consumed(item: Item,expected: u64) -> void effects [] { let (priority,owner) = struct.unpack(move item); assert(priority == expected); assert(own.take(move owner) == expected); }
|
|
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
|
|
let value = opened::<Item,fn before>(2,heap);
|
|
{ mut q = value;
|
|
let first = Item { priority: 9,owner: own.new(9u64,heap) or trap }; pushed::<Item>(std.heap.push::<Item,fn before>(q,move first));
|
|
let second = Item { priority: 1,owner: own.new(1u64,heap) or trap }; pushed::<Item>(std.heap.push::<Item,fn before>(q,move second));
|
|
let full = Item { priority: 42,owner: own.new(42u64,heap) or trap };
|
|
match std.heap.push::<Item,fn before>(q,move full) { std.heap.PushResult::<Item>::full(item) => { consumed(move item,42); }, std.heap.PushResult::<Item>::ok => { trap; } }
|
|
consumed(extracted::<Item>(std.heap.pop::<Item,fn before>(q)),1); consumed(extracted::<Item>(std.heap.pop::<Item,fn before>(q)),9);
|
|
} released::<Item,fn before>(move value); return 42;
|
|
}
|