From a4dee2c93c47293de04441b46726a89f16ebc78d Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 12 Sep 2023 13:12:34 +0100 Subject: [PATCH] Remove Base type-parameter to SiblingGraph/DescendantsGraph, call base_hugr in c'tor --- src/hugr/views.rs | 5 +---- src/hugr/views/descendants.rs | 32 ++++++++++---------------------- src/hugr/views/sibling.rs | 34 +++++++++++----------------------- 3 files changed, 22 insertions(+), 49 deletions(-) diff --git a/src/hugr/views.rs b/src/hugr/views.rs index 841b972400..d96c45edf4 100644 --- a/src/hugr/views.rs +++ b/src/hugr/views.rs @@ -300,11 +300,8 @@ pub trait HugrView: sealed::HugrInternals { /// A common trait for views of a HUGR hierarchical subgraph. pub trait HierarchyView<'a>: HugrView { - /// The base from which the subgraph is derived. - type Base; - /// Create a hierarchical view of a HUGR given a root node. - fn new(hugr: &'a Self::Base, root: Node) -> Self; + fn new(hugr: &'a impl HugrView, root: Node) -> Self; } impl HugrView for T diff --git a/src/hugr/views/descendants.rs b/src/hugr/views/descendants.rs index 4355ddb53c..748a748260 100644 --- a/src/hugr/views/descendants.rs +++ b/src/hugr/views/descendants.rs @@ -25,10 +25,7 @@ type RegionGraph<'g> = portgraph::view::Region<'g, &'g MultiPortGraph>; /// used interchangeably with [`SiblingGraph`]. /// /// [`SiblingGraph`]: super::SiblingGraph -pub struct DescendantsGraph<'g, Root = Node, Base = Hugr> -where - Base: HugrInternals, -{ +pub struct DescendantsGraph<'g, Root = Node> { /// The chosen root node. root: Node, @@ -36,26 +33,24 @@ where graph: RegionGraph<'g>, /// The node hierarchy. - hugr: &'g Base, + hugr: &'g Hugr, /// The operation handle of the root node. _phantom: std::marker::PhantomData, } -impl<'g, Root, Base: Clone> Clone for DescendantsGraph<'g, Root, Base> +impl<'g, Root> Clone for DescendantsGraph<'g, Root> where Root: NodeHandle, - Base: HugrInternals + HugrView, { fn clone(&self) -> Self { DescendantsGraph::new(self.hugr, self.root) } } -impl<'g, Root, Base> HugrView for DescendantsGraph<'g, Root, Base> +impl<'g, Root> HugrView for DescendantsGraph<'g, Root> where Root: NodeHandle, - Base: HugrInternals + HugrView, { type RootHandle = Root; @@ -173,36 +168,29 @@ where } } -impl<'a, Root, Base> HierarchyView<'a> for DescendantsGraph<'a, Root, Base> +impl<'a, Root> HierarchyView<'a> for DescendantsGraph<'a, Root> where Root: NodeHandle, - Base: HugrView, { - type Base = Base; - - fn new(hugr: &'a Base, root: Node) -> Self { + fn new(hugr: &'a impl HugrView, root: Node) -> Self { let root_tag = hugr.get_optype(root).tag(); if !Root::TAG.is_superset(root_tag) { // TODO: Return an error panic!("Root node must have the correct operation type tag.") } + let hugr = hugr.base_hugr(); Self { root, - graph: RegionGraph::new_region( - &hugr.base_hugr().graph, - &hugr.base_hugr().hierarchy, - root.index, - ), + graph: RegionGraph::new_region(&hugr.graph, &hugr.hierarchy, root.index), hugr, _phantom: std::marker::PhantomData, } } } -impl<'g, Root, Base> super::sealed::HugrInternals for DescendantsGraph<'g, Root, Base> +impl<'g, Root> super::sealed::HugrInternals for DescendantsGraph<'g, Root> where Root: NodeHandle, - Base: HugrInternals, { type Portgraph<'p> = &'p RegionGraph<'g> where Self: 'p; @@ -213,7 +201,7 @@ where #[inline] fn base_hugr(&self) -> &Hugr { - self.hugr.base_hugr() + self.hugr } #[inline] diff --git a/src/hugr/views/sibling.rs b/src/hugr/views/sibling.rs index 16640e367d..da5b035749 100644 --- a/src/hugr/views/sibling.rs +++ b/src/hugr/views/sibling.rs @@ -25,37 +25,32 @@ type FlatRegionGraph<'g> = portgraph::view::FlatRegion<'g, &'g MultiPortGraph>; /// used interchangeably with [`DescendantsGraph`]. /// /// [`DescendantsGraph`]: super::DescendantsGraph -pub struct SiblingGraph<'g, Root = Node, Base = Hugr> -where - Base: HugrInternals, -{ +pub struct SiblingGraph<'g, Root = Node> { /// The chosen root node. root: Node, /// The filtered portgraph encoding the adjacency structure of the HUGR. graph: FlatRegionGraph<'g>, - /// The rest of the HUGR. - hugr: &'g Base, + /// View onto the underlying Hugr which this graph filters + hugr: &'g Hugr, /// The operation type of the root node. _phantom: std::marker::PhantomData, } -impl<'g, Root, Base> Clone for SiblingGraph<'g, Root, Base> +impl<'g, Root> Clone for SiblingGraph<'g, Root> where Root: NodeHandle, - Base: HugrInternals + HugrView, { fn clone(&self) -> Self { SiblingGraph::new(self.hugr, self.root) } } -impl<'g, Root, Base> HugrView for SiblingGraph<'g, Root, Base> +impl<'g, Root> HugrView for SiblingGraph<'g, Root> where Root: NodeHandle, - Base: HugrInternals + HugrView, { type RootHandle = Root; @@ -186,36 +181,29 @@ where } } -impl<'a, Root, Base> HierarchyView<'a> for SiblingGraph<'a, Root, Base> +impl<'a, Root> HierarchyView<'a> for SiblingGraph<'a, Root> where Root: NodeHandle, - Base: HugrView, { - type Base = Base; - - fn new(hugr: &'a Base, root: Node) -> Self { + fn new(hugr: &'a impl HugrView, root: Node) -> Self { let root_tag = hugr.get_optype(root).tag(); if !Root::TAG.is_superset(root_tag) { // TODO: Return an error panic!("Root node must have the correct operation type tag.") } + let hugr = hugr.base_hugr(); Self { root, - graph: FlatRegionGraph::new_flat_region( - &hugr.base_hugr().graph, - &hugr.base_hugr().hierarchy, - root.index, - ), + graph: FlatRegionGraph::new_flat_region(&hugr.graph, &hugr.hierarchy, root.index), hugr, _phantom: std::marker::PhantomData, } } } -impl<'g, Root, Base> HugrInternals for SiblingGraph<'g, Root, Base> +impl<'g, Root> HugrInternals for SiblingGraph<'g, Root> where Root: NodeHandle, - Base: HugrInternals, { type Portgraph<'p> = &'p FlatRegionGraph<'g> where Self: 'p; @@ -226,7 +214,7 @@ where #[inline] fn base_hugr(&self) -> &Hugr { - self.hugr.base_hugr() + self.hugr } #[inline]