62
62
//! dot::render(&edges, output).unwrap()
63
63
//! }
64
64
//!
65
- //! impl<'a> dot::Labeller<'a, Nd, Ed> for Edges {
65
+ //! impl<'a> dot::Labeller<'a> for Edges {
66
+ //! type Node = Nd;
67
+ //! type Edge = Ed;
66
68
//! fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example1").unwrap() }
67
69
//!
68
70
//! fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
69
71
//! dot::Id::new(format!("N{}", *n)).unwrap()
70
72
//! }
71
73
//! }
72
74
//!
73
- //! impl<'a> dot::GraphWalk<'a, Nd, Ed> for Edges {
75
+ //! impl<'a> dot::GraphWalk<'a> for Edges {
76
+ //! type Node = Nd;
77
+ //! type Edge = Ed;
74
78
//! fn nodes(&self) -> dot::Nodes<'a,Nd> {
75
79
//! // (assumes that |N| \approxeq |E|)
76
80
//! let &Edges(ref v) = self;
167
171
//! dot::render(&graph, output).unwrap()
168
172
//! }
169
173
//!
170
- //! impl<'a> dot::Labeller<'a, Nd, Ed<'a>> for Graph {
174
+ //! impl<'a> dot::Labeller<'a> for Graph {
175
+ //! type Node = Nd;
176
+ //! type Edge = Ed<'a>;
171
177
//! fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example2").unwrap() }
172
178
//! fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
173
179
//! dot::Id::new(format!("N{}", n)).unwrap()
180
186
//! }
181
187
//! }
182
188
//!
183
- //! impl<'a> dot::GraphWalk<'a, Nd, Ed<'a>> for Graph {
189
+ //! impl<'a> dot::GraphWalk<'a> for Graph {
190
+ //! type Node = Nd;
191
+ //! type Edge = Ed<'a>;
184
192
//! fn nodes(&self) -> dot::Nodes<'a,Nd> { (0..self.nodes.len()).collect() }
185
193
//! fn edges(&'a self) -> dot::Edges<'a,Ed<'a>> { self.edges.iter().collect() }
186
194
//! fn source(&self, e: &Ed) -> Nd { let & &(s,_) = e; s }
225
233
//! dot::render(&graph, output).unwrap()
226
234
//! }
227
235
//!
228
- //! impl<'a> dot::Labeller<'a, Nd<'a>, Ed<'a>> for Graph {
236
+ //! impl<'a> dot::Labeller<'a> for Graph {
237
+ //! type Node = Nd<'a>;
238
+ //! type Edge = Ed<'a>;
229
239
//! fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example3").unwrap() }
230
240
//! fn node_id(&'a self, n: &Nd<'a>) -> dot::Id<'a> {
231
241
//! dot::Id::new(format!("N{}", n.0)).unwrap()
239
249
//! }
240
250
//! }
241
251
//!
242
- //! impl<'a> dot::GraphWalk<'a, Nd<'a>, Ed<'a>> for Graph {
252
+ //! impl<'a> dot::GraphWalk<'a> for Graph {
253
+ //! type Node = Nd<'a>;
254
+ //! type Edge = Ed<'a>;
243
255
//! fn nodes(&'a self) -> dot::Nodes<'a,Nd<'a>> {
244
256
//! self.nodes.iter().map(|s| &s[..]).enumerate().collect()
245
257
//! }
@@ -447,45 +459,48 @@ impl<'a> Id<'a> {
447
459
/// The graph instance is responsible for providing the DOT compatible
448
460
/// identifiers for the nodes and (optionally) rendered labels for the nodes and
449
461
/// edges, as well as an identifier for the graph itself.
450
- pub trait Labeller < ' a , N , E > {
462
+ pub trait Labeller < ' a > {
463
+ type Node ;
464
+ type Edge ;
465
+
451
466
/// Must return a DOT compatible identifier naming the graph.
452
467
fn graph_id ( & ' a self ) -> Id < ' a > ;
453
468
454
469
/// Maps `n` to a unique identifier with respect to `self`. The
455
470
/// implementor is responsible for ensuring that the returned name
456
471
/// is a valid DOT identifier.
457
- fn node_id ( & ' a self , n : & N ) -> Id < ' a > ;
472
+ fn node_id ( & ' a self , n : & Self :: Node ) -> Id < ' a > ;
458
473
459
474
/// Maps `n` to one of the [graphviz `shape` names][1]. If `None`
460
475
/// is returned, no `shape` attribute is specified.
461
476
///
462
477
/// [1]: http://www.graphviz.org/content/node-shapes
463
- fn node_shape ( & ' a self , _node : & N ) -> Option < LabelText < ' a > > {
478
+ fn node_shape ( & ' a self , _node : & Self :: Node ) -> Option < LabelText < ' a > > {
464
479
None
465
480
}
466
481
467
482
/// Maps `n` to a label that will be used in the rendered output.
468
483
/// The label need not be unique, and may be the empty string; the
469
484
/// default is just the output from `node_id`.
470
- fn node_label ( & ' a self , n : & N ) -> LabelText < ' a > {
485
+ fn node_label ( & ' a self , n : & Self :: Node ) -> LabelText < ' a > {
471
486
LabelStr ( self . node_id ( n) . name )
472
487
}
473
488
474
489
/// Maps `e` to a label that will be used in the rendered output.
475
490
/// The label need not be unique, and may be the empty string; the
476
491
/// default is in fact the empty string.
477
- fn edge_label ( & ' a self , e : & E ) -> LabelText < ' a > {
492
+ fn edge_label ( & ' a self , e : & Self :: Edge ) -> LabelText < ' a > {
478
493
let _ignored = e;
479
494
LabelStr ( "" . into_cow ( ) )
480
495
}
481
496
482
497
/// Maps `n` to a style that will be used in the rendered output.
483
- fn node_style ( & ' a self , _n : & N ) -> Style {
498
+ fn node_style ( & ' a self , _n : & Self :: Node ) -> Style {
484
499
Style :: None
485
500
}
486
501
487
502
/// Maps `e` to a style that will be used in the rendered output.
488
- fn edge_style ( & ' a self , _e : & E ) -> Style {
503
+ fn edge_style ( & ' a self , _e : & Self :: Edge ) -> Style {
489
504
Style :: None
490
505
}
491
506
}
@@ -596,15 +611,18 @@ pub type Edges<'a,E> = Cow<'a,[E]>;
596
611
/// `Cow<[T]>` to leave implementors the freedom to create
597
612
/// entirely new vectors or to pass back slices into internally owned
598
613
/// vectors.
599
- pub trait GraphWalk < ' a , N : Clone , E : Clone > {
614
+ pub trait GraphWalk < ' a > {
615
+ type Node : Clone ;
616
+ type Edge : Clone ;
617
+
600
618
/// Returns all the nodes in this graph.
601
- fn nodes ( & ' a self ) -> Nodes < ' a , N > ;
619
+ fn nodes ( & ' a self ) -> Nodes < ' a , Self :: Node > ;
602
620
/// Returns all of the edges in this graph.
603
- fn edges ( & ' a self ) -> Edges < ' a , E > ;
621
+ fn edges ( & ' a self ) -> Edges < ' a , Self :: Edge > ;
604
622
/// The source node for `edge`.
605
- fn source ( & ' a self , edge : & E ) -> N ;
623
+ fn source ( & ' a self , edge : & Self :: Edge ) -> Self :: Node ;
606
624
/// The target node for `edge`.
607
- fn target ( & ' a self , edge : & E ) -> N ;
625
+ fn target ( & ' a self , edge : & Self :: Edge ) -> Self :: Node ;
608
626
}
609
627
610
628
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
@@ -622,28 +640,26 @@ pub fn default_options() -> Vec<RenderOption> {
622
640
623
641
/// Renders directed graph `g` into the writer `w` in DOT syntax.
624
642
/// (Simple wrapper around `render_opts` that passes a default set of options.)
625
- pub fn render < ' a ,
626
- N : Clone + ' a ,
627
- E : Clone + ' a ,
628
- G : Labeller < ' a , N , E > + GraphWalk < ' a , N , E > ,
629
- W : Write >
630
- ( g : & ' a G ,
631
- w : & mut W )
632
- -> io:: Result < ( ) > {
643
+ pub fn render < ' a , N , E , G , W > ( g : & ' a G , w : & mut W ) -> io:: Result < ( ) >
644
+ where N : Clone + ' a ,
645
+ E : Clone + ' a ,
646
+ G : Labeller < ' a , Node =N , Edge =E > + GraphWalk < ' a , Node =N , Edge =E > ,
647
+ W : Write
648
+ {
633
649
render_opts ( g, w, & [ ] )
634
650
}
635
651
636
652
/// Renders directed graph `g` into the writer `w` in DOT syntax.
637
653
/// (Main entry point for the library.)
638
- pub fn render_opts < ' a ,
639
- N : Clone + ' a ,
640
- E : Clone + ' a ,
641
- G : Labeller < ' a , N , E > + GraphWalk < ' a , N , E > ,
642
- W : Write >
643
- ( g : & ' a G ,
644
- w : & mut W ,
645
- options : & [ RenderOption ] )
646
- -> io :: Result < ( ) > {
654
+ pub fn render_opts < ' a , N , E , G , W > ( g : & ' a G ,
655
+ w : & mut W ,
656
+ options : & [ RenderOption ] )
657
+ -> io :: Result < ( ) >
658
+ where N : Clone + ' a ,
659
+ E : Clone + ' a ,
660
+ G : Labeller < ' a , Node = N , Edge = E > + GraphWalk < ' a , Node = N , Edge = E > ,
661
+ W : Write
662
+ {
647
663
fn writeln < W : Write > ( w : & mut W , arg : & [ & str ] ) -> io:: Result < ( ) > {
648
664
for & s in arg {
649
665
try!( w. write_all ( s. as_bytes ( ) ) ) ;
@@ -858,7 +874,9 @@ mod tests {
858
874
Id :: new ( format ! ( "N{}" , * n) ) . unwrap ( )
859
875
}
860
876
861
- impl < ' a > Labeller < ' a , Node , & ' a Edge > for LabelledGraph {
877
+ impl < ' a > Labeller < ' a > for LabelledGraph {
878
+ type Node = Node ;
879
+ type Edge = & ' a Edge ;
862
880
fn graph_id ( & ' a self ) -> Id < ' a > {
863
881
Id :: new ( & self . name [ ..] ) . unwrap ( )
864
882
}
@@ -882,7 +900,9 @@ mod tests {
882
900
}
883
901
}
884
902
885
- impl < ' a > Labeller < ' a , Node , & ' a Edge > for LabelledGraphWithEscStrs {
903
+ impl < ' a > Labeller < ' a > for LabelledGraphWithEscStrs {
904
+ type Node = Node ;
905
+ type Edge = & ' a Edge ;
886
906
fn graph_id ( & ' a self ) -> Id < ' a > {
887
907
self . graph . graph_id ( )
888
908
}
@@ -901,7 +921,9 @@ mod tests {
901
921
}
902
922
}
903
923
904
- impl < ' a > GraphWalk < ' a , Node , & ' a Edge > for LabelledGraph {
924
+ impl < ' a > GraphWalk < ' a > for LabelledGraph {
925
+ type Node = Node ;
926
+ type Edge = & ' a Edge ;
905
927
fn nodes ( & ' a self ) -> Nodes < ' a , Node > {
906
928
( 0 ..self . node_labels . len ( ) ) . collect ( )
907
929
}
@@ -916,7 +938,9 @@ mod tests {
916
938
}
917
939
}
918
940
919
- impl < ' a > GraphWalk < ' a , Node , & ' a Edge > for LabelledGraphWithEscStrs {
941
+ impl < ' a > GraphWalk < ' a > for LabelledGraphWithEscStrs {
942
+ type Node = Node ;
943
+ type Edge = & ' a Edge ;
920
944
fn nodes ( & ' a self ) -> Nodes < ' a , Node > {
921
945
self . graph . nodes ( )
922
946
}
0 commit comments