hydro2_network/
edge.rs

1// ---------------- [ File: src/edge.rs ]
2crate::ix!();
3
4/// Represents an edge in the DAG, connecting one node's output
5/// to another node's input.
6#[derive(Builder,MutGetters,Setters,Getters,Debug,Clone)]
7#[getset(get="pub",set = "pub", get_mut = "pub")]
8#[builder(setter(into))]
9pub struct NetworkEdge {
10    /// The source node index.
11    source_index: usize,
12    /// The output buffer index in the source node.
13    source_output_idx: usize,
14    /// The destination node index.
15    dest_index: usize,
16    /// The input buffer index in the destination node.
17    dest_input_idx: usize,
18}
19
20#[macro_export]
21macro_rules! edge {
22
23    ($src:literal : $src_out:literal -> $dest:literal : $dest_in:literal) => {{
24        assert!($src_out < 4, "src output port must be usize less than 4");
25        assert!($dest_in < 4,  "dst input port must be usize less than 4");
26        NetworkEdgeBuilder::default()
27            .source_index($src as usize)
28            .source_output_idx($src_out as usize)
29            .dest_index($dest as usize)
30            .dest_input_idx($dest_in as usize)
31            .build()
32            .unwrap()
33    }};
34
35    (($src:expr, $src_out:expr) -> ($dest:expr, $dest_in:expr)) => {{
36        assert!($src_out < 4, "src output port must be usize less than 4");
37        assert!($dest_in < 4,  "dst input port must be usize less than 4");
38        NetworkEdgeBuilder::default()
39            .source_index(($src) as usize)
40            .source_output_idx(($src_out) as usize)
41            .dest_index(($dest) as usize)
42            .dest_input_idx(($dest_in) as usize)
43            .build()
44            .unwrap()
45    }};
46}
47
48#[cfg(test)]
49mod edge_macro_tests {
50    use super::*;
51
52    #[test]
53    fn test_edge_macro_literal() {
54        // edge!(0:0 -> 1:0)
55        let e = edge!(0:0 -> 1:0);
56        assert_eq!(*e.source_index(), 0);
57        assert_eq!(*e.source_output_idx(), 0);
58        assert_eq!(*e.dest_index(), 1);
59        assert_eq!(*e.dest_input_idx(), 0);
60    }
61
62    #[test]
63    fn test_edge_macro_tuple() {
64        // edge!((5,1)->(10,2))
65        let e = edge!((5,1) -> (10,2));
66        assert_eq!(*e.source_index(), 5);
67        assert_eq!(*e.source_output_idx(), 1);
68        assert_eq!(*e.dest_index(), 10);
69        assert_eq!(*e.dest_input_idx(), 2);
70    }
71
72    #[test]
73    #[should_panic(expected = "src output port must be usize less than 4")]
74    fn test_edge_macro_panic_src_too_large() {
75        // we pass an out port >=4 => triggers the assert!
76        let _ = edge!(0:5 -> 1:0);
77    }
78
79    #[test]
80    #[should_panic(expected = "dst input port must be usize less than 4")]
81    fn test_edge_macro_panic_dst_too_large() {
82        let _ = edge!((0,1)->(1,4));
83    }
84}