#![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 // Send/Recv type RS = Recv<(), Send<(), End>>; type SR = Send<(), Recv<(), End>>; // Roles type R2A = RoleA>; type R2B = RoleB>; type R2C = RoleC>; type R2D = RoleD>; type R2E = RoleE>; // A enum Branching0fromEtoA { More( MeshedChannels< RS, RS, RS, Recv<(), Send<(), RecursAtoE>>, R2E>>>>, NameA, >, ), Done(MeshedChannels), } type RecursAtoE = Recv; // B enum Branching0fromEtoB { More( MeshedChannels< SR, RS, RS, Recv<(), Send<(), RecursBtoE>>, R2E>>>>, NameB, >, ), Done(MeshedChannels), } type RecursBtoE = Recv; // C enum Branching0fromEtoC { More( MeshedChannels< SR, SR, RS, Recv<(), Send<(), RecursCtoE>>, R2E>>>>, NameC, >, ), Done(MeshedChannels), } type RecursCtoE = Recv; // D enum Branching0fromEtoD { More( MeshedChannels< SR, SR, SR, Recv<(), Send<(), RecursDtoE>>, R2E>>>>, NameD, >, ), Done(MeshedChannels), } type RecursDtoE = Recv; // E type Choose0fromEtoA = Send; type Choose0fromEtoB = Send; type Choose0fromEtoC = Send; type Choose0fromEtoD = Send; type EndpointMoreE = MeshedChannels< Send<(), Recv<(), Choose0fromEtoA>>, Send<(), Recv<(), Choose0fromEtoB>>, Send<(), Recv<(), Choose0fromEtoC>>, Send<(), Recv<(), Choose0fromEtoD>>, R2A>>>, 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::More(s) => { let (_, s) = s.recv()?; let s = s.send(())?; let (_, s) = s.recv()?; let s = s.send(())?; let (_, s) = s.recv()?; let s = s.send(())?; let (_, s) = s.recv()?; let s = s.send(())?; endpoint_a(s) }, }) } #[inline] fn endpoint_b(s: EndpointB) -> Result<(), Box> { offer_mpst!(s, { Branching0fromEtoB::Done(s) => { s.close() }, Branching0fromEtoB::More(s) => { let (_, s) = s.recv()?; let s = s.send(())?; let s = s.send(())?; let (_, s) = s.recv()?; let (_, s) = s.recv()?; let s = s.send(())?; 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::More(s) => { let (_, s) = s.recv()?; let s = s.send(())?; let s = s.send(())?; let (_, s) = s.recv()?; let s = s.send(())?; let (_, s) = s.recv()?; 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::More(s) => { let (_, s) = s.recv()?; let s = s.send(())?; let s = s.send(())?; let (_, s) = s.recv()?; let s = s.send(())?; let (_, s) = s.recv()?; let s = s.send(())?; let (_, s) = s.recv()?; endpoint_d(s) }, }) } #[inline] fn endpoint_e(s: EndpointE) -> Result<(), Box> { let mut temp_s = s; for _ in 1..LOOPS { temp_s = recurs_e(temp_s)?; } let s = choose_mpst_e_to_all!( temp_s, Branching0fromEtoA::Done, Branching0fromEtoB::Done, Branching0fromEtoC::Done, Branching0fromEtoD::Done, ); s.close() } fn recurs_e(s: EndpointE) -> Result> { let s: EndpointMoreE = choose_mpst_e_to_all!( s, Branching0fromEtoA::More, Branching0fromEtoB::More, Branching0fromEtoC::More, Branching0fromEtoD::More, ); let s = s.send(())?; let (_, s) = s.recv()?; let s = s.send(())?; let (_, s) = s.recv()?; let s = s.send(())?; let (_, s) = s.recv()?; let s = s.send(())?; let (_, s) = s.recv()?; 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(); }