Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: HugrView::get_function_type #507

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 9 additions & 10 deletions src/hugr/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions src/hugr/views/descendants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -311,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(())
}
}
4 changes: 0 additions & 4 deletions src/hugr/views/sibling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down