diff --git a/hugr-core/src/hugr/patch.rs b/hugr-core/src/hugr/patch.rs index 4273c9c8a7..ae26055a6a 100644 --- a/hugr-core/src/hugr/patch.rs +++ b/hugr-core/src/hugr/patch.rs @@ -13,6 +13,7 @@ pub mod simple_replace; use crate::HugrView; use crate::core::HugrNode; +use crate::hugr::views::NodesIter; use itertools::Itertools; pub use port_types::{BoundaryPort, HostPort, ReplacementPort}; pub use simple_replace::{SimpleReplacement, SimpleReplacementError}; @@ -21,6 +22,8 @@ use super::HugrMut; use super::views::ExtractionResult; /// Verify that a patch application would succeed. +// TODO: This trait should be parametrised on `H: NodesIter` to match the +// generality of [`Patch`], see https://github.com/CQCL/hugr/issues/2546. pub trait PatchVerification { /// The type of Error with which this Rewrite may fail type Error: std::error::Error; @@ -66,9 +69,9 @@ pub trait PatchVerification { /// /// ### When to implement /// -/// For patches that work on any `H: HugrMut`, prefer implementing [`PatchHugrMut`] instead. This -/// will automatically implement this trait. -pub trait Patch: PatchVerification { +/// For patches that work on any `H: HugrMut`, prefer implementing +/// [`PatchHugrMut`] instead. This will automatically implement this trait. +pub trait Patch: PatchVerification { /// The type returned on successful application of the rewrite. type Outcome; diff --git a/hugr-core/src/hugr/views.rs b/hugr-core/src/hugr/views.rs index c27d2eefb8..31e098ddff 100644 --- a/hugr-core/src/hugr/views.rs +++ b/hugr-core/src/hugr/views.rs @@ -1,6 +1,7 @@ //! Read-only access into HUGR graphs and subgraphs. mod impls; +mod nodes_iter; pub mod petgraph; pub mod render; mod rerooted; @@ -16,6 +17,7 @@ use std::collections::HashMap; pub use self::petgraph::PetgraphWrapper; #[allow(deprecated)] use self::render::{MermaidFormatter, RenderConfig}; +pub use nodes_iter::NodesIter; pub use rerooted::Rerooted; pub use root_checked::{InvalidSignature, RootCheckable, RootChecked, check_tag}; pub use sibling_subgraph::SiblingSubgraph; diff --git a/hugr-core/src/hugr/views/nodes_iter.rs b/hugr-core/src/hugr/views/nodes_iter.rs new file mode 100644 index 0000000000..7ff5c350b8 --- /dev/null +++ b/hugr-core/src/hugr/views/nodes_iter.rs @@ -0,0 +1,26 @@ +use crate::{HugrView, core::HugrNode, hugr::views::SiblingSubgraph}; + +/// Iterate over nodes within a graph view (Hugr, SiblingSubgraph, etc). +pub trait NodesIter { + /// The type of nodes in the graph. + type Node; + + /// Iterate over the nodes in the graph. + fn nodes(&self) -> impl Iterator + '_; +} + +impl NodesIter for H { + type Node = H::Node; + + fn nodes(&self) -> impl Iterator + '_ { + self.nodes() + } +} + +impl NodesIter for SiblingSubgraph { + type Node = N; + + fn nodes(&self) -> impl Iterator + '_ { + self.nodes().iter().copied() + } +}