diff --git a/quantinuum-hugr/src/hugr/hugrmut.rs b/quantinuum-hugr/src/hugr/hugrmut.rs index a0be5aade..b3fd97037 100644 --- a/quantinuum-hugr/src/hugr/hugrmut.rs +++ b/quantinuum-hugr/src/hugr/hugrmut.rs @@ -4,6 +4,7 @@ use core::panic; use std::collections::HashMap; use std::ops::Range; +use nonempty::NonEmpty; use portgraph::view::{NodeFilter, NodeFiltered}; use portgraph::{LinkMut, NodeIndex, PortMut, PortView, SecondaryMap}; @@ -365,7 +366,7 @@ impl + AsMut> HugrMut for T { subgraph: &SiblingSubgraph, ) -> HashMap { // Create a portgraph view with the explicit list of nodes defined by the subgraph. - let portgraph: NodeFiltered<_, NodeFilter<&[Node]>, &[Node]> = + let portgraph: NodeFiltered<_, NodeFilter<&NonEmpty>, &NonEmpty> = NodeFiltered::new_node_filtered( other.portgraph(), |node, ctx| ctx.contains(&node.into()), diff --git a/quantinuum-hugr/src/hugr/rewrite/simple_replace.rs b/quantinuum-hugr/src/hugr/rewrite/simple_replace.rs index 211aca649..d25c970af 100644 --- a/quantinuum-hugr/src/hugr/rewrite/simple_replace.rs +++ b/quantinuum-hugr/src/hugr/rewrite/simple_replace.rs @@ -196,7 +196,6 @@ pub enum SimpleReplacementError { #[cfg(test)] pub(in crate::hugr::rewrite) mod test { use itertools::Itertools; - use nonempty::NonEmpty; use rstest::{fixture, rstest}; use std::collections::{HashMap, HashSet}; @@ -622,7 +621,7 @@ pub(in crate::hugr::rewrite) mod test { replacement.remove_node(in_); replacement.remove_node(out); Replacement { - removal: NonEmpty::from_slice(s.subgraph.nodes()).unwrap(), + removal: s.subgraph.nodes().clone(), replacement, adoptions: HashMap::new(), mu_inp, diff --git a/quantinuum-hugr/src/hugr/views/sibling_subgraph.rs b/quantinuum-hugr/src/hugr/views/sibling_subgraph.rs index 9741b08c7..d14e3203d 100644 --- a/quantinuum-hugr/src/hugr/views/sibling_subgraph.rs +++ b/quantinuum-hugr/src/hugr/views/sibling_subgraph.rs @@ -13,6 +13,7 @@ use std::collections::HashSet; use std::mem; use itertools::Itertools; +use nonempty::NonEmpty; use portgraph::algorithms::ConvexChecker; use portgraph::{view::Subgraph, Direction, PortView}; use thiserror::Error; @@ -54,7 +55,7 @@ use crate::{Hugr, IncomingPort, Node, OutgoingPort, Port, SimpleReplacement}; #[derive(Clone, Debug)] pub struct SiblingSubgraph { /// The nodes of the induced subgraph. - nodes: Vec, + nodes: NonEmpty, /// The input ports of the subgraph. /// /// Grouped by input parameter. Each port must be unique and belong to a @@ -101,16 +102,12 @@ impl SiblingSubgraph { let (inputs, outputs) = get_input_output_ports(dfg_graph); validate_subgraph(dfg_graph, &nodes, &inputs, &outputs)?; - - if nodes.is_empty() { - Err(InvalidSubgraph::EmptySubgraph) - } else { - Ok(Self { - nodes, - inputs, - outputs, - }) - } + let nodes = NonEmpty::from_vec(nodes).ok_or(InvalidSubgraph::EmptySubgraph)?; + Ok(Self { + nodes, + inputs, + outputs, + }) } /// Create a new convex sibling subgraph from input and output boundaries. @@ -192,6 +189,8 @@ impl SiblingSubgraph { return Err(InvalidSubgraph::NotConvex); } + let nodes = NonEmpty::from_vec(nodes).ok_or(InvalidSubgraph::EmptySubgraph)?; + Ok(Self { nodes, inputs, @@ -269,7 +268,7 @@ impl SiblingSubgraph { } /// An iterator over the nodes in the subgraph. - pub fn nodes(&self) -> &[Node] { + pub fn nodes(&self) -> &NonEmpty { &self.nodes } @@ -715,15 +714,12 @@ mod tests { fn from_sibling_graph(sibling_graph: &impl HugrView) -> Result { let root = sibling_graph.root(); let nodes = sibling_graph.children(root).collect_vec(); - if nodes.is_empty() { - Err(InvalidSubgraph::EmptySubgraph) - } else { - Ok(Self { - nodes, - inputs: Vec::new(), - outputs: Vec::new(), - }) - } + let nodes = NonEmpty::from_vec(nodes).ok_or(InvalidSubgraph::EmptySubgraph)?; + Ok(Self { + nodes, + inputs: Vec::new(), + outputs: Vec::new(), + }) } }