use crate::role::a_to_all::RoleAtoAll;
use crate::role::Role;
use crossbeam_channel::{bounded, Sender};
#[derive(Debug)]
pub struct RoleAlltoA<R1, R2>
where
R1: Role,
R2: Role,
R1::Dual: Role,
R2::Dual: Role,
{
#[doc(hidden)]
pub sender1: Sender<R1::Dual>,
#[doc(hidden)]
pub sender2: Sender<R2::Dual>,
}
impl<R1: Role, R2: Role> Role for RoleAlltoA<R1, R2> {
type Dual = RoleAtoAll<R1::Dual, R2::Dual>;
fn new() -> (Self, Self::Dual) {
let (sender_normal_1, _) = bounded::<R1>(1);
let (sender_normal_2, _) = bounded::<R2>(1);
let (sender_dual_1, _) = bounded::<R1::Dual>(1);
let (sender_dual_2, _) = bounded::<R2::Dual>(1);
(
RoleAlltoA {
sender1: sender_dual_1,
sender2: sender_dual_2,
},
RoleAtoAll {
sender1: sender_normal_1,
sender2: sender_normal_2,
},
)
}
fn head_str() -> String {
"RoleAlltoA".to_string()
}
fn tail_str() -> String {
format!(
"{}<{}> + {}<{}>",
R1::head_str(),
R1::tail_str(),
R2::head_str(),
R2::tail_str()
)
}
fn self_head_str(&self) -> String {
"RoleAlltoA".to_string()
}
fn self_tail_str(&self) -> String {
format!(
"{}<{}> + {}<{}>",
R1::head_str(),
R1::tail_str(),
R2::head_str(),
R2::tail_str()
)
}
}
impl<R1: Role, R2: Role> RoleAlltoA<R1, R2> {
pub fn continuation_left(&self) -> R1 {
let (here, there) = R1::new();
self.sender1.send(there).unwrap_or(());
here
}
pub fn continuation_right(&self) -> R2 {
let (here, there) = R2::new();
self.sender2.send(there).unwrap_or(());
here
}
}