snarkvm_synthesizer/vm/helpers/
macros.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16/// A helper macro to downcast a `$variable` to `$object<$network>`.
17#[macro_export]
18macro_rules! cast_ref {
19    // Example: cast_ref!((foo.bar()) as Bar<MainnetV0>)
20    (($variable:expr) as $object:ident<$($traits:path),+>) => {{
21        (&$variable as &dyn std::any::Any)
22            .downcast_ref::<$object<$($traits),+>>()
23            .ok_or_else(|| anyhow!("Failed to downcast {}", stringify!($variable)))?
24    }};
25    // Example: cast_ref!(bar as Bar<MainnetV0>)
26    ($variable:ident as $object:ident<$($traits:path),+>) => {{
27        (&$variable as &dyn std::any::Any)
28            .downcast_ref::<$object<$($traits),+>>()
29            .ok_or_else(|| anyhow!("Failed to downcast {}", stringify!($variable)))?
30    }};
31    // Example: cast_ref!(&bar as Bar<MainnetV0>)
32    (&$variable:ident as $object:ident<$($traits:path),+>) => {{
33        ($variable as &dyn std::any::Any)
34            .downcast_ref::<$object<$($traits),+>>()
35            .ok_or_else(|| anyhow!("Failed to downcast {}", stringify!($variable)))?
36    }};
37}
38
39/// A helper macro to downcast a `$variable` to `&mut $object<$network>`.
40#[macro_export]
41macro_rules! cast_mut_ref {
42    // Example: cast_mut_ref!((foo.bar()) as Bar<MainnetV0>)
43    (($variable:expr) as $object:ident<$($traits:path),+>) => {{
44        (&mut $variable as &mut dyn std::any::Any)
45            .downcast_mut::<$object<$($traits),+>>()
46            .ok_or_else(|| anyhow!("Failed to downcast mut {}", stringify!($variable)))?
47    }};
48    // Example: cast_mut_ref!(bar as Bar<MainnetV0>)
49    ($variable:ident as $object:ident<$($traits:path),+>) => {{
50        (&mut $variable as &mut dyn std::any::Any)
51            .downcast_mut::<$object<$($traits),+>>()
52            .ok_or_else(|| anyhow!("Failed to downcast mut {}", stringify!($variable)))?
53    }};
54}
55
56/// A helper macro to dedup the `Network` trait and `Aleo` trait and process its given logic.
57#[macro_export]
58macro_rules! convert {
59    // Example: convert!(logic)
60    ($logic:ident) => {{
61        match N::ID {
62            console::network::MainnetV0::ID => {
63                // Process the logic.
64                $logic!(console::network::MainnetV0, circuit::AleoV0)
65            }
66            console::network::TestnetV0::ID => {
67                // Process the logic.
68                $logic!(console::network::TestnetV0, circuit::AleoTestnetV0)
69            }
70            console::network::CanaryV0::ID => {
71                // Process the logic.
72                $logic!(console::network::CanaryV0, circuit::AleoCanaryV0)
73            }
74            _ => bail!("Unsupported VM configuration for network: {}", N::ID),
75        }
76    }};
77}
78
79/// A helper macro to dedup the `Network` trait and `Aleo` trait and process its given logic.
80#[macro_export]
81macro_rules! process {
82    // Example: process!(self, logic)
83    ($self:ident, $logic:ident) => {{
84        match N::ID {
85            console::network::MainnetV0::ID => {
86                // Cast the process.
87                let process = (&$self.process as &dyn std::any::Any)
88                    .downcast_ref::<Arc<RwLock<Process<console::network::MainnetV0>>>>()
89                    .ok_or_else(|| anyhow!("Failed to downcast {}", stringify!($self.process)))?;
90                // Process the logic.
91                $logic!(process.read(), console::network::MainnetV0, circuit::AleoV0)
92            }
93            console::network::TestnetV0::ID => {
94                // Cast the process.
95                let process = (&$self.process as &dyn std::any::Any)
96                    .downcast_ref::<Arc<RwLock<Process<console::network::TestnetV0>>>>()
97                    .ok_or_else(|| anyhow!("Failed to downcast {}", stringify!($self.process)))?;
98                // Process the logic.
99                $logic!(process.read(), console::network::TestnetV0, circuit::AleoTestnetV0)
100            }
101            console::network::CanaryV0::ID => {
102                // Cast the process.
103                let process = (&$self.process as &dyn std::any::Any)
104                    .downcast_ref::<Arc<RwLock<Process<console::network::CanaryV0>>>>()
105                    .ok_or_else(|| anyhow!("Failed to downcast {}", stringify!($self.process)))?;
106                // Process the logic.
107                $logic!(process.read(), console::network::CanaryV0, circuit::AleoCanaryV0)
108            }
109            _ => bail!("Unsupported VM configuration for network: {}", N::ID),
110        }
111    }};
112}