From 0a796a33906c72d9e735078835328cc950823d87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Borgna?= <121866228+aborgna-q@users.noreply.github.com> Date: Wed, 6 Sep 2023 12:01:02 +0200 Subject: [PATCH] feat: More general portgraph references (#494) --- Cargo.toml | 2 +- src/hugr/views.rs | 10 ++++++---- src/hugr/views/hierarchy.rs | 12 ++++++------ src/hugr/views/sibling.rs | 12 ++++++------ 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 708413edd..46d83d38b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ path = "src/lib.rs" [dependencies] thiserror = "1.0.28" -portgraph = { version = "0.8.0", features = ["serde", "petgraph"] } +portgraph = { version = "0.9.0", features = ["serde", "petgraph"] } pyo3 = { version = "0.19.0", optional = true, features = [ "multiple-pymethods", ] } diff --git a/src/hugr/views.rs b/src/hugr/views.rs index d882eca42..0db5d39d0 100644 --- a/src/hugr/views.rs +++ b/src/hugr/views.rs @@ -382,10 +382,12 @@ pub(crate) mod sealed { /// view. pub trait HugrInternals { /// The underlying portgraph view type. - type Portgraph: LinkView; + type Portgraph<'p>: LinkView + Clone + 'p + where + Self: 'p; /// Returns a reference to the underlying portgraph. - fn portgraph(&self) -> &Self::Portgraph; + fn portgraph(&self) -> Self::Portgraph<'_>; /// Returns the Hugr at the base of a chain of views. fn base_hugr(&self) -> &Hugr; @@ -398,10 +400,10 @@ pub(crate) mod sealed { where T: AsRef, { - type Portgraph = MultiPortGraph; + type Portgraph<'p> = &'p MultiPortGraph where Self: 'p; #[inline] - fn portgraph(&self) -> &Self::Portgraph { + fn portgraph(&self) -> Self::Portgraph<'_> { &self.as_ref().graph } diff --git a/src/hugr/views/hierarchy.rs b/src/hugr/views/hierarchy.rs index ed42d48b0..76d67ecff 100644 --- a/src/hugr/views/hierarchy.rs +++ b/src/hugr/views/hierarchy.rs @@ -29,7 +29,7 @@ use crate::{hugr::NodeType, hugr::OpType, Direction, Hugr, Node, Port}; use super::{sealed::HugrInternals, HugrView, NodeMetadata}; -type FlatRegionGraph<'g> = portgraph::view::FlatRegion<'g, MultiPortGraph>; +type FlatRegionGraph<'g> = portgraph::view::FlatRegion<'g, &'g MultiPortGraph>; /// View of a HUGR sibling graph. /// @@ -221,7 +221,7 @@ where } } -type RegionGraph<'g> = portgraph::view::Region<'g, MultiPortGraph>; +type RegionGraph<'g> = portgraph::view::Region<'g, &'g MultiPortGraph>; /// View of a HUGR descendants graph. /// @@ -483,10 +483,10 @@ where Root: NodeHandle, Base: HugrInternals, { - type Portgraph = FlatRegionGraph<'g>; + type Portgraph<'p> = &'p FlatRegionGraph<'g> where Self: 'p; #[inline] - fn portgraph(&self) -> &Self::Portgraph { + fn portgraph(&self) -> Self::Portgraph<'_> { &self.graph } @@ -506,10 +506,10 @@ where Root: NodeHandle, Base: HugrInternals, { - type Portgraph = RegionGraph<'g>; + type Portgraph<'p> = &'p RegionGraph<'g> where Self: 'p; #[inline] - fn portgraph(&self) -> &Self::Portgraph { + fn portgraph(&self) -> Self::Portgraph<'_> { &self.graph } diff --git a/src/hugr/views/sibling.rs b/src/hugr/views/sibling.rs index 002c55eba..c344be6b2 100644 --- a/src/hugr/views/sibling.rs +++ b/src/hugr/views/sibling.rs @@ -81,7 +81,7 @@ impl<'g, Base: HugrView> SiblingSubgraph<'g, Base> { /// subgraph is empty. pub fn try_from_dataflow_graph(dfg_graph: &'g Base) -> Result where - Base: HugrView, + Base: Clone + HugrView, Root: ContainerHandle, { let parent = dfg_graph.root(); @@ -147,7 +147,7 @@ impl<'g, Base: HugrView> SiblingSubgraph<'g, Base> { outgoing: OutgoingPorts, ) -> Result where - Base: HugrView, + Base: Clone + HugrView, { let mut checker = ConvexChecker::new(base); Self::try_from_boundary_ports_with_checker(base, incoming, outgoing, &mut checker) @@ -168,14 +168,14 @@ impl<'g, Base: HugrView> SiblingSubgraph<'g, Base> { checker: &mut ConvexChecker<'g, Base>, ) -> Result where - Base: HugrView, + Base: Clone + HugrView, { let pg = base.portgraph(); let to_pg = |(n, p): (Node, Port)| pg.port_index(n.index, p.offset).expect("invalid port"); // Ordering of the edges here is preserved and becomes ordering of the signature. let subpg = Subgraph::new_subgraph( - pg, + pg.clone(), inputs .iter() .flatten() @@ -377,8 +377,8 @@ impl<'g, Base: HugrView> SiblingSubgraph<'g, Base> { /// /// This can be used when constructing multiple sibling subgraphs to speed up /// convexity checking. -pub struct ConvexChecker<'g, Base: HugrView>( - portgraph::algorithms::ConvexChecker<&'g Base::Portgraph>, +pub struct ConvexChecker<'g, Base: 'g + HugrView>( + portgraph::algorithms::ConvexChecker>, ); impl<'g, Base: HugrView> ConvexChecker<'g, Base> {