Skip to content

Commit d276f39

Browse files
authored
Modify Instantiable interface to relax port storage type (#8)
* modify instantiable interface to relax port storage type * add back missing panic
1 parent 1cf361d commit d276f39

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

examples/lut.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ impl Instantiable for Lut {
3535
&self.id
3636
}
3737

38-
fn get_input_ports(&self) -> &[Net] {
38+
fn get_input_ports(&self) -> impl IntoIterator<Item = &Net> {
3939
&self.inputs
4040
}
4141

42-
fn get_output_ports(&self) -> &[Net] {
42+
fn get_output_ports(&self) -> impl IntoIterator<Item = &Net> {
4343
std::slice::from_ref(&self.output)
4444
}
4545

examples/variants.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ impl Instantiable for Gate {
1717
}
1818
}
1919

20-
fn get_input_ports(&self) -> &[Net] {
20+
fn get_input_ports(&self) -> impl IntoIterator<Item = &Net> {
2121
match self {
2222
Gate::And(_, inputs, _) => inputs,
2323
}
2424
}
2525

26-
fn get_output_ports(&self) -> &[Net] {
26+
fn get_output_ports(&self) -> impl IntoIterator<Item = &Net> {
2727
match self {
2828
Gate::And(_, _, output) => std::slice::from_ref(output),
2929
}

src/circuit.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,10 @@ pub trait Instantiable: Clone {
220220
fn get_name(&self) -> &Identifier;
221221

222222
/// Returns the input ports of the primitive
223-
fn get_input_ports(&self) -> &[Net];
223+
fn get_input_ports(&self) -> impl IntoIterator<Item = &Net>;
224224

225225
/// Returns the output ports of the primitive
226-
fn get_output_ports(&self) -> &[Net];
226+
fn get_output_ports(&self) -> impl IntoIterator<Item = &Net>;
227227

228228
/// Returns `true` if the type intakes a parameter with this name.
229229
fn has_parameter(&self, id: &Identifier) -> bool;
@@ -241,20 +241,34 @@ pub trait Instantiable: Clone {
241241

242242
/// Returns the single output port of the primitive.
243243
fn get_single_output_port(&self) -> &Net {
244-
if self.get_output_ports().len() > 1 {
244+
let mut iter = self.get_output_ports().into_iter();
245+
let ret = iter.next().expect("Primitive has no output ports");
246+
if iter.next().is_some() {
245247
panic!("Primitive has more than one output port");
246248
}
247-
self.get_input_ports().first().unwrap()
249+
ret
248250
}
249251

250252
/// Returns the output port at the given index.
253+
/// # Panics
254+
///
255+
/// If the index is out of bounds.
251256
fn get_output_port(&self, index: usize) -> &Net {
252-
&self.get_output_ports()[index]
257+
self.get_output_ports()
258+
.into_iter()
259+
.nth(index)
260+
.expect("Index out of bounds for output ports")
253261
}
254262

255-
/// Returns the input port of the primitive at index `index`.
263+
/// Returns the input port at the given index.
264+
/// # Panics
265+
///
266+
/// If the index is out of bounds.
256267
fn get_input_port(&self, index: usize) -> &Net {
257-
&self.get_input_ports()[index]
268+
self.get_input_ports()
269+
.into_iter()
270+
.nth(index)
271+
.expect("Index out of bounds for output ports")
258272
}
259273
}
260274

src/netlist.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ impl Instantiable for Gate {
3939
&self.name
4040
}
4141

42-
fn get_input_ports(&self) -> &[Net] {
42+
fn get_input_ports(&self) -> impl IntoIterator<Item = &Net> {
4343
&self.inputs
4444
}
4545

46-
fn get_output_ports(&self) -> &[Net] {
46+
fn get_output_ports(&self) -> impl IntoIterator<Item = &Net> {
4747
&self.outputs
4848
}
4949

@@ -597,7 +597,7 @@ where
597597
/// Returns the number of input ports for this circuit node.
598598
pub fn get_num_input_ports(&self) -> usize {
599599
if let Some(inst_type) = self.get_instance_type() {
600-
inst_type.get_input_ports().len()
600+
inst_type.get_input_ports().into_iter().count()
601601
} else {
602602
0
603603
}
@@ -1084,13 +1084,14 @@ where
10841084
) -> Result<NetRef<I>, String> {
10851085
let nets = inst_type
10861086
.get_output_ports()
1087-
.iter()
1087+
.into_iter()
10881088
.map(|pnet| pnet.with_name(format!("{}_{}", inst_name, pnet.get_identifier())))
10891089
.collect::<Vec<_>>();
1090-
if operands.len() != inst_type.get_input_ports().len() {
1090+
let input_count = inst_type.get_input_ports().into_iter().count();
1091+
if operands.len() != input_count {
10911092
return Err(format!(
10921093
"Expected {} operands, got {}",
1093-
inst_type.get_input_ports().len(),
1094+
input_count,
10941095
operands.len()
10951096
));
10961097
}
@@ -1106,13 +1107,19 @@ where
11061107
) -> Result<NetRef<I>, String> {
11071108
let nets = inst_type
11081109
.get_output_ports()
1109-
.iter()
1110+
.into_iter()
11101111
.map(|pnet| pnet.with_name(format!("{}_{}", inst_name, pnet.get_identifier())))
11111112
.collect::<Vec<_>>();
11121113
let object = Object::Instance(nets, inst_name, inst_type);
11131114
let index = self.objects.borrow().len();
11141115
let weak = Rc::downgrade(self);
1115-
let operands = vec![None; object.get_instance_type().unwrap().get_input_ports().len()];
1116+
let input_count = object
1117+
.get_instance_type()
1118+
.unwrap()
1119+
.get_input_ports()
1120+
.into_iter()
1121+
.count();
1122+
let operands = vec![None; input_count];
11161123
let owned_object = Rc::new(RefCell::new(OwnedObject {
11171124
object,
11181125
owner: weak,
@@ -1857,7 +1864,7 @@ where
18571864
writeln!(f, "{} (", inst_name.emit_name())?;
18581865
let level = 4;
18591866
let indent = " ".repeat(level);
1860-
for (idx, port) in inst_type.get_input_ports().iter().enumerate() {
1867+
for (idx, port) in inst_type.get_input_ports().into_iter().enumerate() {
18611868
let port_name = port.get_identifier().emit_name();
18621869
if let Some(operand) = owned.operands[idx].as_ref() {
18631870
let operand = match operand {

0 commit comments

Comments
 (0)