Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions hugr-core/src/hugr/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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`].
pub trait PatchVerification {
/// The type of Error with which this Rewrite may fail
type Error: std::error::Error;
Expand Down Expand Up @@ -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<H: HugrView>: PatchVerification<Node = H::Node> {
/// For patches that work on any `H: HugrMut`, prefer implementing
/// [`PatchHugrMut`] instead. This will automatically implement this trait.
pub trait Patch<H: NodesIter>: PatchVerification<Node = H::Node> {
/// The type returned on successful application of the rewrite.
type Outcome;

Expand Down
2 changes: 2 additions & 0 deletions hugr-core/src/hugr/views.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions hugr-core/src/hugr/views/nodes_iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::{HugrView, core::HugrNode, hugr::views::SiblingSubgraph};

/// A trait for iterating over nodes within a graph view (Hugr, SiblingSubgraph,
/// etc).
///
/// Iterating 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<Item = Self::Node> + '_;
}

impl<H: HugrView> NodesIter for H {
type Node = H::Node;

fn nodes(&self) -> impl Iterator<Item = Self::Node> + '_ {
self.nodes()
}
}

impl<N: HugrNode> NodesIter for SiblingSubgraph<N> {
type Node = N;

fn nodes(&self) -> impl Iterator<Item = Self::Node> + '_ {
self.nodes().iter().copied()
}
}
Loading