Files
aji/tests/conformance/source/programs/channel_transfer.aji
T
dev 591f0aea19 P6A-7 for the freeze: three reference programs in source; source construct coverage
- programs/parallel_map, channel_transfer, tcp_echo_tasks: the Phase 3 exit
  programs in .aji, with AIR equivalence on every schedule seed
- tools/source_coverage.py: every .aji construct (56) has a run/accept test
  and, where it rejects, a reject test; CI step
- gaps filled: wrapping arithmetic run test, break outside a loop (E7006),
  close_receiver without pending (E7004)
- plan: P6A-7 freeze criterion (owner decision); further twins gradually
2026-10-05 18:15:48 +03:00

79 lines
2.4 KiB
Rust

// Reference program (Phase 3 exit, in source): ownership transfer through a bounded
// channel. The producer allocates each message on the heap and moves the owner into
// the channel; the consumer takes it. The budget (64 bytes) only fits the channel and
// three live messages: bounded capacity bounds memory. 10 + 20 + 30 = 60.
// expect: run
// grant: heap=mem.alloc:64
// schedules: 30
// quantum: 2
// result: 60
// equivalent: phase3/run/program_channel_transfer.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.channeltransfer
struct Message {
id: u64,
size: u64,
}
fn producer(tx: sender<own<Message>>, heap: cap<mem.alloc>) -> u8
effects [mem.alloc, task.wait]
{
var id: u64 = 10;
while id != 40 {
let o = own.new(Message { id: id, size: 16 }, heap) or trap;
match channel.send(tx, move o) {
ok => {}
closed(back) => {
own.free(move back);
break;
}
cancelled(back) => {
own.free(move back);
break;
}
}
id = id + 10;
}
channel.close_sender(move tx);
0
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn, task.wait]
{
match channel.make<own<Message>>(2, heap) {
ok(tx, rx) => {
var acc: u64 = 0;
scope sc {
let p = task.spawn(sc, producer, move tx, heap);
var r: receiver<own<Message>> = move rx;
loop {
match channel.recv(r) {
ok(o) => {
let msg = own.take(move o);
acc = acc + msg.id;
}
closed => {
match channel.close_receiver(move r) {
ok => { break; }
pending(r) => {}
}
}
cancelled => {
match channel.close_receiver(move r) {
ok => { break; }
pending(r) => {}
}
}
}
}
let x = task.join(move p);
}
acc
}
err(e) => { trap; }
}
}