@@ -54,11 +54,9 @@ type Nd = int;
5454type Ed = (int,int);
5555struct 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
6462impl<'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
96105Output from first example (in `example1.dot`):
@@ -140,19 +149,17 @@ entity `&sube`).
140149```rust
141150use dot = graphviz;
142151use std::str;
143- use std::io::File;
144152
145153type Nd = uint;
146154type Ed<'a> = &'a (uint, uint);
147155struct 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
158165impl<'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
179197The 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
188206use dot = graphviz;
189207use std::str;
190- use std::io::File;
191208
192209type Nd<'a> = (uint, &'a str);
193210type Ed<'a> = (Nd<'a>, Nd<'a>);
194211struct 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
205221impl<'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