Skip to content

Commit 26632d5

Browse files
committed
auto merge of #13976 : pnkfelix/rust/fsk-fix-13965, r=alexcrichton
Fix #13965. This commit adopts the second strategy I outlined in #13965, where the bulk of the code is still "smoke tested" (in the sense that rustdoc attempts to run it, sending all of the generated output into a locally allocated `MemWriter`). The part of the code that is ignored (but included in the presentation) is isolated to a three-line `main` function that invokes the core rendering routine. In the generated rustdoc output, this leads to a small break between the two code blocks, but I do not think this is a large issue.
2 parents e0fcb4e + a22413b commit 26632d5

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/libgraphviz/lib.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,9 @@ type Nd = int;
5454
type Ed = (int,int);
5555
struct Edges(Vec<Ed>);
5656
57-
pub fn main() {
58-
use std::io::File;
57+
pub fn render_to<W:Writer>(output: &mut W) {
5958
let edges = Edges(vec!((0,1), (0,2), (1,3), (2,3), (3,4), (4,4)));
60-
let mut f = File::create(&Path::new("example1.dot"));
61-
dot::render(&edges, &mut f).unwrap()
59+
dot::render(&edges, output).unwrap()
6260
}
6361
6462
impl<'a> dot::Labeller<'a, Nd, Ed> for Edges {
@@ -91,6 +89,17 @@ impl<'a> dot::GraphWalk<'a, Nd, Ed> for Edges {
9189
9290
fn target(&self, e: &Ed) -> Nd { let &(_,t) = e; t }
9391
}
92+
93+
# pub fn main() { use std::io::MemWriter; render_to(&mut MemWriter::new()) }
94+
```
95+
96+
```no_run
97+
# pub fn render_to<W:Writer>(output: &mut W) { unimplemented!() }
98+
pub fn main() {
99+
use std::io::File;
100+
let mut f = File::create(&Path::new("example1.dot"));
101+
render_to(&mut f)
102+
}
94103
```
95104
96105
Output from first example (in `example1.dot`):
@@ -140,19 +149,17 @@ entity `&sube`).
140149
```rust
141150
use dot = graphviz;
142151
use std::str;
143-
use std::io::File;
144152
145153
type Nd = uint;
146154
type Ed<'a> = &'a (uint, uint);
147155
struct Graph { nodes: Vec<&'static str>, edges: Vec<(uint,uint)> }
148156
149-
pub fn main() {
157+
pub fn render_to<W:Writer>(output: &mut W) {
150158
let nodes = vec!("{x,y}","{x}","{y}","{}");
151159
let edges = vec!((0,1), (0,2), (1,3), (2,3));
152160
let graph = Graph { nodes: nodes, edges: edges };
153161
154-
let mut f = File::create(&Path::new("example2.dot"));
155-
dot::render(&graph, &mut f).unwrap()
162+
dot::render(&graph, output).unwrap()
156163
}
157164
158165
impl<'a> dot::Labeller<'a, Nd, Ed<'a>> for Graph {
@@ -174,6 +181,17 @@ impl<'a> dot::GraphWalk<'a, Nd, Ed<'a>> for Graph {
174181
fn source(&self, e: &Ed) -> Nd { let & &(s,_) = e; s }
175182
fn target(&self, e: &Ed) -> Nd { let & &(_,t) = e; t }
176183
}
184+
185+
# pub fn main() { use std::io::MemWriter; render_to(&mut MemWriter::new()) }
186+
```
187+
188+
```no_run
189+
# pub fn render_to<W:Writer>(output: &mut W) { unimplemented!() }
190+
pub fn main() {
191+
use std::io::File;
192+
let mut f = File::create(&Path::new("example2.dot"));
193+
render_to(&mut f)
194+
}
177195
```
178196
179197
The third example is similar to the second, except now each node and
@@ -187,19 +205,17 @@ Hasse-diagram for the subsets of the set `{x, y}`.
187205
```rust
188206
use dot = graphviz;
189207
use std::str;
190-
use std::io::File;
191208
192209
type Nd<'a> = (uint, &'a str);
193210
type Ed<'a> = (Nd<'a>, Nd<'a>);
194211
struct Graph { nodes: Vec<&'static str>, edges: Vec<(uint,uint)> }
195212
196-
pub fn main() {
213+
pub fn render_to<W:Writer>(output: &mut W) {
197214
let nodes = vec!("{x,y}","{x}","{y}","{}");
198215
let edges = vec!((0,1), (0,2), (1,3), (2,3));
199216
let graph = Graph { nodes: nodes, edges: edges };
200217
201-
let mut f = File::create(&Path::new("example3.dot"));
202-
dot::render(&graph, &mut f).unwrap()
218+
dot::render(&graph, output).unwrap()
203219
}
204220
205221
impl<'a> dot::Labeller<'a, Nd<'a>, Ed<'a>> for Graph {
@@ -229,6 +245,17 @@ impl<'a> dot::GraphWalk<'a, Nd<'a>, Ed<'a>> for Graph {
229245
fn source(&self, e: &Ed<'a>) -> Nd<'a> { let &(s,_) = e; s }
230246
fn target(&self, e: &Ed<'a>) -> Nd<'a> { let &(_,t) = e; t }
231247
}
248+
249+
# pub fn main() { use std::io::MemWriter; render_to(&mut MemWriter::new()) }
250+
```
251+
252+
```no_run
253+
# pub fn render_to<W:Writer>(output: &mut W) { unimplemented!() }
254+
pub fn main() {
255+
use std::io::File;
256+
let mut f = File::create(&Path::new("example3.dot"));
257+
render_to(&mut f)
258+
}
232259
```
233260
234261
# References

0 commit comments

Comments
 (0)