// 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>, heap: cap) -> 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) -> u64 effects [mem.alloc, task.spawn, task.wait] { match channel.make>(2, heap) { ok(tx, rx) => { var acc: u64 = 0; scope sc { let p = task.spawn(sc, producer, move tx, heap); var r: receiver> = 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; } } }