Skip to main content

flowrlib/
block.rs

1use std::fmt;
2
3use serde_derive::{Deserialize, Serialize};
4
5/// Blocks between functions are describes using:
6/// * `blocking_flow_id`
7/// * `blocking_function_id`
8/// * `blocking_io_number`
9/// * `blocked_function_id`
10/// * `blocked_flow_id`
11#[derive(PartialEq, Clone, Hash, Eq, Serialize, Deserialize)]
12pub struct Block {
13    /// The id of the flow where the blocking function reside
14    pub blocking_flow_id: usize,
15    /// The id of the blocking function (destination with input unable to be sent to)
16    pub blocking_function_id: usize,
17    /// The number of the io in the blocking function that is full and causing the block
18    pub blocking_io_number: usize,
19    /// The id of the function that would like to send to the blocking function but cannot because
20    /// the input is full
21    pub blocked_function_id: usize,
22    /// The id of the flow where the blocked function resides
23    pub blocked_flow_id: usize,
24}
25
26impl Block {
27    /// Create a new `Block`
28    #[must_use]
29    pub fn new(
30        blocking_flow_id: usize,
31        blocking_function_id: usize,
32        blocking_io_number: usize,
33        blocked_function_id: usize,
34        blocked_flow_id: usize,
35    ) -> Self {
36        Block {
37            blocking_flow_id,
38            blocking_function_id,
39            blocking_io_number,
40            blocked_function_id,
41            blocked_flow_id,
42        }
43    }
44}
45
46impl fmt::Debug for Block {
47    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48        write!(
49            f,
50            "#{}({}) -> #{}({}):{}",
51            self.blocked_function_id,
52            self.blocked_flow_id,
53            self.blocking_function_id,
54            self.blocking_flow_id,
55            self.blocking_io_number
56        )
57    }
58}
59
60impl fmt::Display for Block {
61    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62        write!(
63            f,
64            "#{}({}) -> #{}({}):{}",
65            self.blocked_function_id,
66            self.blocked_flow_id,
67            self.blocking_function_id,
68            self.blocking_flow_id,
69            self.blocking_io_number
70        )
71    }
72}
73
74#[cfg(test)]
75#[allow(clippy::unwrap_used, clippy::expect_used)]
76mod test {
77    #[test]
78    fn display_block_test() {
79        let block = super::Block::new(1, 2, 0, 1, 0);
80        println!("Block: {block}");
81    }
82
83    #[test]
84    fn debug_block_test() {
85        let block = super::Block::new(1, 2, 0, 1, 0);
86        println!("Block: {block:?}");
87    }
88
89    #[test]
90    fn block_new_test() {
91        let block = super::Block::new(1, 2, 0, 1, 0);
92        assert_eq!(block.blocking_flow_id, 1);
93        assert_eq!(block.blocking_function_id, 2);
94        assert_eq!(block.blocking_io_number, 0);
95        assert_eq!(block.blocked_function_id, 1);
96    }
97}