Skip to content

Commit 056da40

Browse files
authored
Add a root NodeHandle type param to HugrView (#398)
I'll say this closes #380. We could go further and add a type parameter in `Hugr<T: NodeHandle>`, but that would probably be annoying to use and it'd break the pyo3 machinery. Instead, most things should be interfacing via `HugrView`. #284 / #293 will move the builder references to views instead, so we will be able to statically check the root type.
1 parent d65d94f commit 056da40

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/hugr/views.rs

+9
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ use portgraph::{multiportgraph, LinkView, MultiPortGraph, PortView};
1111

1212
use super::{Hugr, NodeMetadata, NodeType};
1313
use super::{Node, Port};
14+
use crate::ops::handle::NodeHandle;
1415
use crate::ops::{OpName, OpTag, OpType};
1516
use crate::types::EdgeKind;
1617
use crate::Direction;
1718

1819
/// A trait for inspecting HUGRs.
1920
/// For end users we intend this to be superseded by region-specific APIs.
2021
pub trait HugrView: sealed::HugrInternals {
22+
/// The kind of handle that can be used to refer to the root node.
23+
///
24+
/// The handle is guaranteed to be able to contain the operation returned by
25+
/// [`HugrView::root_type`].
26+
type RootHandle: NodeHandle;
27+
2128
/// An Iterator over the nodes in a Hugr(View)
2229
type Nodes<'a>: Iterator<Item = Node>
2330
where
@@ -199,6 +206,8 @@ impl<T> HugrView for T
199206
where
200207
T: AsRef<Hugr>,
201208
{
209+
type RootHandle = Node;
210+
202211
/// An Iterator over the nodes in a Hugr(View)
203212
type Nodes<'a> = MapInto<multiportgraph::Nodes<'a>, Node> where Self: 'a;
204213

src/hugr/views/hierarchy.rs

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ where
6969
Root: NodeHandle,
7070
Base: HugrInternals + HugrView,
7171
{
72+
type RootHandle = Root;
73+
7274
type Nodes<'a> = iter::Chain<iter::Once<Node>, MapInto<portgraph::hierarchy::Children<'a>, Node>>
7375
where
7476
Self: 'a;
@@ -238,6 +240,8 @@ where
238240
Root: NodeHandle,
239241
Base: HugrInternals + HugrView,
240242
{
243+
type RootHandle = Root;
244+
241245
type Nodes<'a> = MapInto<<RegionGraph<'g, Base> as PortView>::Nodes<'a>, Node>
242246
where
243247
Self: 'a;

0 commit comments

Comments
 (0)