#![allow( clippy::type_complexity, clippy::too_many_arguments, clippy::large_enum_variant )] use mpstthree::binary::struct_trait::{end::End, recv::Recv, send::Send}; use mpstthree::generate; use mpstthree::role::broadcast::RoleBroadcast; use mpstthree::role::end::RoleEnd; use std::error::Error; static LOOPS: i64 = 100; // Create new roles generate!("rec_and_cancel", MeshedChannels, A, B, C, D, E, F); // Types // A enum Branching0fromFtoA { Forward( MeshedChannels, End, End, End, RecursAtoF, RoleB>, NameA>, ), Backward( MeshedChannels, End, End, End, RecursAtoF, RoleB>, NameA>, ), Done(MeshedChannels), } type RecursAtoF = Recv; // B enum Branching0fromFtoB { Forward( MeshedChannels< Recv<(), End>, Send<(), End>, End, End, RecursBtoF, RoleA>>, NameB, >, ), Backward( MeshedChannels< Send<(), End>, Recv<(), End>, End, End, RecursBtoF, RoleC>>, NameB, >, ), Done(MeshedChannels), } type RecursBtoF = Recv; // C enum Branching0fromFtoC { Forward( MeshedChannels< End, Recv<(), End>, Send<(), End>, End, RecursCtoF, RoleB>>, NameC, >, ), Backward( MeshedChannels< End, Send<(), End>, Recv<(), End>, End, RecursCtoF, RoleD>>, NameC, >, ), Done(MeshedChannels), } type RecursCtoF = Recv; // D enum Branching0fromFtoD { Forward( MeshedChannels< End, End, Recv<(), End>, Send<(), End>, RecursDtoF, RoleC>>, NameD, >, ), Backward( MeshedChannels< End, End, Send<(), End>, Recv<(), End>, RecursDtoF, RoleE>>, NameD, >, ), Done(MeshedChannels), } type RecursDtoF = Recv; // E enum Branching0fromFtoE { Forward( MeshedChannels< End, End, End, Recv<(), End>, Send<(), RecursEtoF>, RoleD>>, NameE, >, ), Backward( MeshedChannels< End, End, End, Send<(), End>, Recv<(), RecursEtoF>, RoleF>>, NameE, >, ), Done(MeshedChannels), } type RecursEtoF = Recv; // F type Choose0fromFtoA = Send; type Choose0fromFtoB = Send; type Choose0fromFtoC = Send; type Choose0fromFtoD = Send; type Choose0fromFtoE = Send; type EndpointForwardF = MeshedChannels< Choose0fromFtoA, Choose0fromFtoB, Choose0fromFtoC, Choose0fromFtoD, Recv<(), Choose0fromFtoE>, RoleE, NameF, >; type EndpointBackwardF = MeshedChannels< Choose0fromFtoA, Choose0fromFtoB, Choose0fromFtoC, Choose0fromFtoD, Send<(), Choose0fromFtoE>, RoleE, NameF, >; // Creating the MP sessions type EndpointA = MeshedChannels, NameA>; type EndpointB = MeshedChannels, NameB>; type EndpointC = MeshedChannels, NameC>; type EndpointD = MeshedChannels, NameD>; type EndpointE = MeshedChannels, NameE>; type EndpointF = MeshedChannels< Choose0fromFtoA, Choose0fromFtoB, Choose0fromFtoC, Choose0fromFtoD, Choose0fromFtoE, RoleBroadcast, NameF, >; fn endpoint_a(s: EndpointA) -> Result<(), Box> { offer_mpst!(s, { Branching0fromFtoA::Done(s) => { s.close() }, Branching0fromFtoA::Forward(s) => { let s = s.send(())?; endpoint_a(s) }, Branching0fromFtoA::Backward(s) => { let (_, s) = s.recv()?; endpoint_a(s) }, }) } #[inline] fn endpoint_b(s: EndpointB) -> Result<(), Box> { offer_mpst!(s, { Branching0fromFtoB::Done(s) => { s.close() }, Branching0fromFtoB::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_b(s) }, Branching0fromFtoB::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_b(s) }, }) } #[inline] fn endpoint_c(s: EndpointC) -> Result<(), Box> { offer_mpst!(s, { Branching0fromFtoC::Done(s) => { s.close() }, Branching0fromFtoC::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_c(s) }, Branching0fromFtoC::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_c(s) }, }) } #[inline] fn endpoint_d(s: EndpointD) -> Result<(), Box> { offer_mpst!(s, { Branching0fromFtoD::Done(s) => { s.close() }, Branching0fromFtoD::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_d(s) }, Branching0fromFtoD::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_d(s) }, }) } #[inline] fn endpoint_e(s: EndpointE) -> Result<(), Box> { offer_mpst!(s, { Branching0fromFtoE::Done(s) => { s.close() }, Branching0fromFtoE::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_e(s) }, Branching0fromFtoE::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_e(s) }, }) } #[inline] fn endpoint_f(s: EndpointF) -> Result<(), Box> { let mut temp_s = s; for i in 1..LOOPS { temp_s = recurs_f(temp_s, i)?; } let s = choose_mpst_f_to_all!( temp_s, Branching0fromFtoA::Done, Branching0fromFtoB::Done, Branching0fromFtoC::Done, Branching0fromFtoD::Done, Branching0fromFtoE::Done ); s.close() } fn recurs_f(s: EndpointF, index: i64) -> Result> { match index { i if i % 2 == 0 => { let s: EndpointForwardF = choose_mpst_f_to_all!( s, Branching0fromFtoA::Forward, Branching0fromFtoB::Forward, Branching0fromFtoC::Forward, Branching0fromFtoD::Forward, Branching0fromFtoE::Forward ); let (_, s) = s.recv()?; Ok(s) } _ => { let s: EndpointBackwardF = choose_mpst_f_to_all!( s, Branching0fromFtoA::Backward, Branching0fromFtoB::Backward, Branching0fromFtoC::Backward, Branching0fromFtoD::Backward, Branching0fromFtoE::Backward ); let s = s.send(())?; Ok(s) } } } fn main() { let (thread_a, thread_b, thread_c, thread_d, thread_e, thread_f) = fork_mpst( endpoint_a, endpoint_b, endpoint_c, endpoint_d, endpoint_e, endpoint_f, ); thread_a.join().unwrap(); thread_b.join().unwrap(); thread_c.join().unwrap(); thread_d.join().unwrap(); thread_e.join().unwrap(); thread_f.join().unwrap(); }