diff --git a/src/circuit.rs b/src/circuit.rs index f36b03c0a..e7ffc4fc1 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -30,9 +30,11 @@ pub use hugr::{Node, Port, Wire}; // - Vertical slice iterator // - Gate count map // - Depth -pub trait Circuit<'circ>: HugrView { +pub trait Circuit: HugrView { /// An iterator over the commands in the circuit. - type Commands: Iterator; + type Commands<'a>: Iterator + where + Self: 'a; /// An iterator over the commands applied to an unit. type UnitCommands: Iterator; @@ -65,10 +67,10 @@ pub trait Circuit<'circ>: HugrView { /// Returns all the commands in the circuit, in some topological order. /// /// Ignores the Input and Output nodes. - fn commands(&'circ self) -> Self::Commands; + fn commands(&self) -> Self::Commands<'_>; /// Returns all the commands applied to the given unit, in order. - fn unit_commands(&'circ self) -> Self::UnitCommands; + fn unit_commands(&self) -> Self::UnitCommands; /// Returns the [`NodeType`] of a command. fn command_nodetype(&self, command: &Command) -> &NodeType { @@ -84,11 +86,11 @@ pub trait Circuit<'circ>: HugrView { fn num_gates(&self) -> usize; } -impl<'circ, T> Circuit<'circ> for T +impl Circuit for T where - T: 'circ + HugrView, + T: HugrView, { - type Commands = CommandIterator<'circ, T>; + type Commands<'a> = CommandIterator<'a, T> where Self: 'a; type UnitCommands = std::iter::Empty; #[inline] @@ -126,12 +128,12 @@ where } } - fn commands(&'circ self) -> Self::Commands { + fn commands(&self) -> Self::Commands<'_> { // Traverse the circuit in topological order. CommandIterator::new(self) } - fn unit_commands(&'circ self) -> Self::UnitCommands { + fn unit_commands(&self) -> Self::UnitCommands { // TODO Can we associate linear i/o with the corresponding unit without // doing the full toposort? unimplemented!() diff --git a/src/circuit/command.rs b/src/circuit/command.rs index 0620a3fe8..1f69e96f5 100644 --- a/src/circuit/command.rs +++ b/src/circuit/command.rs @@ -59,7 +59,7 @@ pub struct CommandIterator<'circ, Circ> { impl<'circ, Circ> CommandIterator<'circ, Circ> where - Circ: Circuit<'circ>, + Circ: Circuit, { /// Create a new iterator over the commands of a circuit. pub(super) fn new(circ: &'circ Circ) -> Self { @@ -157,7 +157,7 @@ where impl<'circ, Circ> Iterator for CommandIterator<'circ, Circ> where - Circ: Circuit<'circ>, + Circ: Circuit, { type Item = Command; @@ -181,7 +181,7 @@ where } } -impl<'circ, Circ> FusedIterator for CommandIterator<'circ, Circ> where Circ: Circuit<'circ> {} +impl<'circ, Circ> FusedIterator for CommandIterator<'circ, Circ> where Circ: Circuit {} #[cfg(test)] mod test { diff --git a/src/json.rs b/src/json.rs index 8470f35a9..82dd60363 100644 --- a/src/json.rs +++ b/src/json.rs @@ -38,7 +38,7 @@ pub trait TKETDecode: Sized { /// Convert the serialized circuit to a [`Hugr`]. fn decode(self) -> Result; /// Convert a [`Hugr`] to a new serialized circuit. - fn encode<'circ>(circuit: &'circ impl Circuit<'circ>) -> Result; + fn encode(circuit: &impl Circuit) -> Result; } impl TKETDecode for SerialCircuit { @@ -60,7 +60,7 @@ impl TKETDecode for SerialCircuit { Ok(decoder.finish()) } - fn encode<'circ>(circ: &'circ impl Circuit<'circ>) -> Result { + fn encode(circ: &impl Circuit) -> Result { let mut encoder = JsonEncoder::new(circ); for com in circ.commands() { let optype = circ.command_optype(&com); diff --git a/src/json/encoder.rs b/src/json/encoder.rs index 6495a7ec3..c592b0480 100644 --- a/src/json/encoder.rs +++ b/src/json/encoder.rs @@ -41,7 +41,7 @@ pub(super) struct JsonEncoder { impl JsonEncoder { /// Create a new [`JsonEncoder`] from a [`Circuit`]. - pub fn new<'circ>(circ: &impl Circuit<'circ>) -> Self { + pub fn new(circ: &impl Circuit) -> Self { let name = circ.name().map(str::to_string); // Compute the linear qubit and bit registers. Each one have independent diff --git a/src/portmatching/matcher.rs b/src/portmatching/matcher.rs index 6ad17c1c3..eac0696be 100644 --- a/src/portmatching/matcher.rs +++ b/src/portmatching/matcher.rs @@ -67,7 +67,7 @@ pub struct CircuitMatch<'a, 'p, C> { pub(super) root: Node, } -impl<'a, 'p, C: Circuit<'a> + Clone> CircuitMatch<'a, 'p, C> { +impl<'a, 'p, C: Circuit + Clone> CircuitMatch<'a, 'p, C> { /// Create a pattern match from the image of a pattern root. /// /// This checks at construction time that the match is convex. This will @@ -132,7 +132,7 @@ impl<'a, 'p, C: Circuit<'a> + Clone> CircuitMatch<'a, 'p, C> { } } -impl<'a, 'p, C: Circuit<'a>> Debug for CircuitMatch<'a, 'p, C> { +impl<'a, 'p, C: Circuit> Debug for CircuitMatch<'a, 'p, C> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("CircuitMatch") .field("root", &self.root) @@ -187,7 +187,7 @@ impl CircuitMatcher { } /// Find all convex pattern matches in a circuit. - pub fn find_matches<'a, 'm, C: Circuit<'a> + Clone>( + pub fn find_matches<'a, 'm, C: Circuit + Clone>( &'m self, circuit: &'a C, ) -> Vec> { @@ -199,7 +199,7 @@ impl CircuitMatcher { } /// Find all convex pattern matches in a circuit rooted at a given node. - fn find_rooted_matches<'a, 'm, C: Circuit<'a> + Clone>( + fn find_rooted_matches<'a, 'm, C: Circuit + Clone>( &'m self, circ: &'a C, root: Node, @@ -329,8 +329,8 @@ fn compatible_offsets((_, pout): &(Port, Port), (pin, _): &(Port, Port)) -> bool } /// Check if an edge `e` is valid in a portgraph `g` without weights. -pub(crate) fn validate_unweighted_edge<'circ>( - circ: &impl Circuit<'circ>, +pub(crate) fn validate_unweighted_edge( + circ: &impl Circuit, ) -> impl for<'a> Fn(Node, &'a PEdge) -> Option + '_ { move |src, &(src_port, tgt_port)| { let (next_node, _) = circ @@ -341,8 +341,8 @@ pub(crate) fn validate_unweighted_edge<'circ>( } /// Check if a node `n` is valid in a weighted portgraph `g`. -pub(crate) fn validate_weighted_node<'circ>( - circ: &impl Circuit<'circ>, +pub(crate) fn validate_weighted_node( + circ: &impl Circuit, ) -> impl for<'a> Fn(Node, &PNode) -> bool + '_ { move |v, prop| { let v_weight = MatchOp::try_from(circ.get_optype(v).clone()); diff --git a/src/portmatching/pattern.rs b/src/portmatching/pattern.rs index 7e7245872..46f9bc857 100644 --- a/src/portmatching/pattern.rs +++ b/src/portmatching/pattern.rs @@ -28,9 +28,7 @@ pub struct CircuitPattern { impl CircuitPattern { /// Construct a pattern from a circuit. - pub fn try_from_circuit<'circ, C: Circuit<'circ>>( - circuit: &'circ C, - ) -> Result { + pub fn try_from_circuit(circuit: &C) -> Result { if circuit.num_gates() == 0 { return Err(InvalidPattern::EmptyCircuit); } @@ -74,11 +72,7 @@ impl CircuitPattern { } /// Compute the map from pattern nodes to circuit nodes in `circ`. - pub fn get_match_map<'a, C: Circuit<'a>>( - &self, - root: Node, - circ: &C, - ) -> Option> { + pub fn get_match_map(&self, root: Node, circ: &C) -> Option> { let single_matcher = SinglePatternMatcher::from_pattern(self.pattern.clone()); single_matcher .get_match_map(