@@ -183,7 +183,23 @@ pub trait HugrMut: HugrMutInternals {
183
183
/// # Panics
184
184
///
185
185
/// If the root node is not in the graph.
186
- fn insert_hugr ( & mut self , root : Self :: Node , other : Hugr ) -> InsertionResult < Node , Self :: Node > ;
186
+ fn insert_hugr ( & mut self , root : Self :: Node , other : Hugr ) -> InsertionResult < Node , Self :: Node > {
187
+ let region = other. entrypoint ( ) ;
188
+ Self :: insert_region ( self , root, other, region)
189
+ }
190
+
191
+ /// Insert a sub-region of another hugr into this one, under a given parent node.
192
+ ///
193
+ /// # Panics
194
+ ///
195
+ /// - If the root node is not in the graph.
196
+ /// - If the `region` node is not in `other`.
197
+ fn insert_region (
198
+ & mut self ,
199
+ root : Self :: Node ,
200
+ other : Hugr ,
201
+ region : Node ,
202
+ ) -> InsertionResult < Node , Self :: Node > ;
187
203
188
204
/// Copy another hugr into this one, under a given parent node.
189
205
///
@@ -247,15 +263,17 @@ pub trait HugrMut: HugrMutInternals {
247
263
ExtensionRegistry : Extend < Reg > ;
248
264
}
249
265
250
- /// Records the result of inserting a Hugr or view
251
- /// via [`HugrMut::insert_hugr`] or [`HugrMut::insert_from_view `].
266
+ /// Records the result of inserting a Hugr or view via [`HugrMut::insert_hugr`],
267
+ /// [`HugrMut::insert_from_view`], or [`HugrMut::insert_region `].
252
268
///
253
- /// Contains a map from the nodes in the source HUGR to the nodes in the
254
- /// target HUGR, using their respective `Node` types.
269
+ /// Contains a map from the nodes in the source HUGR to the nodes in the target
270
+ /// HUGR, using their respective `Node` types.
255
271
pub struct InsertionResult < SourceN = Node , TargetN = Node > {
256
- /// The node, after insertion, that was the entrypoint of the inserted Hugr.
272
+ /// The node, after insertion, that was the root of the inserted Hugr.
257
273
///
258
- /// That is, the value in [`InsertionResult::node_map`] under the key that was the [`HugrView::entrypoint`].
274
+ /// That is, the value in [`InsertionResult::node_map`] under the key that
275
+ /// was the the `region` passed to [`HugrMut::insert_region`] or the
276
+ /// [`HugrView::entrypoint`] in the other cases.
259
277
pub inserted_entrypoint : TargetN ,
260
278
/// Map from nodes in the Hugr/view that was inserted, to their new
261
279
/// positions in the Hugr into which said was inserted.
@@ -394,17 +412,14 @@ impl HugrMut for Hugr {
394
412
( src_port, dst_port)
395
413
}
396
414
397
- fn insert_hugr (
415
+ fn insert_region (
398
416
& mut self ,
399
417
root : Self :: Node ,
400
418
mut other : Hugr ,
419
+ region : Node ,
401
420
) -> InsertionResult < Node , Self :: Node > {
402
- let node_map = insert_hugr_internal ( self , & other, other. entry_descendants ( ) , |& n| {
403
- if n == other. entrypoint ( ) {
404
- Some ( root)
405
- } else {
406
- None
407
- }
421
+ let node_map = insert_hugr_internal ( self , & other, other. descendants ( region) , |& n| {
422
+ if n == region { Some ( root) } else { None }
408
423
} ) ;
409
424
// Merge the extension sets.
410
425
self . extensions . extend ( other. extensions ( ) ) ;
@@ -420,7 +435,7 @@ impl HugrMut for Hugr {
420
435
self . metadata . set ( new_node_pg, meta) ;
421
436
}
422
437
InsertionResult {
423
- inserted_entrypoint : node_map[ & other . entrypoint ( ) ] ,
438
+ inserted_entrypoint : node_map[ & region ] ,
424
439
node_map,
425
440
}
426
441
}
0 commit comments