summary refs log tree commit diff
path: root/src/libgraphviz
diff options
context:
space:
mode:
authorFelix S. Klock II <pnkfelix@pnkfx.org>2014-05-06 11:54:21 +0200
committerFelix S. Klock II <pnkfelix@pnkfx.org>2014-05-07 00:52:27 +0200
commita22413b46aa57ed22e59a9c0e4d581d1eabfe2b2 (patch)
tree0096a514974580e8f8970e8dd50a97b9d7a71300 /src/libgraphviz
parent0605f05e42dfe4476500b4341a5ba78398b02c0f (diff)
downloadrust-a22413b46aa57ed22e59a9c0e4d581d1eabfe2b2.tar.gz
rust-a22413b46aa57ed22e59a9c0e4d581d1eabfe2b2.zip
Revise doc-comments for graphviz to avoid generating files from rustdoc runs.
Fix #13965.

There is a dance here between the `main` that actually runs versus the
`main` that is printed in the output documentation.  We don't run the
latter `main`, but we do at least compile (and thus type-check) it.
It is still the responsibility of the documenter to ensure that the
signatures of `fn render` are kept in sync across the blocks.
Diffstat (limited to 'src/libgraphviz')
-rw-r--r--src/libgraphviz/lib.rs51
1 files changed, 39 insertions, 12 deletions
diff --git a/src/libgraphviz/lib.rs b/src/libgraphviz/lib.rs
index afe29ea2e07..f2d4d84f601 100644
--- a/src/libgraphviz/lib.rs
+++ b/src/libgraphviz/lib.rs
@@ -54,11 +54,9 @@ type Nd = int;
 type Ed = (int,int);
 struct Edges(Vec<Ed>);
 
-pub fn main() {
-    use std::io::File;
+pub fn render_to<W:Writer>(output: &mut W) {
     let edges = Edges(vec!((0,1), (0,2), (1,3), (2,3), (3,4), (4,4)));
-    let mut f = File::create(&Path::new("example1.dot"));
-    dot::render(&edges, &mut f).unwrap()
+    dot::render(&edges, output).unwrap()
 }
 
 impl<'a> dot::Labeller<'a, Nd, Ed> for Edges {
@@ -91,6 +89,17 @@ impl<'a> dot::GraphWalk<'a, Nd, Ed> for Edges {
 
     fn target(&self, e: &Ed) -> Nd { let &(_,t) = e; t }
 }
+
+# pub fn main() { use std::io::MemWriter; render_to(&mut MemWriter::new()) }
+```
+
+```no_run
+# pub fn render_to<W:Writer>(output: &mut W) { unimplemented!() }
+pub fn main() {
+    use std::io::File;
+    let mut f = File::create(&Path::new("example1.dot"));
+    render_to(&mut f)
+}
 ```
 
 Output from first example (in `example1.dot`):
@@ -140,19 +149,17 @@ entity `&sube`).
 ```rust
 use dot = graphviz;
 use std::str;
-use std::io::File;
 
 type Nd = uint;
 type Ed<'a> = &'a (uint, uint);
 struct Graph { nodes: Vec<&'static str>, edges: Vec<(uint,uint)> }
 
-pub fn main() {
+pub fn render_to<W:Writer>(output: &mut W) {
     let nodes = vec!("{x,y}","{x}","{y}","{}");
     let edges = vec!((0,1), (0,2), (1,3), (2,3));
     let graph = Graph { nodes: nodes, edges: edges };
 
-    let mut f = File::create(&Path::new("example2.dot"));
-    dot::render(&graph, &mut f).unwrap()
+    dot::render(&graph, output).unwrap()
 }
 
 impl<'a> dot::Labeller<'a, Nd, Ed<'a>> for Graph {
@@ -174,6 +181,17 @@ impl<'a> dot::GraphWalk<'a, Nd, Ed<'a>> for Graph {
     fn source(&self, e: &Ed) -> Nd { let & &(s,_) = e; s }
     fn target(&self, e: &Ed) -> Nd { let & &(_,t) = e; t }
 }
+
+# pub fn main() { use std::io::MemWriter; render_to(&mut MemWriter::new()) }
+```
+
+```no_run
+# pub fn render_to<W:Writer>(output: &mut W) { unimplemented!() }
+pub fn main() {
+    use std::io::File;
+    let mut f = File::create(&Path::new("example2.dot"));
+    render_to(&mut f)
+}
 ```
 
 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}`.
 ```rust
 use dot = graphviz;
 use std::str;
-use std::io::File;
 
 type Nd<'a> = (uint, &'a str);
 type Ed<'a> = (Nd<'a>, Nd<'a>);
 struct Graph { nodes: Vec<&'static str>, edges: Vec<(uint,uint)> }
 
-pub fn main() {
+pub fn render_to<W:Writer>(output: &mut W) {
     let nodes = vec!("{x,y}","{x}","{y}","{}");
     let edges = vec!((0,1), (0,2), (1,3), (2,3));
     let graph = Graph { nodes: nodes, edges: edges };
 
-    let mut f = File::create(&Path::new("example3.dot"));
-    dot::render(&graph, &mut f).unwrap()
+    dot::render(&graph, output).unwrap()
 }
 
 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 {
     fn source(&self, e: &Ed<'a>) -> Nd<'a> { let &(s,_) = e; s }
     fn target(&self, e: &Ed<'a>) -> Nd<'a> { let &(_,t) = e; t }
 }
+
+# pub fn main() { use std::io::MemWriter; render_to(&mut MemWriter::new()) }
+```
+
+```no_run
+# pub fn render_to<W:Writer>(output: &mut W) { unimplemented!() }
+pub fn main() {
+    use std::io::File;
+    let mut f = File::create(&Path::new("example3.dot"));
+    render_to(&mut f)
+}
 ```
 
 # References