5
5
use std:: { cmp:: Ordering , hash:: Hash , marker:: PhantomData , ops:: Deref } ;
6
6
7
7
use hugr:: {
8
+ hugr:: { views:: HierarchyView , HugrError } ,
8
9
ops:: { DataflowBlock , ExitBlock , Input , NamedOp , OpType , Output , CFG } ,
9
10
types:: Type ,
10
11
Hugr , HugrView , IncomingPort , Node , NodeIndex , OutgoingPort ,
@@ -23,7 +24,7 @@ use itertools::Itertools as _;
23
24
/// different base [Hugr]s. Note that [Node] has this same behaviour.
24
25
///
25
26
/// [FuncDefn]: [hugr::ops::FuncDefn]
26
- #[ derive( Debug , Copy ) ]
27
+ #[ derive( Debug ) ]
27
28
pub struct FatNode < ' c , OT = OpType , H = Hugr >
28
29
where
29
30
H : ?Sized ,
33
34
marker : PhantomData < OT > ,
34
35
}
35
36
36
- impl < ' c , OT : ' c , H : HugrView + ?Sized > FatNode < ' c , OT , H >
37
+ impl < ' c , OT , H : HugrView + ?Sized > FatNode < ' c , OT , H >
37
38
where
38
- & ' c OpType : TryInto < & ' c OT > ,
39
+ for < ' a > & ' a OpType : TryInto < & ' a OT > ,
39
40
{
40
41
/// Create a `FatNode` from a [HugrView] and a [Node].
41
42
///
46
47
/// do not verify that it is actually equal to `ot`.
47
48
pub fn new ( hugr : & ' c H , node : Node , #[ allow( unused) ] ot : & OT ) -> Self {
48
49
assert ! ( hugr. valid_node( node) ) ;
49
- assert ! ( TryInto :: <& ' c OT >:: try_into( hugr. get_optype( node) ) . is_ok( ) ) ;
50
+ assert ! ( TryInto :: <& OT >:: try_into( hugr. get_optype( node) ) . is_ok( ) ) ;
50
51
// We don't actually check `ot == hugr.get_optype(node)` so as to not require OT: PartialEq`
51
52
Self {
52
53
hugr,
68
69
) )
69
70
}
70
71
71
- /// Gets the [OpType] of the `FatNode`.
72
- pub fn get ( & self ) -> & ' c OT {
73
- self . hugr . get_optype ( self . node ) . try_into ( ) . ok ( ) . unwrap ( )
72
+ /// Create a general `FatNode` from a specific one.
73
+ pub fn generalise ( self ) -> FatNode < ' c , OpType , H > {
74
+ // guaranteed to be valid becasue self is valid
75
+ FatNode {
76
+ hugr : self . hugr ,
77
+ node : self . node ,
78
+ marker : PhantomData ,
79
+ }
74
80
}
75
81
}
76
82
@@ -96,9 +102,9 @@ impl<'c, H: HugrView + ?Sized> FatNode<'c, OpType, H> {
96
102
}
97
103
98
104
/// Tries to downcast a general `FatNode` into a specific `OT`.
99
- pub fn try_into_ot < OT : ' c > ( & self ) -> Option < FatNode < ' c , OT , H > >
105
+ pub fn try_into_ot < OT > ( & self ) -> Option < FatNode < ' c , OT , H > >
100
106
where
101
- & ' c OpType : TryInto < & ' c OT > ,
107
+ for < ' a > & ' a OpType : TryInto < & ' a OT > ,
102
108
{
103
109
FatNode :: try_new ( self . hugr , self . node )
104
110
}
@@ -110,7 +116,7 @@ impl<'c, H: HugrView + ?Sized> FatNode<'c, OpType, H> {
110
116
///
111
117
/// Note that while we do check the type of the node's `get_optype`, we
112
118
/// do not verify that it is actually equal to `ot`.
113
- pub fn into_ot < OT : PartialEq + ' c > ( self , ot : & OT ) -> FatNode < ' c , OT , H >
119
+ pub fn into_ot < OT > ( self , ot : & OT ) -> FatNode < ' c , OT , H >
114
120
where
115
121
for < ' a > & ' a OpType : TryInto < & ' a OT > ,
116
122
{
@@ -159,28 +165,24 @@ impl<'c, OT, H: HugrView + ?Sized> FatNode<'c, OT, H> {
159
165
) )
160
166
}
161
167
162
- pub fn node_outputs ( & self ) -> impl Iterator < Item = OutgoingPort > + ' _ {
168
+ /// Iterator over output ports of node.
169
+ pub fn node_outputs ( & self ) -> impl Iterator < Item = OutgoingPort > + ' c {
163
170
self . hugr . node_outputs ( self . node )
164
171
}
165
172
166
- pub fn output_neighbours ( & self ) -> impl Iterator < Item = FatNode < ' c , OpType , H > > + ' _ {
173
+ /// Iterates over the output neighbours of the `node`.
174
+ pub fn output_neighbours ( & self ) -> impl Iterator < Item = FatNode < ' c , OpType , H > > + ' c {
167
175
self . hugr
168
176
. output_neighbours ( self . node )
169
177
. map ( |n| FatNode :: new_optype ( self . hugr , n) )
170
178
}
171
179
172
- /// Create a general `FatNode` from a specific one .
173
- pub fn generalise ( self ) -> FatNode < ' c , OpType , H >
180
+ /// Delegates to `HV::try_new` with the internal [HugrView] and [Node] .
181
+ pub fn try_new_hierarchy_view < HV : HierarchyView < ' c > > ( & self ) -> Result < HV , HugrError >
174
182
where
175
- for < ' a > & ' a OpType : TryInto < & ' a OT > ,
176
- OT : ' c ,
183
+ H : Sized ,
177
184
{
178
- // guaranteed to be valid becasue self is valid
179
- FatNode {
180
- hugr : self . hugr ,
181
- node : self . node ,
182
- marker : PhantomData ,
183
- }
185
+ HV :: try_new ( self . hugr , self . node )
184
186
}
185
187
}
186
188
@@ -204,79 +206,79 @@ impl<'c, H: HugrView> FatNode<'c, CFG, H> {
204
206
}
205
207
}
206
208
207
- impl < ' c , OT , H > PartialEq < Node > for FatNode < ' c , OT , H > {
209
+ impl < OT , H > PartialEq < Node > for FatNode < ' _ , OT , H > {
208
210
fn eq ( & self , other : & Node ) -> bool {
209
211
& self . node == other
210
212
}
211
213
}
212
214
213
- impl < ' c , OT , H > PartialEq < FatNode < ' c , OT , H > > for Node {
214
- fn eq ( & self , other : & FatNode < ' c , OT , H > ) -> bool {
215
+ impl < OT , H > PartialEq < FatNode < ' _ , OT , H > > for Node {
216
+ fn eq ( & self , other : & FatNode < ' _ , OT , H > ) -> bool {
215
217
self == & other. node
216
218
}
217
219
}
218
220
219
- impl < ' c , ' d , OT1 , OT2 , H1 , H2 > PartialEq < FatNode < ' d , OT1 , H1 > > for FatNode < ' c , OT2 , H2 > {
220
- fn eq ( & self , other : & FatNode < ' d , OT1 , H1 > ) -> bool {
221
+ impl < OT1 , OT2 , H1 , H2 > PartialEq < FatNode < ' _ , OT1 , H1 > > for FatNode < ' _ , OT2 , H2 > {
222
+ fn eq ( & self , other : & FatNode < ' _ , OT1 , H1 > ) -> bool {
221
223
self . node == other. node
222
224
}
223
225
}
224
226
225
- impl < ' c , OT , H > Eq for FatNode < ' c , OT , H > { }
227
+ impl < OT , H > Eq for FatNode < ' _ , OT , H > { }
226
228
227
- impl < ' c , OT , H > PartialOrd < Node > for FatNode < ' c , OT , H > {
229
+ impl < OT , H > PartialOrd < Node > for FatNode < ' _ , OT , H > {
228
230
fn partial_cmp ( & self , other : & Node ) -> Option < Ordering > {
229
231
self . node . partial_cmp ( other)
230
232
}
231
233
}
232
234
233
- impl < ' c , OT , H > PartialOrd < FatNode < ' c , OT , H > > for Node {
234
- fn partial_cmp ( & self , other : & FatNode < ' c , OT , H > ) -> Option < Ordering > {
235
+ impl < OT , H > PartialOrd < FatNode < ' _ , OT , H > > for Node {
236
+ fn partial_cmp ( & self , other : & FatNode < ' _ , OT , H > ) -> Option < Ordering > {
235
237
self . partial_cmp ( & other. node )
236
238
}
237
239
}
238
240
239
- impl < ' c , ' d , OT1 , OT2 , H1 , H2 > PartialOrd < FatNode < ' d , OT1 , H1 > > for FatNode < ' c , OT2 , H2 > {
240
- fn partial_cmp ( & self , other : & FatNode < ' d , OT1 , H1 > ) -> Option < Ordering > {
241
+ impl < OT1 , OT2 , H1 , H2 > PartialOrd < FatNode < ' _ , OT1 , H1 > > for FatNode < ' _ , OT2 , H2 > {
242
+ fn partial_cmp ( & self , other : & FatNode < ' _ , OT1 , H1 > ) -> Option < Ordering > {
241
243
self . partial_cmp ( & other. node )
242
244
}
243
245
}
244
246
245
- impl < ' c , OT , H > Ord for FatNode < ' c , OT , H > {
247
+ impl < OT , H > Ord for FatNode < ' _ , OT , H > {
246
248
fn cmp ( & self , other : & Self ) -> Ordering {
247
249
self . node . cmp ( & other. node )
248
250
}
249
251
}
250
252
251
- impl < ' c , OT , H > Hash for FatNode < ' c , OT , H > {
253
+ impl < OT , H > Hash for FatNode < ' _ , OT , H > {
252
254
fn hash < HA : std:: hash:: Hasher > ( & self , state : & mut HA ) {
253
255
self . node . hash ( state) ;
254
256
}
255
257
}
256
258
257
- impl < ' c , OT : ' c , H : HugrView + ?Sized > AsRef < OT > for FatNode < ' c , OT , H >
259
+ impl < OT , H : HugrView + ?Sized > AsRef < OT > for FatNode < ' _ , OT , H >
258
260
where
259
261
for < ' a > & ' a OpType : TryInto < & ' a OT > ,
260
262
{
261
263
fn as_ref ( & self ) -> & OT {
262
- self . get ( )
264
+ self . hugr . get_optype ( self . node ) . try_into ( ) . ok ( ) . unwrap ( )
263
265
}
264
266
}
265
267
266
- impl < ' c , OT , H : HugrView + ?Sized > Deref for FatNode < ' c , OT , H >
268
+ impl < OT , H : HugrView + ?Sized > Deref for FatNode < ' _ , OT , H >
267
269
where
268
- & ' c OpType : TryInto < & ' c OT > ,
269
- <& ' c OpType as TryInto < & ' c OT > >:: Error : std:: fmt:: Debug ,
270
- OT : ' c ,
270
+ for < ' a > & ' a OpType : TryInto < & ' a OT > ,
271
271
{
272
272
type Target = OT ;
273
273
274
274
fn deref ( & self ) -> & Self :: Target {
275
- self . get ( )
275
+ self . as_ref ( )
276
276
}
277
277
}
278
278
279
- impl < ' c , OT , H > Clone for FatNode < ' c , OT , H > {
279
+ impl < OT , H > Copy for FatNode < ' _ , OT , H > { }
280
+
281
+ impl < OT , H > Clone for FatNode < ' _ , OT , H > {
280
282
fn clone ( & self ) -> Self {
281
283
Self {
282
284
hugr : self . hugr ,
@@ -288,22 +290,20 @@ impl<'c, OT, H> Clone for FatNode<'c, OT, H> {
288
290
289
291
impl < ' c , OT : NamedOp , H : HugrView + ?Sized > std:: fmt:: Display for FatNode < ' c , OT , H >
290
292
where
291
- & ' c OpType : TryInto < & ' c OT > ,
292
- <& ' c OpType as TryInto < & ' c OT > >:: Error : std:: fmt:: Debug ,
293
- OT : ' c ,
293
+ for < ' a > & ' a OpType : TryInto < & ' a OT > ,
294
294
{
295
295
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
296
- f. write_fmt ( format_args ! ( "N<{}:{}>" , self . get ( ) . name( ) , self . node) )
296
+ f. write_fmt ( format_args ! ( "N<{}:{}>" , self . as_ref ( ) . name( ) , self . node) )
297
297
}
298
298
}
299
299
300
- impl < ' c , OT , H > NodeIndex for FatNode < ' c , OT , H > {
300
+ impl < OT , H > NodeIndex for FatNode < ' _ , OT , H > {
301
301
fn index ( self ) -> usize {
302
302
self . node . index ( )
303
303
}
304
304
}
305
305
306
- impl < ' c , OT , H > NodeIndex for & FatNode < ' c , OT , H > {
306
+ impl < OT , H > NodeIndex for & FatNode < ' _ , OT , H > {
307
307
fn index ( self ) -> usize {
308
308
self . node . index ( )
309
309
}
@@ -316,7 +316,7 @@ impl<'c, OT, H> NodeIndex for &FatNode<'c, OT, H> {
316
316
/// TODO: Add the remaining [HugrView] equivalents that make sense.
317
317
pub trait FatExt : HugrView {
318
318
/// Try to create a specific [FatNode] for a given [Node].
319
- fn try_fat < ' c , OT : ' c > ( & ' c self , node : Node ) -> Option < FatNode < ' c , OT , Self > >
319
+ fn try_fat < OT > ( & self , node : Node ) -> Option < FatNode < OT , Self > >
320
320
where
321
321
for < ' a > & ' a OpType : TryInto < & ' a OT > ,
322
322
{
0 commit comments