use mpstthree::binary::struct_trait::{end::End, recv::Recv, send::Send, session::Session}; use mpstthree::generate; use mpstthree::role::broadcast::RoleBroadcast; use mpstthree::role::end::RoleEnd; use std::error::Error; // See the folder scribble_protocols for the related Scribble protocol static LOOPS: i64 = 20; // Create new MeshedChannels for four participants generate!("rec_and_cancel", MeshedChannels, A, B); // Types // A type Choose0fromAtoB = ::Dual; // B enum Branching0fromAtoB { More(MeshedChannels>, RoleA>>, NameB>), Done(MeshedChannels), } type RecursBtoA = Recv; // Creating the MP sessions type EndpointA = MeshedChannels; type EndpointAMore = MeshedChannels>, RoleB>, NameA>; type EndpointB = MeshedChannels, NameB>; // Functions fn endpoint_a(s: EndpointA) -> Result<(), Box> { recurs_a(s, LOOPS, 1) } fn recurs_a(s: EndpointA, index: i64, old: i64) -> Result<(), Box> { match index { 0 => { let s = choose_mpst_a_to_all!(s, Branching0fromAtoB::Done); s.close() } i => { let s: EndpointAMore = choose_mpst_a_to_all!(s, Branching0fromAtoB::More); let s = s.send(old)?; let (new, s) = s.recv()?; recurs_a(s, i - 1, new) } } } fn endpoint_b(s: EndpointB) -> Result<(), Box> { recurs_b(s, 0) } fn recurs_b(s: EndpointB, old: i64) -> Result<(), Box> { offer_mpst!(s, { Branching0fromAtoB::Done(s) => { s.close() }, Branching0fromAtoB::More(s) => { let (new, s) = s.recv()?; let s = s.send(new + old)?; recurs_b(s, new + old) }, }) } fn main() { let (thread_a, thread_b) = fork_mpst(endpoint_a, endpoint_b); thread_a.join().unwrap(); thread_b.join().unwrap(); }