Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 68ce052

Browse files
committed
refactor: clean up fat.rs
1 parent b63ba63 commit 68ce052

File tree

4 files changed

+64
-70
lines changed

4 files changed

+64
-70
lines changed

src/emit.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,8 @@ impl<'c, H: HugrView> EmitHugr<'c, H> {
357357
/// are not emitted directly, but instead by [hugr::ops::LoadConstant] emission. So
358358
/// [FuncDefn] and [FuncDecl] are the only interesting children.
359359
pub fn emit_module(mut self, node: FatNode<'c, hugr::ops::Module, H>) -> Result<Self> {
360-
println!("emit module");
361360
for c in node.children() {
362-
println!("emit child: {}", &c);
363-
match c.get() {
361+
match c.as_ref() {
364362
OpType::FuncDefn(ref fd) => {
365363
self = self.emit_global(c.into_ot(fd))?;
366364
}

src/emit/args.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ impl<'c, OT, H> EmitOpArgs<'c, OT, H> {
2525

2626
impl<'c, H: HugrView> EmitOpArgs<'c, OpType, H> {
2727
/// Attempt to specialise the internal [FatNode].
28-
pub fn try_into_ot<OT: 'c>(self) -> Result<EmitOpArgs<'c, OT, H>, Self>
28+
pub fn try_into_ot<OT>(self) -> Result<EmitOpArgs<'c, OT, H>, Self>
2929
where
30-
&'c OpType: TryInto<&'c OT>,
30+
for<'a> &'a OpType: TryInto<&'a OT>,
3131
{
3232
let EmitOpArgs {
3333
node,

src/emit/ops.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,9 @@ struct DataflowParentEmitter<'c, 'd, OT, H: HugrView> {
7878
outputs: Option<RowPromise<'c>>,
7979
}
8080

81-
impl<'c, 'd, OT: OpTrait + 'c, H: HugrView> DataflowParentEmitter<'c, 'd, OT, H>
81+
impl<'c, 'd, OT: OpTrait, H: HugrView> DataflowParentEmitter<'c, 'd, OT, H>
8282
where
83-
&'c OpType: TryInto<&'c OT>,
84-
// &'c OpType: TryInto<&'c OT>,
85-
// <&'c OpType as TryInto<&'c OT>>::Error: std::fmt::Debug,
83+
for<'a> &'a OpType: TryInto<&'a OT>,
8684
{
8785
pub fn new(context: &'d mut EmitFuncContext<'c, H>, args: EmitOpArgs<'c, OT, H>) -> Self {
8886
Self {
@@ -111,10 +109,9 @@ where
111109
}
112110

113111
pub fn emit_children(mut self) -> Result<()> {
114-
use hugr::hugr::views::HierarchyView;
115112
use petgraph::visit::Topo;
116113
let node = self.node.clone();
117-
if !OpTag::DataflowParent.is_superset(OpTrait::tag(node.get())) {
114+
if !OpTag::DataflowParent.is_superset(node.tag()) {
118115
Err(anyhow!("Not a dataflow parent"))?
119116
};
120117

@@ -124,7 +121,7 @@ where
124121
debug_assert!(i.out_value_types().count() == self.inputs.as_ref().unwrap().len());
125122
debug_assert!(o.in_value_types().count() == self.outputs.as_ref().unwrap().len());
126123

127-
let region: SiblingGraph = SiblingGraph::try_new(node.hugr(), node.node()).unwrap();
124+
let region: SiblingGraph = node.try_new_hierarchy_view().unwrap();
128125
Topo::new(&region.as_petgraph())
129126
.iter(&region.as_petgraph())
130127
.filter(|x| (*x != node.node()))
@@ -142,17 +139,16 @@ where
142139
}
143140
}
144141

145-
impl<'c, OT: OpTrait + 'c, H: HugrView> EmitOp<'c, OpType, H>
146-
for DataflowParentEmitter<'c, '_, OT, H>
142+
impl<'c, OT: OpTrait, H: HugrView> EmitOp<'c, OpType, H> for DataflowParentEmitter<'c, '_, OT, H>
147143
where
148-
&'c OpType: TryInto<&'c OT>,
144+
for<'a> &'a OpType: TryInto<&'a OT>,
149145
{
150146
fn emit(&mut self, args: EmitOpArgs<'c, OpType, H>) -> Result<()> {
151147
if !OpTag::DataflowChild.is_superset(args.node().tag()) {
152148
Err(anyhow!("Not a dataflow child"))?
153149
};
154150

155-
match args.node().get() {
151+
match args.node().as_ref() {
156152
OpType::Input(_) => {
157153
let i = self.take_input()?;
158154
args.outputs.finish(self.builder(), i)
@@ -283,12 +279,12 @@ pub fn emit_value<'c, H: HugrView>(
283279
}
284280
}
285281

286-
pub(crate) fn emit_dataflow_parent<'c, OT: OpTrait + 'c, H: HugrView>(
282+
pub(crate) fn emit_dataflow_parent<'c, OT: OpTrait, H: HugrView>(
287283
context: &mut EmitFuncContext<'c, H>,
288284
args: EmitOpArgs<'c, OT, H>,
289285
) -> Result<()>
290286
where
291-
&'c OpType: TryInto<&'c OT>,
287+
for<'a> &'a OpType: TryInto<&'a OT>,
292288
{
293289
DataflowParentEmitter::new(context, args).emit_children()
294290
}
@@ -347,7 +343,7 @@ fn emit_call<'c, H: HugrView>(
347343
.node
348344
.single_linked_output(args.node.called_function_port())
349345
.unwrap();
350-
let func = match func_node.get() {
346+
let func = match func_node.as_ref() {
351347
OpType::FuncDecl(_) => context.get_func_decl(func_node.try_into_ot().unwrap()),
352348
OpType::FuncDefn(_) => context.get_func_defn(func_node.try_into_ot().unwrap()),
353349
_ => Err(anyhow!("emit_call: Not a Decl or Defn")),
@@ -371,7 +367,7 @@ fn emit_optype<'c, H: HugrView>(
371367
args: EmitOpArgs<'c, OpType, H>,
372368
) -> Result<()> {
373369
let node = args.node();
374-
match node.get() {
370+
match node.as_ref() {
375371
OpType::MakeTuple(ref mt) => emit_make_tuple(context, args.into_ot(mt)),
376372
OpType::UnpackTuple(ref ut) => emit_unpack_tuple(context, args.into_ot(ut)),
377373
OpType::Tag(ref tag) => emit_tag(context, args.into_ot(tag)),

src/fat.rs

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use std::{cmp::Ordering, hash::Hash, marker::PhantomData, ops::Deref};
66

77
use hugr::{
8+
hugr::{views::HierarchyView, HugrError},
89
ops::{DataflowBlock, ExitBlock, Input, NamedOp, OpType, Output, CFG},
910
types::Type,
1011
Hugr, HugrView, IncomingPort, Node, NodeIndex, OutgoingPort,
@@ -23,7 +24,7 @@ use itertools::Itertools as _;
2324
/// different base [Hugr]s. Note that [Node] has this same behaviour.
2425
///
2526
/// [FuncDefn]: [hugr::ops::FuncDefn]
26-
#[derive(Debug, Copy)]
27+
#[derive(Debug)]
2728
pub struct FatNode<'c, OT = OpType, H = Hugr>
2829
where
2930
H: ?Sized,
@@ -33,9 +34,9 @@ where
3334
marker: PhantomData<OT>,
3435
}
3536

36-
impl<'c, OT: 'c, H: HugrView + ?Sized> FatNode<'c, OT, H>
37+
impl<'c, OT, H: HugrView + ?Sized> FatNode<'c, OT, H>
3738
where
38-
&'c OpType: TryInto<&'c OT>,
39+
for<'a> &'a OpType: TryInto<&'a OT>,
3940
{
4041
/// Create a `FatNode` from a [HugrView] and a [Node].
4142
///
@@ -46,7 +47,7 @@ where
4647
/// do not verify that it is actually equal to `ot`.
4748
pub fn new(hugr: &'c H, node: Node, #[allow(unused)] ot: &OT) -> Self {
4849
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());
5051
// We don't actually check `ot == hugr.get_optype(node)` so as to not require OT: PartialEq`
5152
Self {
5253
hugr,
@@ -68,9 +69,14 @@ where
6869
))
6970
}
7071

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+
}
7480
}
7581
}
7682

@@ -96,9 +102,9 @@ impl<'c, H: HugrView + ?Sized> FatNode<'c, OpType, H> {
96102
}
97103

98104
/// 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>>
100106
where
101-
&'c OpType: TryInto<&'c OT>,
107+
for<'a> &'a OpType: TryInto<&'a OT>,
102108
{
103109
FatNode::try_new(self.hugr, self.node)
104110
}
@@ -110,7 +116,7 @@ impl<'c, H: HugrView + ?Sized> FatNode<'c, OpType, H> {
110116
///
111117
/// Note that while we do check the type of the node's `get_optype`, we
112118
/// 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>
114120
where
115121
for<'a> &'a OpType: TryInto<&'a OT>,
116122
{
@@ -159,28 +165,24 @@ impl<'c, OT, H: HugrView + ?Sized> FatNode<'c, OT, H> {
159165
))
160166
}
161167

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 {
163170
self.hugr.node_outputs(self.node)
164171
}
165172

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 {
167175
self.hugr
168176
.output_neighbours(self.node)
169177
.map(|n| FatNode::new_optype(self.hugr, n))
170178
}
171179

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>
174182
where
175-
for<'a> &'a OpType: TryInto<&'a OT>,
176-
OT: 'c,
183+
H: Sized,
177184
{
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)
184186
}
185187
}
186188

@@ -204,79 +206,79 @@ impl<'c, H: HugrView> FatNode<'c, CFG, H> {
204206
}
205207
}
206208

207-
impl<'c, OT, H> PartialEq<Node> for FatNode<'c, OT, H> {
209+
impl<OT, H> PartialEq<Node> for FatNode<'_, OT, H> {
208210
fn eq(&self, other: &Node) -> bool {
209211
&self.node == other
210212
}
211213
}
212214

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 {
215217
self == &other.node
216218
}
217219
}
218220

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 {
221223
self.node == other.node
222224
}
223225
}
224226

225-
impl<'c, OT, H> Eq for FatNode<'c, OT, H> {}
227+
impl<OT, H> Eq for FatNode<'_, OT, H> {}
226228

227-
impl<'c, OT, H> PartialOrd<Node> for FatNode<'c, OT, H> {
229+
impl<OT, H> PartialOrd<Node> for FatNode<'_, OT, H> {
228230
fn partial_cmp(&self, other: &Node) -> Option<Ordering> {
229231
self.node.partial_cmp(other)
230232
}
231233
}
232234

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> {
235237
self.partial_cmp(&other.node)
236238
}
237239
}
238240

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> {
241243
self.partial_cmp(&other.node)
242244
}
243245
}
244246

245-
impl<'c, OT, H> Ord for FatNode<'c, OT, H> {
247+
impl<OT, H> Ord for FatNode<'_, OT, H> {
246248
fn cmp(&self, other: &Self) -> Ordering {
247249
self.node.cmp(&other.node)
248250
}
249251
}
250252

251-
impl<'c, OT, H> Hash for FatNode<'c, OT, H> {
253+
impl<OT, H> Hash for FatNode<'_, OT, H> {
252254
fn hash<HA: std::hash::Hasher>(&self, state: &mut HA) {
253255
self.node.hash(state);
254256
}
255257
}
256258

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>
258260
where
259261
for<'a> &'a OpType: TryInto<&'a OT>,
260262
{
261263
fn as_ref(&self) -> &OT {
262-
self.get()
264+
self.hugr.get_optype(self.node).try_into().ok().unwrap()
263265
}
264266
}
265267

266-
impl<'c, OT, H: HugrView + ?Sized> Deref for FatNode<'c, OT, H>
268+
impl<OT, H: HugrView + ?Sized> Deref for FatNode<'_, OT, H>
267269
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>,
271271
{
272272
type Target = OT;
273273

274274
fn deref(&self) -> &Self::Target {
275-
self.get()
275+
self.as_ref()
276276
}
277277
}
278278

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> {
280282
fn clone(&self) -> Self {
281283
Self {
282284
hugr: self.hugr,
@@ -288,22 +290,20 @@ impl<'c, OT, H> Clone for FatNode<'c, OT, H> {
288290

289291
impl<'c, OT: NamedOp, H: HugrView + ?Sized> std::fmt::Display for FatNode<'c, OT, H>
290292
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>,
294294
{
295295
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))
297297
}
298298
}
299299

300-
impl<'c, OT, H> NodeIndex for FatNode<'c, OT, H> {
300+
impl<OT, H> NodeIndex for FatNode<'_, OT, H> {
301301
fn index(self) -> usize {
302302
self.node.index()
303303
}
304304
}
305305

306-
impl<'c, OT, H> NodeIndex for &FatNode<'c, OT, H> {
306+
impl<OT, H> NodeIndex for &FatNode<'_, OT, H> {
307307
fn index(self) -> usize {
308308
self.node.index()
309309
}
@@ -316,7 +316,7 @@ impl<'c, OT, H> NodeIndex for &FatNode<'c, OT, H> {
316316
/// TODO: Add the remaining [HugrView] equivalents that make sense.
317317
pub trait FatExt: HugrView {
318318
/// 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>>
320320
where
321321
for<'a> &'a OpType: TryInto<&'a OT>,
322322
{

0 commit comments

Comments
 (0)