#![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); // Types // A enum Branching0fromEtoA { Forward(MeshedChannels, End, End, RecursAtoE, RoleB>, NameA>), Backward(MeshedChannels, End, End, RecursAtoE, RoleB>, NameA>), Done(MeshedChannels), } type RecursAtoE = Recv; // B enum Branching0fromEtoB { Forward( MeshedChannels< Recv<(), End>, Send<(), End>, End, RecursBtoE, RoleA>>, NameB, >, ), Backward( MeshedChannels< Send<(), End>, Recv<(), End>, End, RecursBtoE, RoleC>>, NameB, >, ), Done(MeshedChannels), } type RecursBtoE = Recv; // C enum Branching0fromEtoC { Forward( MeshedChannels< End, Recv<(), End>, Send<(), End>, RecursCtoE, RoleB>>, NameC, >, ), Backward( MeshedChannels< End, Send<(), End>, Recv<(), End>, RecursCtoE, RoleD>>, NameC, >, ), Done(MeshedChannels), } type RecursCtoE = Recv; // D enum Branching0fromEtoD { Forward( MeshedChannels< End, End, Recv<(), End>, Send<(), RecursDtoE>, RoleC>>, NameD, >, ), Backward( MeshedChannels< End, End, Send<(), End>, Recv<(), RecursDtoE>, RoleE>>, NameD, >, ), Done(MeshedChannels), } type RecursDtoE = Recv; // E type Choose0fromEtoA = Send; type Choose0fromEtoB = Send; type Choose0fromEtoC = Send; type Choose0fromEtoD = Send; type EndpointForwardE = MeshedChannels< Choose0fromEtoA, Choose0fromEtoB, Choose0fromEtoC, Recv<(), Choose0fromEtoD>, RoleD, NameE, >; type EndpointBackwardE = MeshedChannels< Choose0fromEtoA, Choose0fromEtoB, Choose0fromEtoC, Send<(), Choose0fromEtoD>, RoleD, NameE, >; // Creating the MP sessions type EndpointA = MeshedChannels, NameA>; type EndpointB = MeshedChannels, NameB>; type EndpointC = MeshedChannels, NameC>; type EndpointD = MeshedChannels, NameD>; type EndpointE = MeshedChannels< Choose0fromEtoA, Choose0fromEtoB, Choose0fromEtoC, Choose0fromEtoD, RoleBroadcast, NameE, >; fn endpoint_a(s: EndpointA) -> Result<(), Box> { offer_mpst!(s, { Branching0fromEtoA::Done(s) => { s.close() }, Branching0fromEtoA::Forward(s) => { let s = s.send(())?; endpoint_a(s) }, Branching0fromEtoA::Backward(s) => { let (_, s) = s.recv()?; endpoint_a(s) }, }) } #[inline] fn endpoint_b(s: EndpointB) -> Result<(), Box> { offer_mpst!(s, { Branching0fromEtoB::Done(s) => { s.close() }, Branching0fromEtoB::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_b(s) }, Branching0fromEtoB::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_b(s) }, }) } #[inline] fn endpoint_c(s: EndpointC) -> Result<(), Box> { offer_mpst!(s, { Branching0fromEtoC::Done(s) => { s.close() }, Branching0fromEtoC::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_c(s) }, Branching0fromEtoC::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_c(s) }, }) } #[inline] fn endpoint_d(s: EndpointD) -> Result<(), Box> { offer_mpst!(s, { Branching0fromEtoD::Done(s) => { s.close() }, Branching0fromEtoD::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_d(s) }, Branching0fromEtoD::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_d(s) }, }) } #[inline] fn endpoint_e(s: EndpointE) -> Result<(), Box> { let mut temp_s = s; for i in 1..LOOPS { temp_s = recurs_e(temp_s, i)?; } let s = choose_mpst_e_to_all!( temp_s, Branching0fromEtoA::Done, Branching0fromEtoB::Done, Branching0fromEtoC::Done, Branching0fromEtoD::Done ); s.close() } fn recurs_e(s: EndpointE, index: i64) -> Result> { match index { i if i % 2 == 0 => { let s: EndpointForwardE = choose_mpst_e_to_all!( s, Branching0fromEtoA::Forward, Branching0fromEtoB::Forward, Branching0fromEtoC::Forward, Branching0fromEtoD::Forward ); let (_, s) = s.recv()?; Ok(s) } _ => { let s: EndpointBackwardE = choose_mpst_e_to_all!( s, Branching0fromEtoA::Backward, Branching0fromEtoB::Backward, Branching0fromEtoC::Backward, Branching0fromEtoD::Backward ); let s = s.send(())?; Ok(s) } } } fn main() { let (thread_a, thread_b, thread_c, thread_d, thread_e) = fork_mpst(endpoint_a, endpoint_b, endpoint_c, endpoint_d, endpoint_e); thread_a.join().unwrap(); thread_b.join().unwrap(); thread_c.join().unwrap(); thread_d.join().unwrap(); thread_e.join().unwrap(); }