diff --git a/quantinuum-hugr/src/hugr/rewrite.rs b/quantinuum-hugr/src/hugr/rewrite.rs index 151fcaca0..dd26b1ac2 100644 --- a/quantinuum-hugr/src/hugr/rewrite.rs +++ b/quantinuum-hugr/src/hugr/rewrite.rs @@ -18,10 +18,6 @@ pub trait Rewrite { type Error: std::error::Error; /// The type returned on successful application of the rewrite. type ApplyResult; - /// The node iterator returned by [`Rewrite::invalidation_set`] - type InvalidationSet<'a>: Iterator + 'a - where - Self: 'a; /// If `true`, [self.apply]'s of this rewrite guarantee that they do not mutate the Hugr when they return an Err. /// If `false`, there is no guarantee; the Hugr should be assumed invalid when Err is returned. @@ -47,7 +43,7 @@ pub trait Rewrite { /// /// Two `impl Rewrite`s can be composed if their invalidation sets are /// disjoint. - fn invalidation_set(&self) -> Self::InvalidationSet<'_>; + fn invalidation_set(&self) -> impl Iterator; } /// Wraps any rewrite into a transaction (i.e. that has no effect upon failure) @@ -60,9 +56,6 @@ pub struct Transactional { impl Rewrite for Transactional { type Error = R::Error; type ApplyResult = R::ApplyResult; - type InvalidationSet<'a> = R::InvalidationSet<'a> - where - Self: 'a; const UNCHANGED_ON_FAILURE: bool = true; fn verify(&self, h: &impl HugrView) -> Result<(), Self::Error> { @@ -93,7 +86,7 @@ impl Rewrite for Transactional { } #[inline] - fn invalidation_set(&self) -> Self::InvalidationSet<'_> { + fn invalidation_set(&self) -> impl Iterator { self.underlying.invalidation_set() } } diff --git a/quantinuum-hugr/src/hugr/rewrite/consts.rs b/quantinuum-hugr/src/hugr/rewrite/consts.rs index a42b7d2c9..3fbd0aabd 100644 --- a/quantinuum-hugr/src/hugr/rewrite/consts.rs +++ b/quantinuum-hugr/src/hugr/rewrite/consts.rs @@ -29,8 +29,6 @@ impl Rewrite for RemoveLoadConstant { // The Const node the LoadConstant was connected to. type ApplyResult = Node; - type InvalidationSet<'a> = iter::Once; - const UNCHANGED_ON_FAILURE: bool = true; fn verify(&self, h: &impl HugrView) -> Result<(), Self::Error> { @@ -64,7 +62,7 @@ impl Rewrite for RemoveLoadConstant { Ok(source) } - fn invalidation_set(&self) -> Self::InvalidationSet<'_> { + fn invalidation_set(&self) -> impl Iterator { iter::once(self.0) } } @@ -79,8 +77,6 @@ impl Rewrite for RemoveConst { // The parent of the Const node. type ApplyResult = Node; - type InvalidationSet<'a> = iter::Once; - const UNCHANGED_ON_FAILURE: bool = true; fn verify(&self, h: &impl HugrView) -> Result<(), Self::Error> { @@ -108,7 +104,7 @@ impl Rewrite for RemoveConst { Ok(parent) } - fn invalidation_set(&self) -> Self::InvalidationSet<'_> { + fn invalidation_set(&self) -> impl Iterator { iter::once(self.0) } } diff --git a/quantinuum-hugr/src/hugr/rewrite/inline_dfg.rs b/quantinuum-hugr/src/hugr/rewrite/inline_dfg.rs index 45b57baec..e5a6c4062 100644 --- a/quantinuum-hugr/src/hugr/rewrite/inline_dfg.rs +++ b/quantinuum-hugr/src/hugr/rewrite/inline_dfg.rs @@ -25,8 +25,6 @@ impl Rewrite for InlineDFG { type ApplyResult = [Node; 3]; type Error = InlineDFGError; - type InvalidationSet<'a> = <[Node; 1] as IntoIterator>::IntoIter; - const UNCHANGED_ON_FAILURE: bool = true; fn verify(&self, h: &impl crate::HugrView) -> Result<(), Self::Error> { @@ -122,7 +120,7 @@ impl Rewrite for InlineDFG { Ok([n, input, output]) } - fn invalidation_set(&self) -> Self::InvalidationSet<'_> { + fn invalidation_set(&self) -> impl Iterator { [self.0.node()].into_iter() } } diff --git a/quantinuum-hugr/src/hugr/rewrite/insert_identity.rs b/quantinuum-hugr/src/hugr/rewrite/insert_identity.rs index 540ff05cc..ee61db971 100644 --- a/quantinuum-hugr/src/hugr/rewrite/insert_identity.rs +++ b/quantinuum-hugr/src/hugr/rewrite/insert_identity.rs @@ -48,9 +48,6 @@ impl Rewrite for IdentityInsertion { type Error = IdentityInsertionError; /// The inserted node. type ApplyResult = Node; - type InvalidationSet<'a> = iter::Once - where - Self: 'a; const UNCHANGED_ON_FAILURE: bool = true; fn verify(&self, _h: &impl HugrView) -> Result<(), IdentityInsertionError> { /* @@ -90,7 +87,7 @@ impl Rewrite for IdentityInsertion { } #[inline] - fn invalidation_set(&self) -> Self::InvalidationSet<'_> { + fn invalidation_set(&self) -> impl Iterator { iter::once(self.post_node) } } diff --git a/quantinuum-hugr/src/hugr/rewrite/outline_cfg.rs b/quantinuum-hugr/src/hugr/rewrite/outline_cfg.rs index f6120e4a5..43f3b12f9 100644 --- a/quantinuum-hugr/src/hugr/rewrite/outline_cfg.rs +++ b/quantinuum-hugr/src/hugr/rewrite/outline_cfg.rs @@ -1,6 +1,5 @@ //! Rewrite for inserting a CFG-node into the hierarchy containing a subsection of an existing CFG -use std::collections::{hash_set, HashSet}; -use std::iter; +use std::collections::HashSet; use itertools::Itertools; use thiserror::Error; @@ -101,9 +100,6 @@ impl Rewrite for OutlineCfg { /// /// [CFG]: OpType::CFG type ApplyResult = (Node, Node); - type InvalidationSet<'a> = iter::Copied> - where - Self: 'a; const UNCHANGED_ON_FAILURE: bool = true; fn verify(&self, h: &impl HugrView) -> Result<(), OutlineCfgError> { @@ -216,8 +212,7 @@ impl Rewrite for OutlineCfg { Ok((new_block, cfg_node)) } - #[inline] - fn invalidation_set(&self) -> Self::InvalidationSet<'_> { + fn invalidation_set(&self) -> impl Iterator { self.blocks.iter().copied() } } diff --git a/quantinuum-hugr/src/hugr/rewrite/replace.rs b/quantinuum-hugr/src/hugr/rewrite/replace.rs index aa4dca469..dc54f6652 100644 --- a/quantinuum-hugr/src/hugr/rewrite/replace.rs +++ b/quantinuum-hugr/src/hugr/rewrite/replace.rs @@ -1,8 +1,6 @@ //! Implementation of the `Replace` operation. use std::collections::{HashMap, HashSet, VecDeque}; -use std::iter::Copied; -use std::slice::Iter; use itertools::Itertools; use thiserror::Error; @@ -215,10 +213,6 @@ impl Rewrite for Replacement { type ApplyResult = (); - type InvalidationSet<'a> = Copied> - where - Self: 'a; - const UNCHANGED_ON_FAILURE: bool = false; fn verify(&self, h: &impl crate::HugrView) -> Result<(), Self::Error> { @@ -331,7 +325,7 @@ impl Rewrite for Replacement { Ok(()) } - fn invalidation_set(&self) -> Self::InvalidationSet<'_> { + fn invalidation_set(&self) -> impl Iterator { self.removal.iter().copied() } } diff --git a/quantinuum-hugr/src/hugr/rewrite/simple_replace.rs b/quantinuum-hugr/src/hugr/rewrite/simple_replace.rs index f7d836d58..ebee0d1bf 100644 --- a/quantinuum-hugr/src/hugr/rewrite/simple_replace.rs +++ b/quantinuum-hugr/src/hugr/rewrite/simple_replace.rs @@ -1,8 +1,6 @@ //! Implementation of the `SimpleReplace` operation. -use std::collections::{hash_map, HashMap}; -use std::iter::{self, Copied}; -use std::slice; +use std::collections::HashMap; use crate::hugr::views::SiblingSubgraph; use crate::hugr::{HugrMut, HugrView, NodeMetadataMap, Rewrite}; @@ -55,19 +53,9 @@ impl SimpleReplacement { } } -type SubgraphNodesIter<'a> = Copied>; -type NuOutNodesIter<'a> = iter::Map< - hash_map::Keys<'a, (Node, IncomingPort), IncomingPort>, - fn(&'a (Node, IncomingPort)) -> Node, ->; - impl Rewrite for SimpleReplacement { type Error = SimpleReplacementError; type ApplyResult = (); - type InvalidationSet<'a> = iter::Chain, NuOutNodesIter<'a>> - where - Self: 'a; - const UNCHANGED_ON_FAILURE: bool = true; fn verify(&self, _h: &impl HugrView) -> Result<(), SimpleReplacementError> { @@ -184,10 +172,9 @@ impl Rewrite for SimpleReplacement { } #[inline] - fn invalidation_set(&self) -> Self::InvalidationSet<'_> { + fn invalidation_set(&self) -> impl Iterator { let subcirc = self.subgraph.nodes().iter().copied(); - let get_node: fn(&(Node, IncomingPort)) -> Node = |key| key.0; - let out_neighs = self.nu_out.keys().map(get_node); + let out_neighs = self.nu_out.keys().map(|key| key.0); subcirc.chain(out_neighs) } }