@@ -54,11 +54,9 @@ type Nd = int;
54
54
type Ed = (int,int);
55
55
struct Edges(Vec<Ed>);
56
56
57
- pub fn main() {
58
- use std::io::File;
57
+ pub fn render_to<W:Writer>(output: &mut W) {
59
58
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()
62
60
}
63
61
64
62
impl<'a> dot::Labeller<'a, Nd, Ed> for Edges {
@@ -91,6 +89,17 @@ impl<'a> dot::GraphWalk<'a, Nd, Ed> for Edges {
91
89
92
90
fn target(&self, e: &Ed) -> Nd { let &(_,t) = e; t }
93
91
}
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
+ }
94
103
```
95
104
96
105
Output from first example (in `example1.dot`):
@@ -140,19 +149,17 @@ entity `&sube`).
140
149
```rust
141
150
use dot = graphviz;
142
151
use std::str;
143
- use std::io::File;
144
152
145
153
type Nd = uint;
146
154
type Ed<'a> = &'a (uint, uint);
147
155
struct Graph { nodes: Vec<&'static str>, edges: Vec<(uint,uint)> }
148
156
149
- pub fn main( ) {
157
+ pub fn render_to<W:Writer>(output: &mut W ) {
150
158
let nodes = vec!("{x,y}","{x}","{y}","{}");
151
159
let edges = vec!((0,1), (0,2), (1,3), (2,3));
152
160
let graph = Graph { nodes: nodes, edges: edges };
153
161
154
- let mut f = File::create(&Path::new("example2.dot"));
155
- dot::render(&graph, &mut f).unwrap()
162
+ dot::render(&graph, output).unwrap()
156
163
}
157
164
158
165
impl<'a> dot::Labeller<'a, Nd, Ed<'a>> for Graph {
@@ -174,6 +181,17 @@ impl<'a> dot::GraphWalk<'a, Nd, Ed<'a>> for Graph {
174
181
fn source(&self, e: &Ed) -> Nd { let & &(s,_) = e; s }
175
182
fn target(&self, e: &Ed) -> Nd { let & &(_,t) = e; t }
176
183
}
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
+ }
177
195
```
178
196
179
197
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}`.
187
205
```rust
188
206
use dot = graphviz;
189
207
use std::str;
190
- use std::io::File;
191
208
192
209
type Nd<'a> = (uint, &'a str);
193
210
type Ed<'a> = (Nd<'a>, Nd<'a>);
194
211
struct Graph { nodes: Vec<&'static str>, edges: Vec<(uint,uint)> }
195
212
196
- pub fn main( ) {
213
+ pub fn render_to<W:Writer>(output: &mut W ) {
197
214
let nodes = vec!("{x,y}","{x}","{y}","{}");
198
215
let edges = vec!((0,1), (0,2), (1,3), (2,3));
199
216
let graph = Graph { nodes: nodes, edges: edges };
200
217
201
- let mut f = File::create(&Path::new("example3.dot"));
202
- dot::render(&graph, &mut f).unwrap()
218
+ dot::render(&graph, output).unwrap()
203
219
}
204
220
205
221
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 {
229
245
fn source(&self, e: &Ed<'a>) -> Nd<'a> { let &(s,_) = e; s }
230
246
fn target(&self, e: &Ed<'a>) -> Nd<'a> { let &(_,t) = e; t }
231
247
}
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
+ }
232
259
```
233
260
234
261
# References
0 commit comments