#![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, G); // Types // A enum Branching0fromGtoA { Forward( MeshedChannels< Send<(), End>, End, End, End, End, RecursAtoG, RoleB>, NameA, >, ), Backward( MeshedChannels< Recv<(), End>, End, End, End, End, RecursAtoG, RoleB>, NameA, >, ), Done(MeshedChannels), } type RecursAtoG = Recv; // B enum Branching0fromGtoB { Forward( MeshedChannels< Recv<(), End>, Send<(), End>, End, End, End, RecursBtoG, RoleA>>, NameB, >, ), Backward( MeshedChannels< Send<(), End>, Recv<(), End>, End, End, End, RecursBtoG, RoleC>>, NameB, >, ), Done(MeshedChannels), } type RecursBtoG = Recv; // C enum Branching0fromGtoC { Forward( MeshedChannels< End, Recv<(), End>, Send<(), End>, End, End, RecursCtoG, RoleB>>, NameC, >, ), Backward( MeshedChannels< End, Send<(), End>, Recv<(), End>, End, End, RecursCtoG, RoleD>>, NameC, >, ), Done(MeshedChannels), } type RecursCtoG = Recv; // D enum Branching0fromGtoD { Forward( MeshedChannels< End, End, Recv<(), End>, Send<(), End>, End, RecursDtoG, RoleC>>, NameD, >, ), Backward( MeshedChannels< End, End, Send<(), End>, Recv<(), End>, End, RecursDtoG, RoleE>>, NameD, >, ), Done(MeshedChannels), } type RecursDtoG = Recv; // E enum Branching0fromGtoE { Forward( MeshedChannels< End, End, End, Recv<(), End>, Send<(), End>, RecursEtoG, RoleD>>, NameE, >, ), Backward( MeshedChannels< End, End, End, Send<(), End>, Recv<(), End>, RecursEtoG, RoleF>>, NameE, >, ), Done(MeshedChannels), } type RecursEtoG = Recv; // F enum Branching0fromGtoF { Forward( MeshedChannels< End, End, End, End, Recv<(), End>, Send<(), RecursFtoG>, RoleE>>, NameF, >, ), Backward( MeshedChannels< End, End, End, End, Send<(), End>, Recv<(), RecursFtoG>, RoleG>>, NameF, >, ), Done(MeshedChannels), } type RecursFtoG = Recv; // F type Choose0fromGtoA = Send; type Choose0fromGtoB = Send; type Choose0fromGtoC = Send; type Choose0fromGtoD = Send; type Choose0fromGtoE = Send; type Choose0fromGtoF = Send; type EndpointForwardG = MeshedChannels< Choose0fromGtoA, Choose0fromGtoB, Choose0fromGtoC, Choose0fromGtoD, Choose0fromGtoE, Recv<(), Choose0fromGtoF>, RoleF, NameG, >; type EndpointBackwardG = MeshedChannels< Choose0fromGtoA, Choose0fromGtoB, Choose0fromGtoC, Choose0fromGtoD, Choose0fromGtoE, Send<(), Choose0fromGtoF>, RoleF, NameG, >; // 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, NameF>; type EndpointG = MeshedChannels< Choose0fromGtoA, Choose0fromGtoB, Choose0fromGtoC, Choose0fromGtoD, Choose0fromGtoE, Choose0fromGtoF, RoleBroadcast, NameG, >; fn endpoint_a(s: EndpointA) -> Result<(), Box> { offer_mpst!(s, { Branching0fromGtoA::Done(s) => { s.close() }, Branching0fromGtoA::Forward(s) => { let s = s.send(())?; endpoint_a(s) }, Branching0fromGtoA::Backward(s) => { let (_, s) = s.recv()?; endpoint_a(s) }, }) } #[inline] fn endpoint_b(s: EndpointB) -> Result<(), Box> { offer_mpst!(s, { Branching0fromGtoB::Done(s) => { s.close() }, Branching0fromGtoB::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_b(s) }, Branching0fromGtoB::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_b(s) }, }) } #[inline] fn endpoint_c(s: EndpointC) -> Result<(), Box> { offer_mpst!(s, { Branching0fromGtoC::Done(s) => { s.close() }, Branching0fromGtoC::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_c(s) }, Branching0fromGtoC::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_c(s) }, }) } #[inline] fn endpoint_d(s: EndpointD) -> Result<(), Box> { offer_mpst!(s, { Branching0fromGtoD::Done(s) => { s.close() }, Branching0fromGtoD::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_d(s) }, Branching0fromGtoD::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_d(s) }, }) } #[inline] fn endpoint_e(s: EndpointE) -> Result<(), Box> { offer_mpst!(s, { Branching0fromGtoE::Done(s) => { s.close() }, Branching0fromGtoE::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_e(s) }, Branching0fromGtoE::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_e(s) }, }) } #[inline] fn endpoint_f(s: EndpointF) -> Result<(), Box> { offer_mpst!(s, { Branching0fromGtoF::Done(s) => { s.close() }, Branching0fromGtoF::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_f(s) }, Branching0fromGtoF::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_f(s) }, }) } #[inline] fn endpoint_g(s: EndpointG) -> Result<(), Box> { let mut temp_s = s; for i in 1..LOOPS { temp_s = recurs_g(temp_s, i)?; } let s = choose_mpst_g_to_all!( temp_s, Branching0fromGtoA::Done, Branching0fromGtoB::Done, Branching0fromGtoC::Done, Branching0fromGtoD::Done, Branching0fromGtoE::Done, Branching0fromGtoF::Done ); s.close() } fn recurs_g(s: EndpointG, index: i64) -> Result> { match index { i if i % 2 == 0 => { let s: EndpointForwardG = choose_mpst_g_to_all!( s, Branching0fromGtoA::Forward, Branching0fromGtoB::Forward, Branching0fromGtoC::Forward, Branching0fromGtoD::Forward, Branching0fromGtoE::Forward, Branching0fromGtoF::Forward ); let (_, s) = s.recv()?; Ok(s) } _ => { let s: EndpointBackwardG = choose_mpst_g_to_all!( s, Branching0fromGtoA::Backward, Branching0fromGtoB::Backward, Branching0fromGtoC::Backward, Branching0fromGtoD::Backward, Branching0fromGtoE::Backward, Branching0fromGtoF::Backward ); let s = s.send(())?; Ok(s) } } } fn main() { let (thread_a, thread_b, thread_c, thread_d, thread_e, thread_f, thread_g) = fork_mpst( endpoint_a, endpoint_b, endpoint_c, endpoint_d, endpoint_e, endpoint_f, endpoint_g, ); thread_a.join().unwrap(); thread_b.join().unwrap(); thread_c.join().unwrap(); thread_d.join().unwrap(); thread_e.join().unwrap(); thread_f.join().unwrap(); thread_g.join().unwrap(); }