flowrlib/
block.rs

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