#![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); // Types // A enum Branching0fromCtoA { Forward(MeshedChannels, RecursAtoC, RoleB>, NameA>), Backward(MeshedChannels, RecursAtoC, RoleB>, NameA>), Done(MeshedChannels), } type RecursAtoC = Recv; // B enum Branching0fromCtoB { Forward( MeshedChannels< Recv<(), End>, Send<(), RecursBtoC>, RoleA>>, NameB, >, ), Backward( MeshedChannels< Send<(), End>, Recv<(), RecursBtoC>, RoleC>>, NameB, >, ), Done(MeshedChannels), } type RecursBtoC = Recv; // C type Choose0fromCtoA = Send; type Choose0fromCtoB = Send; type EndpointForwardC = MeshedChannels, RoleB, NameC>; type EndpointBackwardC = MeshedChannels, RoleB, NameC>; // Creating the MP sessions type EndpointA = MeshedChannels, NameA>; type EndpointB = MeshedChannels, NameB>; type EndpointC = MeshedChannels; fn endpoint_a(s: EndpointA) -> Result<(), Box> { offer_mpst!(s, { Branching0fromCtoA::Done(s) => { s.close() }, Branching0fromCtoA::Forward(s) => { let s = s.send(())?; endpoint_a(s) }, Branching0fromCtoA::Backward(s) => { let (_, s) = s.recv()?; endpoint_a(s) }, }) } #[inline] fn endpoint_b(s: EndpointB) -> Result<(), Box> { offer_mpst!(s, { Branching0fromCtoB::Done(s) => { s.close() }, Branching0fromCtoB::Forward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_b(s) }, Branching0fromCtoB::Backward(s) => { let ((), s) = s.recv()?; let s = s.send(())?; endpoint_b(s) }, }) } #[inline] fn endpoint_c(s: EndpointC) -> Result<(), Box> { let mut temp_s = s; for i in 1..LOOPS { temp_s = recurs_c(temp_s, i)?; } let s = choose_mpst_c_to_all!(temp_s, Branching0fromCtoA::Done, Branching0fromCtoB::Done); s.close() } fn recurs_c(s: EndpointC, index: i64) -> Result> { match index { i if i % 2 == 0 => { let s: EndpointForwardC = choose_mpst_c_to_all!(s, Branching0fromCtoA::Forward, Branching0fromCtoB::Forward); let (_, s) = s.recv()?; Ok(s) } _ => { let s: EndpointBackwardC = choose_mpst_c_to_all!( s, Branching0fromCtoA::Backward, Branching0fromCtoB::Backward ); let s = s.send(())?; Ok(s) } } } fn main() { let (thread_a, thread_b, thread_c) = fork_mpst(endpoint_a, endpoint_b, endpoint_c); thread_a.join().unwrap(); thread_b.join().unwrap(); thread_c.join().unwrap(); }