gwr_components/
connect.rs

1// Copyright (c) 2023 Graphcore Ltd. All rights reserved.
2
3//! Helper connection macros
4
5pub use paste::paste;
6
7#[macro_export]
8/// Connect an [OutPort](gwr_engine::port::OutPort) port to an
9/// [InPort](gwr_engine::port::InPort)
10macro_rules! connect_port {
11    ($from:expr, $from_port_name:ident => $to:expr, $to_port_name:ident) => {
12        {
13            gwr_track::debug!($from.entity ; "Connect {}.{} => {}.{}", $from, stringify!($from_port_name), $to, stringify!($to_port_name));
14            $crate::connect::paste! {
15                $from.[< connect_port_ $from_port_name >]($to.[< port_ $to_port_name >]())
16            }
17        }
18    };
19    ($from:expr, $from_port_name:ident, $from_index:expr => $to:expr, $to_port_name:ident) => {
20        {
21            let from_index: usize = $from_index;
22            gwr_track::debug!($from.entity ; "Connect {}.{}[{}] => {}.{}", $from, stringify!($from_port_name), from_index, $to, stringify!($to_port_name));
23            $crate::connect::paste! {
24                $from.[< connect_port_ $from_port_name _i >](from_index, $to.[< port_ $to_port_name >]())
25            }
26        }
27    };
28    ($from:expr, $from_port_name:ident => $to:expr, $to_port_name:ident, $to_index:expr) => {
29        {
30            let to_index: usize = $to_index;
31            gwr_track::debug!($from.entity ; "Connect {}.{} => {}.{}[{}]", $from, stringify!($from_port_name), $to, stringify!($to_port_name), to_index);
32            $crate::connect::paste! {
33                $from.[< connect_port_ $from_port_name >]($to.[< port_ $to_port_name _i >](to_index))
34            }
35        }
36    };
37    ($from:expr, $from_port_name:ident, $from_index:expr => $to:expr, $to_port_name:ident, $to_index:expr) => {
38        {
39            let from_index: usize = $from_index;
40            let to_index: usize = $to_index;
41            gwr_track::debug!($from.entity ; "Connect {}.{}[{}] => {}.{}[{}]", $from, stringify!($from_port_name), from_index, $to, stringify!($to_port_name), to_index);
42            $crate::connect::paste! {
43                $from.[< connect_port_ $from_port_name _i >](from_index, $to.[< port_ $to_port_name _i >](to_index))
44            }
45        }
46    };
47}
48
49#[macro_export]
50/// Connect a tx port for a subcomponent.
51///
52/// The subcomponent is expected to be stored in a `RefCell<Option<>>`
53macro_rules! connect_tx {
54    ($component:expr, $fn:ident ; $port_state:ident) => {
55        $crate::connect::paste! {
56            $component
57                .borrow_mut()
58                .as_mut()
59                .unwrap()
60                .$fn($port_state)
61        }
62    };
63}
64
65#[macro_export]
66/// Connect a tx port for a subcomponent where the port is one of an array.
67///
68/// The subcomponent is expected to be stored in a `RefCell<Option<>>`
69macro_rules! connect_tx_i {
70    ($component:expr, $fn:ident, $index:expr ; $port_state:ident) => {
71        $component
72            .borrow_mut()
73            .as_mut()
74            .unwrap()
75            .$fn($index, $port_state)
76    };
77}
78
79#[macro_export]
80/// Access rx port for a subcomponent.
81///
82/// The subcomponent is expected to be stored in a `RefCell<Option<>>`
83macro_rules! port_rx {
84    ($component:expr, $fn:ident) => {
85        $component.borrow().as_ref().unwrap().$fn()
86    };
87}
88
89#[macro_export]
90/// Access an individual index of an rx port array for a subcomponent.
91///
92/// The subcomponent is expected to be stored in a `RefCell<Option<>>`
93macro_rules! port_rx_i {
94    ($component:expr, $fn:ident, $index:expr) => {
95        $component.borrow().as_ref().unwrap().$fn($index)
96    };
97}
98
99#[macro_export]
100/// Get a reference to a variable stored in a `RefCell<Option<>>`.
101macro_rules! borrow_option {
102    ($var:expr) => {
103        $var.borrow().as_ref().unwrap()
104    };
105}
106
107#[macro_export]
108/// Get a mutable reference to a variable stored in a `RefCell<Option<>>`.
109macro_rules! borrow_option_mut {
110    ($var:expr) => {
111        $var.borrow_mut().as_mut().unwrap()
112    };
113}
114
115#[macro_export]
116/// Take a variable out of a `RefCell<Option<>>`.
117macro_rules! take_option {
118    ($var:expr) => {
119        $var.borrow_mut().take().unwrap()
120    };
121}