Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: allow NodeType::new to take any Into<Option<ExtensionSet>> #511

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)),
croyzor marked this conversation as resolved.
Show resolved Hide resolved
),
)?;

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 {
croyzor marked this conversation as resolved.
Show resolved Hide resolved
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