From f97ba062bc3562e61ad76defef6e96db9dbc9189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Borgna?= <121866228+aborgna-q@users.noreply.github.com> Date: Wed, 29 May 2024 11:34:20 +0100 Subject: [PATCH] feat(hugr-core): Unseal and make public the traits `HugrInternals` and `HugrMutInternals` (#1122) Exposes `HugrInternals` and `HugrMutInternals` in `hugr_core::hugr::internal` (but not in `hugr`). Closes #1121 --- hugr-core/src/builder/module.rs | 12 +- hugr-core/src/extension/infer/test.rs | 2 +- hugr-core/src/hugr.rs | 1 + hugr-core/src/hugr/hugrmut.rs | 185 +------------------ hugr-core/src/hugr/internal.rs | 214 ++++++++++++++++++++++ hugr-core/src/hugr/rewrite/outline_cfg.rs | 2 +- hugr-core/src/hugr/rewrite/replace.rs | 2 +- hugr-core/src/hugr/serialize/test.rs | 2 +- hugr-core/src/hugr/validate/test.rs | 2 +- hugr-core/src/hugr/views.rs | 48 +---- hugr-core/src/hugr/views/descendants.rs | 4 +- hugr-core/src/hugr/views/root_checked.rs | 4 +- hugr-core/src/hugr/views/sibling.rs | 4 +- hugr-core/src/ops/custom.rs | 2 +- hugr/src/hugr.rs | 7 + hugr/src/lib.rs | 5 +- 16 files changed, 256 insertions(+), 240 deletions(-) create mode 100644 hugr-core/src/hugr/internal.rs create mode 100644 hugr/src/hugr.rs diff --git a/hugr-core/src/builder/module.rs b/hugr-core/src/builder/module.rs index f6b4b7f20..9866ab326 100644 --- a/hugr-core/src/builder/module.rs +++ b/hugr-core/src/builder/module.rs @@ -4,12 +4,12 @@ use super::{ BuildError, Container, }; -use crate::{ - extension::ExtensionRegistry, - hugr::{hugrmut::sealed::HugrMutInternals, views::HugrView, ValidationError}, - ops, - types::{PolyFuncType, Type, TypeBound}, -}; +use crate::extension::ExtensionRegistry; +use crate::hugr::internal::HugrMutInternals; +use crate::hugr::views::HugrView; +use crate::hugr::ValidationError; +use crate::ops; +use crate::types::{PolyFuncType, Type, TypeBound}; use crate::ops::handle::{AliasID, FuncID, NodeHandle}; diff --git a/hugr-core/src/extension/infer/test.rs b/hugr-core/src/extension/infer/test.rs index 74e0eb124..5954ae7ce 100644 --- a/hugr-core/src/extension/infer/test.rs +++ b/hugr-core/src/extension/infer/test.rs @@ -16,7 +16,7 @@ use crate::ops::{CustomOp, Lift, OpType}; use crate::{ builder::test::closed_dfg_root_hugr, extension::prelude::PRELUDE_ID, - hugr::{hugrmut::sealed::HugrMutInternals, validate::ValidationError}, + hugr::{internal::HugrMutInternals, validate::ValidationError}, ops::{dataflow::DataflowParent, handle::NodeHandle}, }; diff --git a/hugr-core/src/hugr.rs b/hugr-core/src/hugr.rs index f53dfe482..f54e56655 100644 --- a/hugr-core/src/hugr.rs +++ b/hugr-core/src/hugr.rs @@ -3,6 +3,7 @@ pub mod hugrmut; pub(crate) mod ident; +pub mod internal; pub mod rewrite; pub mod serialize; pub mod validate; diff --git a/hugr-core/src/hugr/hugrmut.rs b/hugr-core/src/hugr/hugrmut.rs index fe754de0a..c06d87fd5 100644 --- a/hugr-core/src/hugr/hugrmut.rs +++ b/hugr-core/src/hugr/hugrmut.rs @@ -2,18 +2,16 @@ use core::panic; use std::collections::HashMap; -use std::ops::Range; use portgraph::view::{NodeFilter, NodeFiltered}; use portgraph::{LinkMut, NodeIndex, PortMut, PortView, SecondaryMap}; use crate::hugr::views::SiblingSubgraph; -use crate::hugr::{Direction, HugrError, HugrView, Node, NodeType, RootTagged}; +use crate::hugr::{HugrView, Node, NodeType, RootTagged}; use crate::hugr::{NodeMetadata, Rewrite}; use crate::{Hugr, IncomingPort, OutgoingPort, Port, PortIndex}; -use self::sealed::HugrMutInternals; - +use super::internal::HugrMutInternals; use super::NodeMetadataMap; /// Functions for low-level building of a HUGR. @@ -465,7 +463,7 @@ fn insert_subgraph_internal( /// Panic if [`HugrView::valid_node`] fails. #[track_caller] -fn panic_invalid_node(hugr: &H, node: Node) { +pub(super) fn panic_invalid_node(hugr: &H, node: Node) { if !hugr.valid_node(node) { panic!( "Received an invalid node {node} while mutating a HUGR:\n\n {}", @@ -476,7 +474,7 @@ fn panic_invalid_node(hugr: &H, node: Node) { /// Panic if [`HugrView::valid_non_root`] fails. #[track_caller] -fn panic_invalid_non_root(hugr: &H, node: Node) { +pub(super) fn panic_invalid_non_root(hugr: &H, node: Node) { if !hugr.valid_non_root(node) { panic!( "Received an invalid non-root node {node} while mutating a HUGR:\n\n {}", @@ -487,7 +485,11 @@ fn panic_invalid_non_root(hugr: &H, node: Node) { /// Panic if [`HugrView::valid_node`] fails. #[track_caller] -fn panic_invalid_port(hugr: &H, node: Node, port: impl Into) { +pub(super) fn panic_invalid_port( + hugr: &H, + node: Node, + port: impl Into, +) { let port = port.into(); if hugr .portgraph() @@ -501,175 +503,6 @@ fn panic_invalid_port(hugr: &H, node: Node, port: impl Int } } -pub(crate) mod sealed { - use super::*; - use crate::ops::handle::NodeHandle; - - /// Trait for accessing the mutable internals of a Hugr(Mut). - /// - /// Specifically, this trait lets you apply arbitrary modifications that may - /// invalidate the HUGR. - pub trait HugrMutInternals: RootTagged { - /// Returns the Hugr at the base of a chain of views. - fn hugr_mut(&mut self) -> &mut Hugr; - - /// Set the number of ports on a node. This may invalidate the node's `PortIndex`. - /// - /// # Panics - /// - /// If the node is not in the graph. - fn set_num_ports(&mut self, node: Node, incoming: usize, outgoing: usize) { - panic_invalid_node(self, node); - self.hugr_mut().set_num_ports(node, incoming, outgoing) - } - - /// Alter the number of ports on a node and returns a range with the new - /// port offsets, if any. This may invalidate the node's `PortIndex`. - /// - /// The `direction` parameter specifies whether to add ports to the incoming - /// or outgoing list. - /// - /// # Panics - /// - /// If the node is not in the graph. - fn add_ports(&mut self, node: Node, direction: Direction, amount: isize) -> Range { - panic_invalid_node(self, node); - self.hugr_mut().add_ports(node, direction, amount) - } - - /// Sets the parent of a node. - /// - /// The node becomes the parent's last child. - /// - /// # Panics - /// - /// If either the node or the parent is not in the graph. - fn set_parent(&mut self, node: Node, parent: Node) { - panic_invalid_node(self, parent); - panic_invalid_non_root(self, node); - self.hugr_mut().set_parent(node, parent); - } - - /// Move a node in the hierarchy to be the subsequent sibling of another - /// node. - /// - /// The sibling node's parent becomes the new node's parent. - /// - /// The node becomes the parent's last child. - /// - /// # Panics - /// - /// If either node is not in the graph, or if it is a root. - fn move_after_sibling(&mut self, node: Node, after: Node) { - panic_invalid_non_root(self, node); - panic_invalid_non_root(self, after); - self.hugr_mut().move_after_sibling(node, after); - } - - /// Move a node in the hierarchy to be the prior sibling of another node. - /// - /// The sibling node's parent becomes the new node's parent. - /// - /// The node becomes the parent's last child. - /// - /// # Panics - /// - /// If either node is not in the graph, or if it is a root. - fn move_before_sibling(&mut self, node: Node, before: Node) { - panic_invalid_non_root(self, node); - panic_invalid_non_root(self, before); - self.hugr_mut().move_before_sibling(node, before) - } - - /// Replace the OpType at node and return the old OpType. - /// In general this invalidates the ports, which may need to be resized to - /// match the OpType signature. - /// TODO: Add a version which ignores input extensions - /// - /// # Errors - /// - /// Returns a [`HugrError::InvalidTag`] if this would break the bound - /// ([`Self::RootHandle`]) on the root node's [OpTag]. - /// - /// # Panics - /// - /// If the node is not in the graph. - fn replace_op(&mut self, node: Node, op: NodeType) -> Result { - panic_invalid_node(self, node); - if node == self.root() && !Self::RootHandle::TAG.is_superset(op.tag()) { - return Err(HugrError::InvalidTag { - required: Self::RootHandle::TAG, - actual: op.tag(), - }); - } - self.hugr_mut().replace_op(node, op) - } - } - - /// Impl for non-wrapped Hugrs. Overwrites the recursive default-impls to directly use the hugr. - impl + AsMut> HugrMutInternals for T { - fn hugr_mut(&mut self) -> &mut Hugr { - self.as_mut() - } - - #[inline] - fn set_num_ports(&mut self, node: Node, incoming: usize, outgoing: usize) { - self.hugr_mut() - .graph - .set_num_ports(node.pg_index(), incoming, outgoing, |_, _| {}) - } - - fn add_ports(&mut self, node: Node, direction: Direction, amount: isize) -> Range { - let mut incoming = self.hugr_mut().graph.num_inputs(node.pg_index()); - let mut outgoing = self.hugr_mut().graph.num_outputs(node.pg_index()); - let increment = |num: &mut usize| { - let new = num.saturating_add_signed(amount); - let range = *num..new; - *num = new; - range - }; - let range = match direction { - Direction::Incoming => increment(&mut incoming), - Direction::Outgoing => increment(&mut outgoing), - }; - self.hugr_mut() - .graph - .set_num_ports(node.pg_index(), incoming, outgoing, |_, _| {}); - range - } - - fn set_parent(&mut self, node: Node, parent: Node) { - self.hugr_mut().hierarchy.detach(node.pg_index()); - self.hugr_mut() - .hierarchy - .push_child(node.pg_index(), parent.pg_index()) - .expect("Inserting a newly-created node into the hierarchy should never fail."); - } - - fn move_after_sibling(&mut self, node: Node, after: Node) { - self.hugr_mut().hierarchy.detach(node.pg_index()); - self.hugr_mut() - .hierarchy - .insert_after(node.pg_index(), after.pg_index()) - .expect("Inserting a newly-created node into the hierarchy should never fail."); - } - - fn move_before_sibling(&mut self, node: Node, before: Node) { - self.hugr_mut().hierarchy.detach(node.pg_index()); - self.hugr_mut() - .hierarchy - .insert_before(node.pg_index(), before.pg_index()) - .expect("Inserting a newly-created node into the hierarchy should never fail."); - } - - fn replace_op(&mut self, node: Node, op: NodeType) -> Result { - // We know RootHandle=Node here so no need to check - let cur = self.hugr_mut().op_types.get_mut(node.pg_index()); - Ok(std::mem::replace(cur, op)) - } - } -} - #[cfg(test)] mod test { use crate::{ diff --git a/hugr-core/src/hugr/internal.rs b/hugr-core/src/hugr/internal.rs new file mode 100644 index 000000000..3cf2b9c63 --- /dev/null +++ b/hugr-core/src/hugr/internal.rs @@ -0,0 +1,214 @@ +//! Internal traits, not exposed in the public `hugr` API. + +use std::ops::Range; + +use portgraph::{LinkView, MultiPortGraph, PortMut, PortView}; + +use crate::ops::handle::NodeHandle; +use crate::{Direction, Hugr, Node}; + +use super::hugrmut::{panic_invalid_node, panic_invalid_non_root}; +use super::{HugrError, NodeType, RootTagged}; + +/// Trait for accessing the internals of a Hugr(View). +/// +/// Specifically, this trait provides access to the underlying portgraph +/// view. +pub trait HugrInternals { + /// The underlying portgraph view type. + type Portgraph<'p>: LinkView + Clone + 'p + where + Self: 'p; + + /// Returns a reference to the underlying portgraph. + fn portgraph(&self) -> Self::Portgraph<'_>; + + /// Returns the Hugr at the base of a chain of views. + fn base_hugr(&self) -> &Hugr; + + /// Return the root node of this view. + fn root_node(&self) -> Node; +} + +impl> HugrInternals for T { + type Portgraph<'p> = &'p MultiPortGraph where Self: 'p; + + #[inline] + fn portgraph(&self) -> Self::Portgraph<'_> { + &self.as_ref().graph + } + + #[inline] + fn base_hugr(&self) -> &Hugr { + self.as_ref() + } + + #[inline] + fn root_node(&self) -> Node { + self.as_ref().root.into() + } +} + +/// Trait for accessing the mutable internals of a Hugr(Mut). +/// +/// Specifically, this trait lets you apply arbitrary modifications that may +/// invalidate the HUGR. +pub trait HugrMutInternals: RootTagged { + /// Returns the Hugr at the base of a chain of views. + fn hugr_mut(&mut self) -> &mut Hugr; + + /// Set the number of ports on a node. This may invalidate the node's `PortIndex`. + /// + /// # Panics + /// + /// If the node is not in the graph. + fn set_num_ports(&mut self, node: Node, incoming: usize, outgoing: usize) { + panic_invalid_node(self, node); + self.hugr_mut().set_num_ports(node, incoming, outgoing) + } + + /// Alter the number of ports on a node and returns a range with the new + /// port offsets, if any. This may invalidate the node's `PortIndex`. + /// + /// The `direction` parameter specifies whether to add ports to the incoming + /// or outgoing list. + /// + /// # Panics + /// + /// If the node is not in the graph. + fn add_ports(&mut self, node: Node, direction: Direction, amount: isize) -> Range { + panic_invalid_node(self, node); + self.hugr_mut().add_ports(node, direction, amount) + } + + /// Sets the parent of a node. + /// + /// The node becomes the parent's last child. + /// + /// # Panics + /// + /// If either the node or the parent is not in the graph. + fn set_parent(&mut self, node: Node, parent: Node) { + panic_invalid_node(self, parent); + panic_invalid_non_root(self, node); + self.hugr_mut().set_parent(node, parent); + } + + /// Move a node in the hierarchy to be the subsequent sibling of another + /// node. + /// + /// The sibling node's parent becomes the new node's parent. + /// + /// The node becomes the parent's last child. + /// + /// # Panics + /// + /// If either node is not in the graph, or if it is a root. + fn move_after_sibling(&mut self, node: Node, after: Node) { + panic_invalid_non_root(self, node); + panic_invalid_non_root(self, after); + self.hugr_mut().move_after_sibling(node, after); + } + + /// Move a node in the hierarchy to be the prior sibling of another node. + /// + /// The sibling node's parent becomes the new node's parent. + /// + /// The node becomes the parent's last child. + /// + /// # Panics + /// + /// If either node is not in the graph, or if it is a root. + fn move_before_sibling(&mut self, node: Node, before: Node) { + panic_invalid_non_root(self, node); + panic_invalid_non_root(self, before); + self.hugr_mut().move_before_sibling(node, before) + } + + /// Replace the OpType at node and return the old OpType. + /// In general this invalidates the ports, which may need to be resized to + /// match the OpType signature. + /// TODO: Add a version which ignores input extensions + /// + /// # Errors + /// + /// Returns a [`HugrError::InvalidTag`] if this would break the bound + /// (`Self::RootHandle`) on the root node's OpTag. + /// + /// # Panics + /// + /// If the node is not in the graph. + fn replace_op(&mut self, node: Node, op: NodeType) -> Result { + panic_invalid_node(self, node); + if node == self.root() && !Self::RootHandle::TAG.is_superset(op.tag()) { + return Err(HugrError::InvalidTag { + required: Self::RootHandle::TAG, + actual: op.tag(), + }); + } + self.hugr_mut().replace_op(node, op) + } +} + +/// Impl for non-wrapped Hugrs. Overwrites the recursive default-impls to directly use the hugr. +impl + AsMut> HugrMutInternals for T { + fn hugr_mut(&mut self) -> &mut Hugr { + self.as_mut() + } + + #[inline] + fn set_num_ports(&mut self, node: Node, incoming: usize, outgoing: usize) { + self.hugr_mut() + .graph + .set_num_ports(node.pg_index(), incoming, outgoing, |_, _| {}) + } + + fn add_ports(&mut self, node: Node, direction: Direction, amount: isize) -> Range { + let mut incoming = self.hugr_mut().graph.num_inputs(node.pg_index()); + let mut outgoing = self.hugr_mut().graph.num_outputs(node.pg_index()); + let increment = |num: &mut usize| { + let new = num.saturating_add_signed(amount); + let range = *num..new; + *num = new; + range + }; + let range = match direction { + Direction::Incoming => increment(&mut incoming), + Direction::Outgoing => increment(&mut outgoing), + }; + self.hugr_mut() + .graph + .set_num_ports(node.pg_index(), incoming, outgoing, |_, _| {}); + range + } + + fn set_parent(&mut self, node: Node, parent: Node) { + self.hugr_mut().hierarchy.detach(node.pg_index()); + self.hugr_mut() + .hierarchy + .push_child(node.pg_index(), parent.pg_index()) + .expect("Inserting a newly-created node into the hierarchy should never fail."); + } + + fn move_after_sibling(&mut self, node: Node, after: Node) { + self.hugr_mut().hierarchy.detach(node.pg_index()); + self.hugr_mut() + .hierarchy + .insert_after(node.pg_index(), after.pg_index()) + .expect("Inserting a newly-created node into the hierarchy should never fail."); + } + + fn move_before_sibling(&mut self, node: Node, before: Node) { + self.hugr_mut().hierarchy.detach(node.pg_index()); + self.hugr_mut() + .hierarchy + .insert_before(node.pg_index(), before.pg_index()) + .expect("Inserting a newly-created node into the hierarchy should never fail."); + } + + fn replace_op(&mut self, node: Node, op: NodeType) -> Result { + // We know RootHandle=Node here so no need to check + let cur = self.hugr_mut().op_types.get_mut(node.pg_index()); + Ok(std::mem::replace(cur, op)) + } +} diff --git a/hugr-core/src/hugr/rewrite/outline_cfg.rs b/hugr-core/src/hugr/rewrite/outline_cfg.rs index a6badb72d..335a7a44d 100644 --- a/hugr-core/src/hugr/rewrite/outline_cfg.rs +++ b/hugr-core/src/hugr/rewrite/outline_cfg.rs @@ -6,7 +6,7 @@ use thiserror::Error; use crate::builder::{BlockBuilder, Container, Dataflow, SubContainer}; use crate::extension::ExtensionSet; -use crate::hugr::hugrmut::sealed::HugrMutInternals; +use crate::hugr::internal::HugrMutInternals; use crate::hugr::rewrite::Rewrite; use crate::hugr::views::sibling::SiblingMut; use crate::hugr::{HugrMut, HugrView}; diff --git a/hugr-core/src/hugr/rewrite/replace.rs b/hugr-core/src/hugr/rewrite/replace.rs index 5b2ed2948..9cc028053 100644 --- a/hugr-core/src/hugr/rewrite/replace.rs +++ b/hugr-core/src/hugr/rewrite/replace.rs @@ -453,7 +453,7 @@ mod test { use crate::extension::{ ExtensionId, ExtensionRegistry, ExtensionSet, PRELUDE, PRELUDE_REGISTRY, }; - use crate::hugr::hugrmut::sealed::HugrMutInternals; + use crate::hugr::internal::HugrMutInternals; use crate::hugr::rewrite::replace::WhichHugr; use crate::hugr::{HugrMut, NodeType, Rewrite}; use crate::ops::custom::{CustomOp, OpaqueOp}; diff --git a/hugr-core/src/hugr/serialize/test.rs b/hugr-core/src/hugr/serialize/test.rs index ab86c1e33..1a2f2b54c 100644 --- a/hugr-core/src/hugr/serialize/test.rs +++ b/hugr-core/src/hugr/serialize/test.rs @@ -6,7 +6,7 @@ use crate::builder::{ use crate::extension::prelude::{BOOL_T, PRELUDE_ID, QB_T, USIZE_T}; use crate::extension::simple_op::MakeRegisteredOp; use crate::extension::{EMPTY_REG, PRELUDE_REGISTRY}; -use crate::hugr::hugrmut::sealed::HugrMutInternals; +use crate::hugr::internal::HugrMutInternals; use crate::ops::custom::{ExtensionOp, OpaqueOp}; use crate::ops::{self, dataflow::IOTrait, Input, Module, Noop, Output, Value, DFG}; use crate::std_extensions::arithmetic::float_types::FLOAT64_TYPE; diff --git a/hugr-core/src/hugr/validate/test.rs b/hugr-core/src/hugr/validate/test.rs index 34e5e6abc..8e8e1f0c5 100644 --- a/hugr-core/src/hugr/validate/test.rs +++ b/hugr-core/src/hugr/validate/test.rs @@ -9,7 +9,7 @@ use crate::builder::{ }; use crate::extension::prelude::{BOOL_T, PRELUDE, PRELUDE_ID, USIZE_T}; use crate::extension::{Extension, ExtensionSet, TypeDefBound, EMPTY_REG, PRELUDE_REGISTRY}; -use crate::hugr::hugrmut::sealed::HugrMutInternals; +use crate::hugr::internal::HugrMutInternals; use crate::hugr::HugrMut; use crate::ops::dataflow::{IOTrait, LoadFunction}; use crate::ops::handle::NodeHandle; diff --git a/hugr-core/src/hugr/views.rs b/hugr-core/src/hugr/views.rs index a22cd309c..8ad14190f 100644 --- a/hugr-core/src/hugr/views.rs +++ b/hugr-core/src/hugr/views.rs @@ -22,8 +22,9 @@ pub use sibling_subgraph::SiblingSubgraph; use context_iterators::{ContextIterator, IntoContextIterator, MapWithCtx}; use itertools::{Itertools, MapInto}; use portgraph::render::{DotFormat, MermaidFormat}; -use portgraph::{multiportgraph, LinkView, MultiPortGraph, PortView}; +use portgraph::{multiportgraph, LinkView, PortView}; +use super::internal::HugrInternals; use super::{ Hugr, HugrError, NodeMetadata, NodeMetadataMap, NodeType, ValidationError, DEFAULT_NODETYPE, }; @@ -39,7 +40,7 @@ use itertools::Either; /// A trait for inspecting HUGRs. /// For end users we intend this to be superseded by region-specific APIs. -pub trait HugrView: sealed::HugrInternals { +pub trait HugrView: HugrInternals { /// An Iterator over the nodes in a Hugr(View) type Nodes<'a>: Iterator where @@ -669,46 +670,3 @@ where P: Into + Copy, { } - -pub(crate) mod sealed { - use super::*; - - /// Trait for accessing the internals of a Hugr(View). - /// - /// Specifically, this trait provides access to the underlying portgraph - /// view. - pub trait HugrInternals { - /// The underlying portgraph view type. - type Portgraph<'p>: LinkView + Clone + 'p - where - Self: 'p; - - /// Returns a reference to the underlying portgraph. - fn portgraph(&self) -> Self::Portgraph<'_>; - - /// Returns the Hugr at the base of a chain of views. - fn base_hugr(&self) -> &Hugr; - - /// Return the root node of this view. - fn root_node(&self) -> Node; - } - - impl> HugrInternals for T { - type Portgraph<'p> = &'p MultiPortGraph where Self: 'p; - - #[inline] - fn portgraph(&self) -> Self::Portgraph<'_> { - &self.as_ref().graph - } - - #[inline] - fn base_hugr(&self) -> &Hugr { - self.as_ref() - } - - #[inline] - fn root_node(&self) -> Node { - self.as_ref().root.into() - } - } -} diff --git a/hugr-core/src/hugr/views/descendants.rs b/hugr-core/src/hugr/views/descendants.rs index 71df55524..05d57ce90 100644 --- a/hugr-core/src/hugr/views/descendants.rs +++ b/hugr-core/src/hugr/views/descendants.rs @@ -9,7 +9,7 @@ use crate::hugr::HugrError; use crate::ops::handle::NodeHandle; use crate::{Direction, Hugr, Node, Port}; -use super::{check_tag, sealed::HugrInternals, HierarchyView, HugrView, RootTagged}; +use super::{check_tag, HierarchyView, HugrInternals, HugrView, RootTagged}; type RegionGraph<'g> = portgraph::view::Region<'g, &'g MultiPortGraph>; @@ -175,7 +175,7 @@ where } } -impl<'g, Root> super::sealed::HugrInternals for DescendantsGraph<'g, Root> +impl<'g, Root> super::HugrInternals for DescendantsGraph<'g, Root> where Root: NodeHandle, { diff --git a/hugr-core/src/hugr/views/root_checked.rs b/hugr-core/src/hugr/views/root_checked.rs index 8bde3c587..d8d52a7ae 100644 --- a/hugr-core/src/hugr/views/root_checked.rs +++ b/hugr-core/src/hugr/views/root_checked.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use crate::hugr::hugrmut::sealed::HugrMutInternals; +use crate::hugr::internal::HugrMutInternals; use crate::hugr::{HugrError, HugrMut}; use crate::ops::handle::NodeHandle; use crate::{Hugr, Node}; @@ -71,7 +71,7 @@ impl, Root: NodeHandle> HugrMut for RootChecke mod test { use super::RootChecked; use crate::extension::ExtensionSet; - use crate::hugr::hugrmut::sealed::HugrMutInternals; + use crate::hugr::internal::HugrMutInternals; use crate::hugr::{HugrError, HugrMut, NodeType}; use crate::ops::handle::{BasicBlockID, CfgID, DataflowParentID, DfgID}; use crate::ops::{DataflowBlock, MakeTuple, OpTag}; diff --git a/hugr-core/src/hugr/views/sibling.rs b/hugr-core/src/hugr/views/sibling.rs index 7d69cc4b0..90723b9e4 100644 --- a/hugr-core/src/hugr/views/sibling.rs +++ b/hugr-core/src/hugr/views/sibling.rs @@ -6,12 +6,12 @@ use context_iterators::{ContextIterator, IntoContextIterator, MapWithCtx}; use itertools::{Itertools, MapInto}; use portgraph::{LinkView, MultiPortGraph, PortIndex, PortView}; -use crate::hugr::hugrmut::sealed::HugrMutInternals; +use crate::hugr::internal::HugrMutInternals; use crate::hugr::{HugrError, HugrMut}; use crate::ops::handle::NodeHandle; use crate::{Direction, Hugr, Node, Port}; -use super::{check_tag, sealed::HugrInternals, HierarchyView, HugrView, RootTagged}; +use super::{check_tag, HierarchyView, HugrInternals, HugrView, RootTagged}; type FlatRegionGraph<'g> = portgraph::view::FlatRegion<'g, &'g MultiPortGraph>; diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index 31248385f..e8e7e0e2a 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -9,7 +9,7 @@ use { }; use crate::extension::{ConstFoldResult, ExtensionId, ExtensionRegistry, OpDef, SignatureError}; -use crate::hugr::hugrmut::sealed::HugrMutInternals; +use crate::hugr::internal::HugrMutInternals; use crate::hugr::{HugrView, NodeType}; use crate::types::EdgeKind; use crate::types::{type_param::TypeArg, FunctionType}; diff --git a/hugr/src/hugr.rs b/hugr/src/hugr.rs new file mode 100644 index 000000000..281bd80d8 --- /dev/null +++ b/hugr/src/hugr.rs @@ -0,0 +1,7 @@ +//! The Hugr data structure, and its basic component handles. + +// Exports everything except the `internal` module. +pub use hugr_core::hugr::{ + hugrmut, rewrite, serialize, validate, views, Hugr, HugrError, NodeMetadata, NodeMetadataMap, + NodeType, DEFAULT_NODETYPE, +}; diff --git a/hugr/src/lib.rs b/hugr/src/lib.rs index d626d2dd8..7e2562cf3 100644 --- a/hugr/src/lib.rs +++ b/hugr/src/lib.rs @@ -134,10 +134,13 @@ // These modules are re-exported as-is. If more control is needed, define a new module in this crate with the desired exports. // The doc inline directive is necessary for renamed modules to appear as if they were defined in this crate. -pub use hugr_core::{builder, core, extension, hugr, ops, std_extensions, types, utils}; +pub use hugr_core::{builder, core, extension, ops, std_extensions, types, utils}; #[doc(inline)] pub use hugr_passes as algorithms; +// Modules with hand-picked re-exports. +pub mod hugr; + // Top-level re-exports for convenience. pub use hugr_core::core::{ CircuitUnit, Direction, IncomingPort, Node, NodeIndex, OutgoingPort, Port, PortIndex, Wire,