variants/
variants.rs

1use safety_net::{Identifier, Instantiable, Logic, Net, Netlist, Parameter, filter_nodes};
2
3#[derive(Debug, Clone)]
4enum Gate {
5    And(Identifier, Vec<Net>, Net),
6}
7
8impl Instantiable for Gate {
9    fn get_name(&self) -> &Identifier {
10        match self {
11            Gate::And(id, _, _) => id,
12        }
13    }
14
15    fn get_input_ports(&self) -> impl IntoIterator<Item = &Net> {
16        match self {
17            Gate::And(_, inputs, _) => inputs,
18        }
19    }
20
21    fn get_output_ports(&self) -> impl IntoIterator<Item = &Net> {
22        match self {
23            Gate::And(_, _, output) => std::slice::from_ref(output),
24        }
25    }
26
27    fn has_parameter(&self, _id: &Identifier) -> bool {
28        false
29    }
30
31    fn get_parameter(&self, _id: &Identifier) -> Option<Parameter> {
32        None
33    }
34
35    fn set_parameter(&mut self, _id: &Identifier, _val: Parameter) -> Option<Parameter> {
36        None
37    }
38
39    fn parameters(&self) -> impl Iterator<Item = (Identifier, Parameter)> {
40        std::iter::empty()
41    }
42
43    fn from_constant(_val: Logic) -> Option<Self> {
44        None
45    }
46
47    fn get_constant(&self) -> Option<Logic> {
48        None
49    }
50
51    fn is_seq(&self) -> bool {
52        false
53    }
54}
55
56fn and_gate() -> Gate {
57    Gate::And(
58        "AND".into(),
59        vec![Net::new_logic("A".into()), Net::new_logic("B".into())],
60        Net::new_logic("Y".into()),
61    )
62}
63
64fn main() {
65    let netlist = Netlist::new("example".to_string());
66
67    // Add the the two inputs
68    let a = netlist.insert_input("a".into());
69    let b = netlist.insert_input("b".into());
70
71    // Instantiate an AND gate
72    let instance = netlist
73        .insert_gate(and_gate(), "inst_0".into(), &[a, b])
74        .unwrap();
75
76    // Make this AND gate an output
77    instance.expose_with_name("y".into());
78
79    // Print the netlist
80    println!("{netlist}");
81    for node in filter_nodes!(netlist, Gate::And(_, _, _)) {
82        println!("Found AND gate: {node}");
83    }
84}