From 69a795073a11ccc1871aa45dd991589f67b0848d Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 7 Nov 2023 13:01:41 +0000 Subject: [PATCH] feat: impl GraphRef for PetgraphWrapper Since PetgraphWrapper is a reference to a HugrView, we implement GraphRef directly. This simplifies the implementation of several traits. --- src/hugr/views/petgraph.rs | 39 ++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/hugr/views/petgraph.rs b/src/hugr/views/petgraph.rs index 7fb3a9c84..9ae2a2331 100644 --- a/src/hugr/views/petgraph.rs +++ b/src/hugr/views/petgraph.rs @@ -12,11 +12,28 @@ use petgraph::visit as pv; /// Wrapper for a HugrView that implements petgraph's traits. /// /// It can be used to apply petgraph's algorithms to a Hugr. -#[derive(Debug, Clone, Copy)] +#[derive(Debug)] pub struct PetgraphWrapper<'a, T> { pub(crate) hugr: &'a T, } +impl<'a, T> Clone for PetgraphWrapper<'a, T> { + fn clone(&self) -> Self { + *self + } +} + +impl<'a, T> Copy for PetgraphWrapper<'a, T> {} + +impl<'a, T> From<&'a T> for PetgraphWrapper<'a, T> +where + T: HugrView, +{ + fn from(hugr: &'a T) -> Self { + Self { hugr } + } +} + impl<'a, T> pv::GraphBase for PetgraphWrapper<'a, T> where T: HugrView, @@ -32,6 +49,8 @@ where type EdgeType = petgraph::Directed; } +impl<'a, T> pv::GraphRef for PetgraphWrapper<'a, T> where T: HugrView {} + impl<'a, T> pv::NodeCount for PetgraphWrapper<'a, T> where T: HugrView, @@ -75,12 +94,12 @@ where type EdgeWeight = EdgeKind; } -impl<'g, 'a, T> pv::IntoNodeReferences for &'g PetgraphWrapper<'a, T> +impl<'a, T> pv::IntoNodeReferences for PetgraphWrapper<'a, T> where T: HugrView, { - type NodeRef = HugrNodeRef<'g>; - type NodeReferences = MapWithCtx<::Nodes<'g>, Self, HugrNodeRef<'g>>; + type NodeRef = HugrNodeRef<'a>; + type NodeReferences = MapWithCtx<::Nodes<'a>, Self, HugrNodeRef<'a>>; fn node_references(self) -> Self::NodeReferences { self.hugr @@ -90,33 +109,33 @@ where } } -impl<'g, 'a, T> pv::IntoNodeIdentifiers for &'g PetgraphWrapper<'a, T> +impl<'a, T> pv::IntoNodeIdentifiers for PetgraphWrapper<'a, T> where T: HugrView, { - type NodeIdentifiers = ::Nodes<'g>; + type NodeIdentifiers = ::Nodes<'a>; fn node_identifiers(self) -> Self::NodeIdentifiers { self.hugr.nodes() } } -impl<'g, 'a, T> pv::IntoNeighbors for &'g PetgraphWrapper<'a, T> +impl<'a, T> pv::IntoNeighbors for PetgraphWrapper<'a, T> where T: HugrView, { - type Neighbors = ::Neighbours<'g>; + type Neighbors = ::Neighbours<'a>; fn neighbors(self, n: Self::NodeId) -> Self::Neighbors { self.hugr.output_neighbours(n) } } -impl<'g, 'a, T> pv::IntoNeighborsDirected for &'g PetgraphWrapper<'a, T> +impl<'a, T> pv::IntoNeighborsDirected for PetgraphWrapper<'a, T> where T: HugrView, { - type NeighborsDirected = ::Neighbours<'g>; + type NeighborsDirected = ::Neighbours<'a>; fn neighbors_directed( self,