From 1affdf41f4227ee47e928da297f5ca56d0d082ce Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Thu, 7 Sep 2023 17:29:23 +0100 Subject: [PATCH 1/2] Move HugrView::get_function_type impl for Hugr to default --- src/hugr/views.rs | 19 +++++++++---------- src/hugr/views/descendants.rs | 4 ---- src/hugr/views/sibling.rs | 4 ---- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/hugr/views.rs b/src/hugr/views.rs index f26bfb3e9..cb66167e5 100644 --- a/src/hugr/views.rs +++ b/src/hugr/views.rs @@ -179,7 +179,15 @@ pub trait HugrView: sealed::HugrInternals { /// For function-like HUGRs (DFG, FuncDefn, FuncDecl), report the function /// type. Otherwise return None. - fn get_function_type(&self) -> Option<&FunctionType>; + fn get_function_type(&self) -> Option<&FunctionType> { + let op = self.get_nodetype(self.root()); + match &op.op { + OpType::DFG(DFG { signature }) + | OpType::FuncDecl(FuncDecl { signature, .. }) + | OpType::FuncDefn(FuncDefn { signature, .. }) => Some(signature), + _ => None, + } + } /// Return a wrapper over the view that can be used in petgraph algorithms. #[inline] @@ -379,15 +387,6 @@ where } } - fn get_function_type(&self) -> Option<&FunctionType> { - let op = self.get_nodetype(self.root()); - match &op.op { - OpType::DFG(DFG { signature }) - | OpType::FuncDecl(FuncDecl { signature, .. }) - | OpType::FuncDefn(FuncDefn { signature, .. }) => Some(signature), - _ => None, - } - } #[inline] fn get_metadata(&self, node: Node) -> &NodeMetadata { self.as_ref().metadata.get(node.index) diff --git a/src/hugr/views/descendants.rs b/src/hugr/views/descendants.rs index 508517f40..e37526032 100644 --- a/src/hugr/views/descendants.rs +++ b/src/hugr/views/descendants.rs @@ -194,10 +194,6 @@ where fn get_io(&self, node: Node) -> Option<[Node; 2]> { self.base_hugr().get_io(node) } - - fn get_function_type(&self) -> Option<&crate::types::FunctionType> { - self.base_hugr().get_function_type() - } } impl<'a, Root, Base> HierarchyView<'a> for DescendantsGraph<'a, Root, Base> diff --git a/src/hugr/views/sibling.rs b/src/hugr/views/sibling.rs index 86bc142d3..d2caf509d 100644 --- a/src/hugr/views/sibling.rs +++ b/src/hugr/views/sibling.rs @@ -204,10 +204,6 @@ where None } } - - fn get_function_type(&self) -> Option<&crate::types::FunctionType> { - self.base_hugr().get_function_type() - } } impl<'a, Root, Base> HierarchyView<'a> for SiblingGraph<'a, Root, Base> From 8070192835baa3a49c4c52c8e66d87a3c35bfec1 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Thu, 7 Sep 2023 17:38:54 +0100 Subject: [PATCH 2/2] Test --- src/hugr/views/descendants.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/hugr/views/descendants.rs b/src/hugr/views/descendants.rs index e37526032..73f7791a9 100644 --- a/src/hugr/views/descendants.rs +++ b/src/hugr/views/descendants.rs @@ -307,6 +307,16 @@ pub(super) mod test { || hugr.get_parent(n) == Some(inner))); assert_eq!(region.children(inner).count(), 2); + assert_eq!( + region.get_function_type(), + Some(&FunctionType::new(type_row![NAT, QB], type_row![NAT, QB])) + ); + let inner_region: DescendantsGraph = DescendantsGraph::new(&hugr, inner); + assert_eq!( + inner_region.get_function_type(), + Some(&FunctionType::new(type_row![NAT], type_row![NAT])) + ); + Ok(()) } }