Skip to content

Commit

Permalink
refactor: allow NodeType::new to take any Into<Option<ExtensionSet>> (#…
Browse files Browse the repository at this point in the history
…511)

This might be the most extreme refactor/rename/etc. that answers [your
comment](#510 (review))
- but, well, is there a reason *not* to do this? :)
  • Loading branch information
acl-cqc authored Sep 11, 2023
1 parent 3f6d3c8 commit edee1ef
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 26 deletions.
5 changes: 1 addition & 4 deletions src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,7 @@ pub trait Dataflow: Container {
let op = ops::DFG {
signature: signature.clone(),
};
let nodetype = match &input_extensions {
None => NodeType::open_extensions(op),
Some(rs) => NodeType::new(op, rs.clone()),
};
let nodetype = NodeType::new(op, input_extensions.clone());
let (dfg_n, _) = add_node_with_wires(self, nodetype, input_wires.into_iter().collect())?;

DFGBuilder::create_with_io(self.hugr_mut(), dfg_n, signature, input_extensions)
Expand Down
18 changes: 6 additions & 12 deletions src/builder/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,14 @@ impl<T: AsMut<Hugr> + AsRef<Hugr>> DFGBuilder<T> {
let output = ops::Output {
types: signature.output().clone(),
};
base.as_mut()
.add_node_with_parent(parent, NodeType::new(input, input_extensions.clone()))?;
base.as_mut().add_node_with_parent(
parent,
match &input_extensions {
None => NodeType::open_extensions(input),
Some(rs) => NodeType::new(input, rs.clone()),
},
)?;
base.as_mut().add_node_with_parent(
parent,
match input_extensions.map(|inp| inp.union(&signature.extension_reqs)) {
// TODO: Make this NodeType::open_extensions
None => NodeType::open_extensions(output),
Some(rs) => NodeType::new(output, rs),
},
NodeType::new(
output,
input_extensions.map(|inp| inp.union(&signature.extension_reqs)),
),
)?;

Ok(Self {
Expand Down
4 changes: 2 additions & 2 deletions src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ pub struct NodeType {

impl NodeType {
/// Create a new optype with some ExtensionSet
pub fn new(op: impl Into<OpType>, input_extensions: ExtensionSet) -> Self {
pub fn new(op: impl Into<OpType>, input_extensions: impl Into<Option<ExtensionSet>>) -> Self {
NodeType {
op: op.into(),
input_extensions: Some(input_extensions),
input_extensions: input_extensions.into(),
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/hugr/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ impl TryFrom<SerHugrV0> for Hugr {
// if there are any unconnected ports or copy nodes the capacity will be
// an underestimate
let mut hugr = Hugr::with_capacity(
match input_extensions {
None => NodeType::open_extensions(root_type),
Some(rs) => NodeType::new(root_type, rs),
},
NodeType::new(root_type, input_extensions),
nodes.len(),
edges.len() * 2,
);
Expand Down
5 changes: 1 addition & 4 deletions src/ops/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,7 @@ pub fn resolve_extension_ops(
// Only now can we perform the replacements as the 'for' loop was borrowing 'h' preventing use from using it mutably
for (n, op) in replacements {
let leaf: LeafOp = op.into();
let node_type = match h.get_nodetype(n).input_extensions() {
None => NodeType::open_extensions(leaf),
Some(exts) => NodeType::new(leaf, exts.clone()),
};
let node_type = NodeType::new(leaf, h.get_nodetype(n).input_extensions().cloned());
h.replace_op(n, node_type);
}
Ok(())
Expand Down

0 comments on commit edee1ef

Please sign in to comment.