mpstthree/role/
b.rs

1//! This module contains the required definitions and
2//! functions for the basic role B.
3//! Its dual is [RoleBDual](crate::role::b_dual::RoleBDual).
4
5use crate::role::b_dual::RoleBDual;
6use crate::role::Role;
7use crossbeam_channel::{bounded, Sender};
8
9/// Gives the order to the
10/// [`MeshedChannels`] related to B.
11///
12/// This `struct` should only be used in the `stack` field
13/// of the [`MeshedChannels`] related
14/// to B.
15///
16/// [`MeshedChannels`]: crate::meshedchannels::MeshedChannels
17///
18/// # Example
19///
20/// ```
21/// use mpstthree::role::b::RoleB;
22/// use mpstthree::role::end::RoleEnd;
23/// use mpstthree::role::Role; // Only used for example
24///
25/// type StackB = RoleB<RoleEnd>;
26///
27/// let _ = StackB::new(); // Only used for example
28/// ```
29#[derive(Debug)]
30pub struct RoleB<R>
31where
32    R: Role,
33    R::Dual: Role,
34{
35    #[doc(hidden)]
36    pub sender: Sender<R::Dual>,
37}
38
39impl<R: Role> Role for RoleB<R> {
40    type Dual = RoleBDual<R::Dual>;
41
42    fn new() -> (Self, Self::Dual) {
43        let (sender_normal, _) = bounded::<R>(1);
44        let (sender_dual, _) = bounded::<R::Dual>(1);
45
46        (
47            RoleB {
48                sender: sender_dual,
49            },
50            RoleBDual {
51                sender: sender_normal,
52            },
53        )
54    }
55
56    fn head_str() -> String {
57        "RoleB".to_string()
58    }
59
60    fn tail_str() -> String {
61        format!("{}<{}>", R::head_str(), R::tail_str())
62    }
63
64    fn self_head_str(&self) -> String {
65        "RoleB".to_string()
66    }
67
68    fn self_tail_str(&self) -> String {
69        format!("{}<{}>", R::head_str(), R::tail_str())
70    }
71}
72
73impl<R: Role> RoleB<R> {
74    /// Return the continuation for RoleB
75    pub fn continuation(&self) -> R {
76        let (here, there) = R::new();
77        self.sender.send(there).unwrap_or(());
78        here
79    }
80}