-
Notifications
You must be signed in to change notification settings - Fork 7
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
feat!: HugrView API improvements #680
Changes from 15 commits
cd5e544
2b1bd37
b959e16
29350ee
7477cea
e6cc8e1
11ef7ba
0437efc
47d4646
2ffd29a
da9aea8
b2a2ae0
b4db711
39e1490
89e1f26
b1db6cc
1677014
5fd1035
09844a1
b124160
328ce45
44cb5b0
f7bfb46
920880e
2d1e47c
7ea609a
37ceea8
d31794d
4636bf7
0a6bb8a
9556156
f091b5a
451ce23
c1822db
65ed37a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,9 +139,8 @@ impl NewEdgeSpec { | |
true | ||
}; | ||
let found_incoming = h | ||
.linked_ports(self.tgt, tgt_pos) | ||
.exactly_one() | ||
.is_ok_and(|(src_n, _)| descends_from_legal(src_n)); | ||
.single_source(self.tgt, tgt_pos) | ||
.is_some_and(|(src_n, _)| descends_from_legal(src_n)); | ||
if !found_incoming { | ||
return Err(ReplaceError::NoRemovedEdge(err_edge())); | ||
}; | ||
|
@@ -617,7 +616,7 @@ mod test { | |
entry: bool, | ||
) -> Result<BasicBlockID, BuildError> { | ||
let op: OpType = op.into(); | ||
let op_sig = op.signature(); | ||
let op_sig = op.signature().expect("dataflow op needs signature"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmmm, we can't do something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, I've used this logic in ~3 places, there are probably more. (Could be a follow up task for builder to require |
||
let mut bb = if entry { | ||
assert_eq!( | ||
match h.hugr().get_optype(h.container_node()) { | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,8 +4,6 @@ use std::collections::{hash_map, HashMap}; | |||||||||||||
use std::iter::{self, Copied}; | ||||||||||||||
use std::slice; | ||||||||||||||
|
||||||||||||||
use itertools::Itertools; | ||||||||||||||
|
||||||||||||||
use crate::hugr::views::SiblingSubgraph; | ||||||||||||||
use crate::hugr::{HugrMut, HugrView, NodeMetadataMap, Rewrite}; | ||||||||||||||
use crate::ops::{OpTag, OpTrait, OpType}; | ||||||||||||||
|
@@ -129,11 +127,8 @@ impl Rewrite for SimpleReplacement { | |||||||||||||
for ((rep_inp_node, rep_inp_port), (rem_inp_node, rem_inp_port)) in &self.nu_inp { | ||||||||||||||
if self.replacement.get_optype(*rep_inp_node).tag() != OpTag::Output { | ||||||||||||||
// add edge from predecessor of (s_inp_node, s_inp_port) to (new_inp_node, n_inp_port) | ||||||||||||||
let (rem_inp_pred_node, rem_inp_pred_port) = h | ||||||||||||||
.linked_outputs(*rem_inp_node, *rem_inp_port) | ||||||||||||||
.exactly_one() | ||||||||||||||
.ok() // PortLinks does not implement Debug | ||||||||||||||
.unwrap(); | ||||||||||||||
let (rem_inp_pred_node, rem_inp_pred_port) = | ||||||||||||||
h.single_source(*rem_inp_node, *rem_inp_port).unwrap(); | ||||||||||||||
h.disconnect(*rem_inp_node, *rem_inp_port).unwrap(); | ||||||||||||||
let new_inp_node = index_map.get(rep_inp_node).unwrap(); | ||||||||||||||
h.connect( | ||||||||||||||
|
@@ -150,8 +145,7 @@ impl Rewrite for SimpleReplacement { | |||||||||||||
for ((rem_out_node, rem_out_port), rep_out_port) in &self.nu_out { | ||||||||||||||
let (rep_out_pred_node, rep_out_pred_port) = self | ||||||||||||||
.replacement | ||||||||||||||
.linked_outputs(replacement_output_node, *rep_out_port) | ||||||||||||||
.exactly_one() | ||||||||||||||
.single_source(replacement_output_node, *rep_out_port) | ||||||||||||||
.unwrap(); | ||||||||||||||
if self.replacement.get_optype(rep_out_pred_node).tag() != OpTag::Input { | ||||||||||||||
let new_out_node = index_map.get(&rep_out_pred_node).unwrap(); | ||||||||||||||
|
@@ -171,11 +165,8 @@ impl Rewrite for SimpleReplacement { | |||||||||||||
let rem_inp_nodeport = self.nu_inp.get(&(replacement_output_node, rep_out_port)); | ||||||||||||||
if let Some((rem_inp_node, rem_inp_port)) = rem_inp_nodeport { | ||||||||||||||
// add edge from predecessor of (rem_inp_node, rem_inp_port) to (rem_out_node, rem_out_port): | ||||||||||||||
let (rem_inp_pred_node, rem_inp_pred_port) = h | ||||||||||||||
.linked_outputs(*rem_inp_node, *rem_inp_port) | ||||||||||||||
.exactly_one() | ||||||||||||||
.ok() // PortLinks does not implement Debug | ||||||||||||||
.unwrap(); | ||||||||||||||
let (rem_inp_pred_node, rem_inp_pred_port) = | ||||||||||||||
h.single_source(*rem_inp_node, *rem_inp_port).unwrap(); | ||||||||||||||
h.disconnect(*rem_inp_node, *rem_inp_port).unwrap(); | ||||||||||||||
h.disconnect(*rem_out_node, *rem_out_port).unwrap(); | ||||||||||||||
h.connect( | ||||||||||||||
|
@@ -505,15 +496,27 @@ pub(in crate::hugr::rewrite) mod test { | |||||||||||||
.collect_vec(); | ||||||||||||||
let inputs = h | ||||||||||||||
.node_outputs(input) | ||||||||||||||
.filter(|&p| h.get_optype(input).signature().get(p).is_some()) | ||||||||||||||
.filter(|&p| { | ||||||||||||||
h.get_optype(input) | ||||||||||||||
.signature() | ||||||||||||||
.unwrap() | ||||||||||||||
.port_type(p) | ||||||||||||||
.is_some() | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps an Alternatively,
Suggested change
(there's many other cases of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've taken a different approach - casting to known types and using |
||||||||||||||
}) | ||||||||||||||
.map(|p| { | ||||||||||||||
let link = h.linked_inputs(input, p).next().unwrap(); | ||||||||||||||
(link, link) | ||||||||||||||
}) | ||||||||||||||
.collect(); | ||||||||||||||
let outputs = h | ||||||||||||||
.node_inputs(output) | ||||||||||||||
.filter(|&p| h.get_optype(output).signature().get(p).is_some()) | ||||||||||||||
.filter(|&p| { | ||||||||||||||
h.get_optype(output) | ||||||||||||||
.signature() | ||||||||||||||
.unwrap() | ||||||||||||||
.port_type(p) | ||||||||||||||
.is_some() | ||||||||||||||
}) | ||||||||||||||
.map(|p| ((output, p), p)) | ||||||||||||||
.collect(); | ||||||||||||||
h.apply_rewrite(SimpleReplacement::new( | ||||||||||||||
|
@@ -565,7 +568,7 @@ pub(in crate::hugr::rewrite) mod test { | |||||||||||||
|
||||||||||||||
let outputs = repl | ||||||||||||||
.node_inputs(repl_output) | ||||||||||||||
.filter(|&p| repl.get_optype(repl_output).signature().get(p).is_some()) | ||||||||||||||
.filter(|&p| repl.signature(repl_output).unwrap().port_type(p).is_some()) | ||||||||||||||
.map(|p| ((repl_output, p), p)) | ||||||||||||||
.collect(); | ||||||||||||||
|
||||||||||||||
|
@@ -598,7 +601,7 @@ pub(in crate::hugr::rewrite) mod test { | |||||||||||||
if *tgt == out { | ||||||||||||||
unimplemented!() | ||||||||||||||
}; | ||||||||||||||
let (src, src_port) = h.linked_outputs(*r_n, *r_p).exactly_one().ok().unwrap(); | ||||||||||||||
let (src, src_port) = h.single_source(*r_n, *r_p).unwrap(); | ||||||||||||||
NewEdgeSpec { | ||||||||||||||
src, | ||||||||||||||
tgt: *tgt, | ||||||||||||||
|
@@ -613,11 +616,7 @@ pub(in crate::hugr::rewrite) mod test { | |||||||||||||
.nu_out | ||||||||||||||
.iter() | ||||||||||||||
.map(|((tgt, tgt_port), out_port)| { | ||||||||||||||
let (src, src_port) = replacement | ||||||||||||||
.linked_outputs(out, *out_port) | ||||||||||||||
.exactly_one() | ||||||||||||||
.ok() | ||||||||||||||
.unwrap(); | ||||||||||||||
let (src, src_port) = replacement.single_source(out, *out_port).unwrap(); | ||||||||||||||
if src == in_ { | ||||||||||||||
unimplemented!() | ||||||||||||||
}; | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spicy nightly crimes...
This is alright for now, but we should not publish the crate until 1.75 is stable, and this can be removed.