Skip to content

Commit 3e1dcec

Browse files
committed
rename Node::new to Node::from_list
1 parent aaa3993 commit 3e1dcec

File tree

8 files changed

+165
-137
lines changed

8 files changed

+165
-137
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ fn main() {
99
let style: NodeStyle = Default::default();
1010

1111
let nodes = vec![
12-
Node::new(
12+
Node::from_list(
1313
vec!["_1 = const 1_i32".into(), "_2 = const 2_i32".into()],
1414
label1.clone(),
1515
"0".into(),
1616
style.clone(),
1717
),
18-
Node::new(
18+
Node::from_list(
1919
vec!["return".into()],
2020
label2.clone(),
2121
"1".into(),
@@ -48,13 +48,13 @@ fn test_diff_readme() {
4848
let g1 = Graph::new(
4949
"small".into(),
5050
vec![
51-
Node::new(
51+
Node::from_list(
5252
vec!["_1 = const 1_i32".into(), "_2 = const 2_i32".into()],
5353
"bb0".into(),
5454
"bb0".into(),
5555
style.clone(),
5656
),
57-
Node::new(
57+
Node::from_list(
5858
vec!["return".into()],
5959
"bb1".into(),
6060
"bb1".into(),

src/graph.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ impl Graph {
120120
}
121121
if let Some(label) = &settings.graph_label {
122122
writeln!(w, r#" label=<{}>;"#, label)?;
123+
} else {
124+
if as_subgraph {
125+
writeln!(w, " label=\"{}\";", self.name)?;
126+
}
123127
}
124128

125129
for node in self.nodes.iter() {
@@ -143,11 +147,11 @@ mod tests {
143147
let stmts: Vec<String> = vec!["hi".into(), "hell".into()];
144148
let label1: String = "bb0__0_3".into();
145149
let style: NodeStyle = Default::default();
146-
let node1 = Node::new(stmts, label1.clone(), "0".into(), style.clone());
150+
let node1 = Node::from_list(stmts, label1.clone(), "0".into(), style.clone());
147151

148152
let stmts: Vec<String> = vec!["_1 = const 1_i32".into(), "_2 = const 2_i32".into()];
149153
let label2: String = "bb0__1_3".into();
150-
let node2 = Node::new(stmts, label2.clone(), "1".into(), style.clone());
154+
let node2 = Node::from_list(stmts, label2.clone(), "1".into(), style.clone());
151155

152156
Graph::new(
153157
"Mir_0_3".into(),

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
//! let style: NodeStyle = Default::default();
1717
//!
1818
//! let nodes = vec![
19-
//! Node::new(
19+
//! Node::from_list(
2020
//! vec!["_1 = const 1_i32".into(), "_2 = const 2_i32".into()],
2121
//! label1.clone(),
2222
//! "0".into(),
2323
//! style.clone(),
2424
//! ),
25-
//! Node::new(
25+
//! Node::from_list(
2626
//! vec!["return".into()],
2727
//! label2.clone(),
2828
//! "1".into(),

src/node.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct Node {
4242

4343
impl Node {
4444
// TODO: rename to from_list
45-
pub fn new(stmts: Vec<String>, label: String, title: String, style: NodeStyle) -> Node {
45+
pub fn from_list(stmts: Vec<String>, label: String, title: String, style: NodeStyle) -> Node {
4646
let stmts_len = stmts.len();
4747
let mut transformed_stmts = Vec::with_capacity(stmts_len);
4848
if !stmts.is_empty() {
@@ -107,24 +107,6 @@ impl Node {
107107
)?;
108108

109109
write!(w, "{}", self.content)?;
110-
// if !self.stmts.is_empty() {
111-
// if self.stmts.len() > 1 {
112-
// write!(w, r#"<tr><td align="left" balign="left">"#)?;
113-
// for statement in &self.stmts[..stmts_len - 1] {
114-
// write!(w, "{}<br/>", escape_html(statement))?;
115-
// }
116-
// write!(w, "</td></tr>")?;
117-
// }
118-
//
119-
// if !self.style.last_stmt_sep {
120-
// write!(w, r#"<tr><td align="left">"#)?;
121-
// write!(w, "{}", escape_html(&self.stmts[stmts_len - 1]))?;
122-
// } else {
123-
// write!(w, r#"<tr><td align="left" balign="left">"#)?;
124-
// write!(w, "{}", escape_html(&self.stmts[stmts_len - 1]))?;
125-
// }
126-
// write!(w, "</td></tr>")?;
127-
// }
128110

129111
write!(w, "</table>")
130112
}

tests/helpers.rs

Lines changed: 75 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,116 +13,148 @@ pub fn read_graph_from_file(file: &str) -> Graph {
1313
serde_json::from_str(&contents).unwrap()
1414
}
1515

16+
pub fn get_small_graph() -> Graph {
17+
let style: NodeStyle = Default::default();
18+
19+
Graph::new(
20+
"small".into(),
21+
vec![
22+
Node::from_list(
23+
vec!["_1 = const 1_i32".into(), "_2 = const 2_i32".into()],
24+
"bb0".into(),
25+
"bb0".into(),
26+
style.clone(),
27+
),
28+
Node::from_list(
29+
vec!["_2 = const 2_i32".into(), "_3 = const 3_i32".into()],
30+
"bb1".into(),
31+
"bb1".into(),
32+
style.clone(),
33+
),
34+
Node::from_list(
35+
vec!["return".into()],
36+
"bb2".into(),
37+
"bb2".into(),
38+
style.clone(),
39+
),
40+
],
41+
vec![
42+
Edge::new("bb0".into(), "bb1".into(), "return".into()),
43+
Edge::new("bb1".into(), "bb2".into(), "return".into()),
44+
],
45+
)
46+
}
47+
1648
pub fn get_graph_1() -> Graph {
1749
let style: NodeStyle = Default::default();
1850

1951
Graph::new(
2052
"Mir_0_3".into(),
21-
vec![Node::new(
53+
vec![Node::from_list(
2254
vec!["StorageLive(_1)".into(), "_1 = Vec::<i32>::new()".into()],
2355
"bb0".into(), "bb0".into(), style.clone()
2456
),
25-
Node::new(
57+
Node::from_list(
2658
vec!["resume".into()],
2759
"bb1".into(), "bb1".into(), style.clone()
2860
),
29-
Node::new(
61+
Node::from_list(
3062
vec!["StorageLive(_2)".into(), "StorageLive(_3)".into(), "(_3.0: i32) = const 1_i32".into(), "(_3.1: i32) = const 10_i32".into(), "_2 = <std::ops::Range<i32> as IntoIterator>::into_iter(move _3)".into()],
3163
"bb2".into(), "bb2".into(), style.clone()
3264
),
33-
Node::new(
65+
Node::from_list(
3466
vec!["StorageDead(_3)".into(), "StorageLive(_4)".into(), "_4 = move _2".into(), "goto".into()],
3567
"bb3".into(), "bb3".into(), style.clone()
3668
),
37-
Node::new(
69+
Node::from_list(
3870
vec!["drop(_1)".into()],
3971
"bb4".into(), "bb4".into(), style.clone()
4072
),
41-
Node::new(
73+
Node::from_list(
4274
vec!["StorageLive(_5)".into(), "StorageLive(_6)".into(), "StorageLive(_7)".into(), "StorageLive(_8)".into(), "_8 = &mut _4 _7 = &mut (*_8)".into(), "_6 = <std::ops::Range<i32> as Iterator>::next(move _7)".into()],
4375
"bb5".into(), "bb5".into(), style.clone()
4476
),
45-
Node::new(
77+
Node::from_list(
4678
vec!["StorageDead(_7)".into(), "_9 = discriminant(_6)".into(), "switchInt(move _9)".into()],
4779
"bb6".into(), "bb6".into(), style.clone()
4880
),
49-
Node::new(
81+
Node::from_list(
5082
vec!["StorageDead(_8)".into(), "StorageDead(_6)".into(), "StorageDead(_5)".into(), "StorageDead(_4)".into(), "StorageDead(_2)".into(), "StorageLive(_21)".into(), "StorageLive(_22)".into(), "(_22.0: i32) = const 1_i32".into(), "(_22.1: i32) = const 10_i32".into(), "_21 = <std::ops::Range<i32> as IntoIterator>::into_iter(move _22)".into()],
5183
"bb7".into(), "bb7".into(), style.clone()
5284
),
53-
Node::new(
85+
Node::from_list(
5486
vec!["unreachable".into()],
5587
"bb8".into(), "bb8".into(), style.clone()
5688
),
57-
Node::new(
89+
Node::from_list(
5890
vec!["StorageLive(_10)".into(), "_10 = ((_6 as Some).0: i32) StorageLive(_11)".into(), "_11 = _10 _5 = move _11 StorageDead(_11)".into(), "StorageDead(_10)".into(), "StorageDead(_8)".into(), "StorageDead(_6)".into(), "StorageLive(_12)".into(), "_12 = _5 StorageLive(_13)".into(), "StorageLive(_14)".into(), "_14 = _12 _15 = const false".into(), "_16 = Eq(_14, const i32::MIN) _17 = BitAnd(move _15, move _16)".into(), "assert(!move _17, attempt to compute the remainder of `{} % {}` which would overflow, _14, const 2_i32)".into()],
5991
"bb9".into(), "bb9".into(), style.clone()
6092
),
61-
Node::new(
93+
Node::from_list(
6294
vec!["_13 = Rem(move _14, const 2_i32)".into(),"StorageDead(_14)".into(), "switchInt(move _13)".into()],
6395
"bb10".into(), "bb10".into(), style.clone()
6496
),
65-
Node::new(
97+
Node::from_list(
6698
vec!["StorageDead(_13)".into(), "goto".into()],
6799
"bb11".into(), "bb11".into(), style.clone()
68100
),
69-
Node::new(
101+
Node::from_list(
70102
vec!["StorageDead(_13)".into(), "StorageLive(_18)".into(), "StorageLive(_19)".into(), "_19 = &mut _1 StorageLive(_20)".into(), "_20 = _12".into(), "_18 = Vec::<i32>::push(move _19, move _20)".into()],
71103
"bb12".into(), "bb12".into(), style.clone()
72104
),
73-
Node::new(
105+
Node::from_list(
74106
vec!["StorageDead(_20)".into(), "StorageDead(_19)".into(), "StorageDead(_18)".into(), "goto".into()],
75107
"bb13".into(), "bb13".into(), style.clone()
76108
),
77-
Node::new(
109+
Node::from_list(
78110
vec!["StorageDead(_12)".into(), "StorageDead(_5)".into(), "goto".into()],
79111
"bb14".into(), "bb14".into(), style.clone()
80112
),
81-
Node::new(
113+
Node::from_list(
82114
vec!["StorageDead(_22)".into(), "StorageLive(_23)".into(), "_23 = move _21".into(), "goto".into()],
83115
"bb15".into(), "bb15".into(), style.clone()
84116
),
85-
Node::new(
117+
Node::from_list(
86118
vec!["StorageLive(_24)".into(), "StorageLive(_25)".into(), "StorageLive(_26)".into(), "StorageLive(_27)".into(), "_27 = &mut _23 _26 = &mut (*_27)".into(), "_25 = <std::ops::Range<i32> as Iterator>::next(move _26)".into()],
87119
"bb16".into(), "bb16".into(), style.clone()
88120
),
89-
Node::new(
121+
Node::from_list(
90122
vec!["StorageDead(_26)".into(), "_28 = discriminant(_25)".into(), "switchInt(move _28)".into()],
91123
"bb17".into(), "bb17".into(), style.clone()
92124
),
93-
Node::new(
125+
Node::from_list(
94126
vec!["_0 = const () StorageDead(_27)".into(), "StorageDead(_25)".into(), "StorageDead(_24)".into(), "StorageDead(_23)".into(), "StorageDead(_21)".into(), "drop(_1)".into()],
95127
"bb18".into(), "bb18".into(), style.clone()
96128
),
97-
Node::new(
129+
Node::from_list(
98130
vec!["unreachable".into()],
99131
"bb19".into(), "bb19".into(), style.clone()
100132
),
101-
Node::new(
133+
Node::from_list(
102134
vec!["StorageLive(_29)".into(), "_29 = ((_25 as Some).0: i32)".into(), "StorageLive(_30)".into(), "_30 = _29 _24 = move _30 StorageDead(_30)".into(), "StorageDead(_29)".into(), "StorageDead(_27)".into(), "StorageDead(_25)".into(), "StorageLive(_31)".into(), "_31 = _24 StorageLive(_32)".into(), "StorageLive(_33)".into(), "_33 = _31 _34 = const false".into(), "_35 = Eq(_33, const i32::MIN) _36 = BitAnd(move _34, move _35)".into(), "assert(!move _36, attempt to compute the remainder of `{} % {}` which would overflow, _33, const 3_i32)".into()],
103135
"bb20".into(), "bb20".into(), style.clone()
104136
),
105-
Node::new(
137+
Node::from_list(
106138
vec!["_32 = Rem(move _33, const 3_i32)".into(), "StorageDead(_33)".into(), "switchInt(move _32)".into()],
107139
"bb21".into(), "bb21".into(), style.clone()
108140
),
109-
Node::new(
141+
Node::from_list(
110142
vec!["StorageDead(_32)".into(), "goto".into()],
111143
"bb22".into(), "bb22".into(), style.clone()
112144
),
113-
Node::new(
145+
Node::from_list(
114146
vec!["StorageDead(_32)".into(), "StorageLive(_37)".into(), "StorageLive(_38)".into(), "_38 = &mut _1 StorageLive(_39)".into(), "_39 = _31".into(), "_37 = Vec::<i32>::push(move _38, move _39)".into()],
115147
"bb23".into(), "bb23".into(), style.clone()
116148
),
117-
Node::new(
149+
Node::from_list(
118150
vec!["StorageDead(_39)".into(), "StorageDead(_38)".into(), "StorageDead(_37)".into(), "goto".into()],
119151
"bb24".into(), "bb24".into(), style.clone()
120152
),
121-
Node::new(
153+
Node::from_list(
122154
vec!["StorageDead(_31)".into(), "StorageDead(_24)".into(), "goto".into()],
123155
"bb25".into(), "bb25".into(), style.clone()
124156
),
125-
Node::new(
157+
Node::from_list(
126158
vec!["StorageDead(_1)".into(), "return".into()],
127159
"bb26".into(), "bb26".into(), style.clone()
128160
)],
@@ -172,67 +204,67 @@ pub fn get_graph_2() -> Graph {
172204

173205
Graph::new(
174206
"Mir_0_3".into(),
175-
vec![Node::new(
207+
vec![Node::from_list(
176208
vec!["StorageLive(_1)".into(), "_1 = Vec::<i32>::new()".into()],
177209
"bb0".into(), "bb0".into(), style.clone()
178210
),
179-
Node::new(
211+
Node::from_list(
180212
vec!["resume".into()],
181213
"bb1".into(), "bb1".into(), style.clone()
182214
),
183-
Node::new(
215+
Node::from_list(
184216
vec!["StorageLive(_2)".into(), "StorageLive(_3)".into(), "(_3.0: i32)".into(), "= const 1_i32".into(), "(_3.1: i32)".into(), "= const 10_i32".into(), "_2 = <std::ops::Range<i32> as IntoIterator>::into_iter(move _3)".into()],
185217
"bb2".into(), "bb2".into(), style.clone()
186218
),
187-
Node::new(
219+
Node::from_list(
188220
vec!["StorageDead(_3)".into(), "StorageLive(_4)".into(), "_4 = move _2".into(), "goto".into()],
189221
"bb3".into(), "bb3".into(), style.clone()
190222
),
191-
Node::new(
223+
Node::from_list(
192224
vec!["drop(_1)".into()],
193225
"bb4".into(), "bb4".into(), style.clone()
194226
),
195-
Node::new(
227+
Node::from_list(
196228
vec!["StorageLive(_5)".into(), "StorageLive(_6)".into(), "StorageLive(_7)".into(), "StorageLive(_8)".into(), "_8 = &mut _4 _7 = &mut (*_8)".into(), "_6 = <std::ops::Range<i32> as Iterator>::next(move _7)".into()],
197229
"bb5".into(), "bb5".into(), style.clone()
198230
),
199-
Node::new(
231+
Node::from_list(
200232
vec!["StorageDead(_7)".into(), "_9 = discriminant(_6)".into(), "switchInt(move _9)".into()],
201233
"bb6".into(), "bb6".into(), style.clone()
202234
),
203-
Node::new(
235+
Node::from_list(
204236
vec!["_0 = const () StorageDead(_8)".into(), "StorageDead(_6)".into(), "StorageDead(_5)".into(), "StorageDead(_4)".into(), "StorageDead(_2)".into(), "drop(_1)".into()],
205237
"bb7".into(), "bb7".into(), style.clone()
206238
),
207-
Node::new(
239+
Node::from_list(
208240
vec!["unreachable".into()],
209241
"bb8".into(), "bb8".into(), style.clone()
210242
),
211-
Node::new(
243+
Node::from_list(
212244
vec!["StorageLive(_10)".into(), "_10 = ((_6 as Some).0: i32)".into(), "StorageLive(_11)".into(), "_11 = _10 _5 = move _11 StorageDead(_11)".into(), "StorageDead(_10)".into(), "StorageDead(_8)".into(), "StorageDead(_6)".into(), "StorageLive(_12)".into(), "_12 = _5 StorageLive(_13)".into(), "StorageLive(_14)".into(), "_14 = _12 _15 = const false".into(), "_16 = Eq(_14, const i32::MIN) _17 = BitAnd(move _15, move _16)".into(), "assert(!move _17, attempt to compute the remainder of `{} % {}` which would overflow, _14, const 3_i32)".into()],
213245
"bb9".into(), "bb9".into(), style.clone()
214246
),
215-
Node::new(
247+
Node::from_list(
216248
vec!["_13 = Rem(move _14, const 3_i32)".into(), "StorageDead(_14)".into(), "switchInt(move _13)".into()],
217249
"bb10".into(), "bb10".into(), style.clone()
218250
),
219-
Node::new(
251+
Node::from_list(
220252
vec!["StorageDead(_13)".into(), "goto".into()],
221253
"bb11".into(), "bb11".into(), style.clone()
222254
),
223-
Node::new(
255+
Node::from_list(
224256
vec!["StorageDead(_13)".into(), "StorageLive(_18)".into(), "StorageLive(_19)".into(), "_19 = &mut _1 StorageLive(_20)".into(), "_20 = _12".into(), "_18 = Vec::<i32>::push(move _19, move _20)".into()],
225257
"bb12".into(), "bb12".into(), style.clone()
226258
),
227-
Node::new(
259+
Node::from_list(
228260
vec!["StorageDead(_20)".into(), "StorageDead(_19)".into(), "StorageDead(_18)".into(), "goto".into()],
229261
"bb13".into(), "bb13".into(), style.clone()
230262
),
231-
Node::new(
263+
Node::from_list(
232264
vec!["StorageDead(_12)".into(), "StorageDead(_5)".into(), "goto".into()],
233265
"bb14".into(), "bb14".into(), style.clone()
234266
),
235-
Node::new(
267+
Node::from_list(
236268
vec!["StorageDead(_1)".into(), "return".into()],
237269
"bb15".into(), "bb15".into(), style.clone()
238270
)],

0 commit comments

Comments
 (0)