@@ -127,15 +127,15 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
127
127
///
128
128
/// The returned port will be in `replacement`, unless the wire in the
129
129
/// replacement is empty and `return_invalid` is
130
- /// [`IncludeReplacementNodes::Valid `] (the default), in which case it
131
- /// will be another `host` port. If [`IncludeReplacementNodes::All `] is
130
+ /// [`BoundaryMode::SnapToHost `] (the default), in which case it
131
+ /// will be another `host` port. If [`BoundaryMode::IncludeIO `] is
132
132
/// passed, the returned port will always be in `replacement` even if it
133
133
/// is invalid (i.e. it is an IO node in the replacement).
134
134
pub fn linked_replacement_output (
135
135
& self ,
136
136
port : impl Into < HostPort < HostNode , IncomingPort > > ,
137
137
host : & impl HugrView < Node = HostNode > ,
138
- return_invalid : IncludeReplacementNodes ,
138
+ return_invalid : BoundaryMode ,
139
139
) -> Option < BoundaryPort < HostNode , OutgoingPort > > {
140
140
let HostPort ( node, port) = port. into ( ) ;
141
141
let pos = self
@@ -155,7 +155,7 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
155
155
& self ,
156
156
pos : usize ,
157
157
host : & impl HugrView < Node = HostNode > ,
158
- return_invalid : IncludeReplacementNodes ,
158
+ return_invalid : BoundaryMode ,
159
159
) -> BoundaryPort < HostNode , OutgoingPort > {
160
160
debug_assert ! ( pos < self . subgraph( ) . signature( host) . output_count( ) ) ;
161
161
@@ -166,7 +166,7 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
166
166
. single_linked_output ( repl_out, pos)
167
167
. expect ( "valid dfg wire" ) ;
168
168
169
- if out_node != repl_inp || return_invalid == IncludeReplacementNodes :: All {
169
+ if out_node != repl_inp || return_invalid == BoundaryMode :: IncludeIO {
170
170
BoundaryPort :: Replacement ( out_node, out_port)
171
171
} else {
172
172
let ( in_node, in_port) = * self . subgraph . incoming_ports ( ) [ out_port. index ( ) ]
@@ -214,16 +214,16 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
214
214
///
215
215
/// The returned ports will be in `replacement`, unless the wires in the
216
216
/// replacement are empty and `return_invalid` is
217
- /// [`IncludeReplacementNodes::Valid `] (the default), in which case they
218
- /// will be other `host` ports. If [`IncludeReplacementNodes::All `] is
217
+ /// [`BoundaryMode::SnapToHost `] (the default), in which case they
218
+ /// will be other `host` ports. If [`BoundaryMode::IncludeIO `] is
219
219
/// passed, the returned ports will always be in
220
220
/// `replacement` even if they are invalid (i.e. they are an IO node in
221
221
/// the replacement).
222
222
pub fn linked_replacement_inputs < ' a > (
223
223
& ' a self ,
224
224
port : impl Into < HostPort < HostNode , OutgoingPort > > ,
225
225
host : & ' a impl HugrView < Node = HostNode > ,
226
- return_invalid : IncludeReplacementNodes ,
226
+ return_invalid : BoundaryMode ,
227
227
) -> impl Iterator < Item = BoundaryPort < HostNode , IncomingPort > > + ' a {
228
228
let HostPort ( node, port) = port. into ( ) ;
229
229
let positions = self
@@ -245,15 +245,15 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
245
245
& self ,
246
246
pos : usize ,
247
247
host : & impl HugrView < Node = HostNode > ,
248
- return_invalid : IncludeReplacementNodes ,
248
+ return_invalid : BoundaryMode ,
249
249
) -> impl Iterator < Item = BoundaryPort < HostNode , IncomingPort > > {
250
250
debug_assert ! ( pos < self . subgraph( ) . signature( host) . input_count( ) ) ;
251
251
252
252
let [ repl_inp, repl_out] = self . get_replacement_io ( ) ;
253
253
self . replacement
254
254
. linked_inputs ( repl_inp, pos)
255
255
. flat_map ( move |( in_node, in_port) | {
256
- if in_node != repl_out || return_invalid == IncludeReplacementNodes :: All {
256
+ if in_node != repl_out || return_invalid == BoundaryMode :: IncludeIO {
257
257
Either :: Left ( std:: iter:: once ( BoundaryPort :: Replacement ( in_node, in_port) ) )
258
258
} else {
259
259
let ( out_node, out_port) = self . subgraph . outgoing_ports ( ) [ in_port. index ( ) ] ;
@@ -327,12 +327,8 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
327
327
subgraph_outgoing_ports
328
328
. enumerate ( )
329
329
. flat_map ( |( pos, subg_np) | {
330
- self . linked_replacement_inputs_by_position (
331
- pos,
332
- host,
333
- IncludeReplacementNodes :: Valid ,
334
- )
335
- . filter_map ( move |np| Some ( ( np. as_replacement ( ) ?, subg_np) ) )
330
+ self . linked_replacement_inputs_by_position ( pos, host, BoundaryMode :: SnapToHost )
331
+ . filter_map ( move |np| Some ( ( np. as_replacement ( ) ?, subg_np) ) )
336
332
} )
337
333
. map ( |( ( repl_node, repl_port) , ( subgraph_node, subgraph_port) ) | {
338
334
(
@@ -374,11 +370,7 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
374
370
. enumerate ( )
375
371
. filter_map ( |( pos, subg_all) | {
376
372
let np = self
377
- . linked_replacement_output_by_position (
378
- pos,
379
- host,
380
- IncludeReplacementNodes :: Valid ,
381
- )
373
+ . linked_replacement_output_by_position ( pos, host, BoundaryMode :: SnapToHost )
382
374
. as_replacement ( ) ?;
383
375
Some ( ( np, subg_all) )
384
376
} )
@@ -425,12 +417,8 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
425
417
. enumerate ( )
426
418
. filter_map ( |( pos, subg_all) | {
427
419
Some ( (
428
- self . linked_replacement_output_by_position (
429
- pos,
430
- host,
431
- IncludeReplacementNodes :: Valid ,
432
- )
433
- . as_host ( ) ?,
420
+ self . linked_replacement_output_by_position ( pos, host, BoundaryMode :: SnapToHost )
421
+ . as_host ( ) ?,
434
422
subg_all,
435
423
) )
436
424
} )
@@ -567,15 +555,19 @@ impl<HostNode: HugrNode> PatchVerification for SimpleReplacement<HostNode> {
567
555
/// In [`SimpleReplacement`], some nodes in the replacement may not be valid
568
556
/// after the replacement is applied.
569
557
///
570
- /// This enum allows to filter out such nodes.
558
+ /// This enum allows specifying whether these invalid nodes on the boundary
559
+ /// should be returned or should be resolved to valid nodes in the host.
571
560
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Default ) ]
572
- #[ non_exhaustive]
573
- pub enum IncludeReplacementNodes {
561
+ pub enum BoundaryMode {
574
562
/// Only consider nodes that are valid after the replacement is applied.
563
+ ///
564
+ /// This means that nodes in hosts may be returned in places where nodes in
565
+ /// the replacement would be typically expected.
575
566
#[ default]
576
- Valid ,
577
- /// Include all nodes, including potentially invalid ones.
578
- All ,
567
+ SnapToHost ,
568
+ /// Include all nodes, including potentially invalid ones (inputs and
569
+ /// outputs of replacements).
570
+ IncludeIO ,
579
571
}
580
572
581
573
/// Result of applying a [`SimpleReplacement`].
@@ -690,7 +682,7 @@ pub(in crate::hugr::patch) mod test {
690
682
ModuleBuilder , endo_sig, inout_sig,
691
683
} ;
692
684
use crate :: extension:: prelude:: { bool_t, qb_t} ;
693
- use crate :: hugr:: patch:: simple_replace:: { IncludeReplacementNodes , Outcome } ;
685
+ use crate :: hugr:: patch:: simple_replace:: { BoundaryMode , Outcome } ;
694
686
use crate :: hugr:: patch:: { BoundaryPort , HostPort , PatchVerification , ReplacementPort } ;
695
687
use crate :: hugr:: views:: { HugrView , SiblingSubgraph } ;
696
688
use crate :: hugr:: { Hugr , HugrMut , Patch } ;
@@ -1186,7 +1178,7 @@ pub(in crate::hugr::patch) mod test {
1186
1178
. linked_replacement_inputs (
1187
1179
( inp, OutgoingPort :: from ( 0 ) ) ,
1188
1180
& hugr,
1189
- IncludeReplacementNodes :: Valid ,
1181
+ BoundaryMode :: SnapToHost ,
1190
1182
)
1191
1183
. collect ( ) ;
1192
1184
@@ -1203,7 +1195,7 @@ pub(in crate::hugr::patch) mod test {
1203
1195
repl. linked_replacement_output (
1204
1196
( out, IncomingPort :: from ( i) ) ,
1205
1197
& hugr,
1206
- IncludeReplacementNodes :: Valid ,
1198
+ BoundaryMode :: SnapToHost ,
1207
1199
)
1208
1200
. unwrap ( )
1209
1201
} )
@@ -1240,7 +1232,7 @@ pub(in crate::hugr::patch) mod test {
1240
1232
. linked_replacement_inputs (
1241
1233
( inp, OutgoingPort :: from ( 0 ) ) ,
1242
1234
& hugr,
1243
- IncludeReplacementNodes :: Valid ,
1235
+ BoundaryMode :: SnapToHost ,
1244
1236
)
1245
1237
. collect ( ) ;
1246
1238
@@ -1256,7 +1248,7 @@ pub(in crate::hugr::patch) mod test {
1256
1248
repl. linked_replacement_output (
1257
1249
( out, IncomingPort :: from ( i) ) ,
1258
1250
& hugr,
1259
- IncludeReplacementNodes :: Valid ,
1251
+ BoundaryMode :: SnapToHost ,
1260
1252
)
1261
1253
. unwrap ( )
1262
1254
} )
@@ -1298,7 +1290,7 @@ pub(in crate::hugr::patch) mod test {
1298
1290
. linked_replacement_inputs (
1299
1291
( inp, OutgoingPort :: from ( 0 ) ) ,
1300
1292
& hugr,
1301
- IncludeReplacementNodes :: Valid ,
1293
+ BoundaryMode :: SnapToHost ,
1302
1294
)
1303
1295
. collect ( ) ;
1304
1296
@@ -1318,7 +1310,7 @@ pub(in crate::hugr::patch) mod test {
1318
1310
repl. linked_replacement_output (
1319
1311
( out, IncomingPort :: from ( i) ) ,
1320
1312
& hugr,
1321
- IncludeReplacementNodes :: Valid ,
1313
+ BoundaryMode :: SnapToHost ,
1322
1314
)
1323
1315
. unwrap ( )
1324
1316
} )
0 commit comments