6262//! dot::render(&edges, output).unwrap()
6363//! }
6464//!
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;
6668//! fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example1").unwrap() }
6769//!
6870//! fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
6971//! dot::Id::new(format!("N{}", *n)).unwrap()
7072//! }
7173//! }
7274//!
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;
7478//! fn nodes(&self) -> dot::Nodes<'a,Nd> {
7579//! // (assumes that |N| \approxeq |E|)
7680//! let &Edges(ref v) = self;
167171//! dot::render(&graph, output).unwrap()
168172//! }
169173//!
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>;
171177//! fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example2").unwrap() }
172178//! fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
173179//! dot::Id::new(format!("N{}", n)).unwrap()
180186//! }
181187//! }
182188//!
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>;
184192//! fn nodes(&self) -> dot::Nodes<'a,Nd> { (0..self.nodes.len()).collect() }
185193//! fn edges(&'a self) -> dot::Edges<'a,Ed<'a>> { self.edges.iter().collect() }
186194//! fn source(&self, e: &Ed) -> Nd { let & &(s,_) = e; s }
225233//! dot::render(&graph, output).unwrap()
226234//! }
227235//!
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>;
229239//! fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example3").unwrap() }
230240//! fn node_id(&'a self, n: &Nd<'a>) -> dot::Id<'a> {
231241//! dot::Id::new(format!("N{}", n.0)).unwrap()
239249//! }
240250//! }
241251//!
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>;
243255//! fn nodes(&'a self) -> dot::Nodes<'a,Nd<'a>> {
244256//! self.nodes.iter().map(|s| &s[..]).enumerate().collect()
245257//! }
@@ -447,45 +459,48 @@ impl<'a> Id<'a> {
447459/// The graph instance is responsible for providing the DOT compatible
448460/// identifiers for the nodes and (optionally) rendered labels for the nodes and
449461/// 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+
451466 /// Must return a DOT compatible identifier naming the graph.
452467 fn graph_id ( & ' a self ) -> Id < ' a > ;
453468
454469 /// Maps `n` to a unique identifier with respect to `self`. The
455470 /// implementor is responsible for ensuring that the returned name
456471 /// 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 > ;
458473
459474 /// Maps `n` to one of the [graphviz `shape` names][1]. If `None`
460475 /// is returned, no `shape` attribute is specified.
461476 ///
462477 /// [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 > > {
464479 None
465480 }
466481
467482 /// Maps `n` to a label that will be used in the rendered output.
468483 /// The label need not be unique, and may be the empty string; the
469484 /// 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 > {
471486 LabelStr ( self . node_id ( n) . name )
472487 }
473488
474489 /// Maps `e` to a label that will be used in the rendered output.
475490 /// The label need not be unique, and may be the empty string; the
476491 /// 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 > {
478493 let _ignored = e;
479494 LabelStr ( "" . into_cow ( ) )
480495 }
481496
482497 /// 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 {
484499 Style :: None
485500 }
486501
487502 /// 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 {
489504 Style :: None
490505 }
491506}
@@ -596,15 +611,18 @@ pub type Edges<'a,E> = Cow<'a,[E]>;
596611/// `Cow<[T]>` to leave implementors the freedom to create
597612/// entirely new vectors or to pass back slices into internally owned
598613/// vectors.
599- pub trait GraphWalk < ' a , N : Clone , E : Clone > {
614+ pub trait GraphWalk < ' a > {
615+ type Node : Clone ;
616+ type Edge : Clone ;
617+
600618 /// Returns all the nodes in this graph.
601- fn nodes ( & ' a self ) -> Nodes < ' a , N > ;
619+ fn nodes ( & ' a self ) -> Nodes < ' a , Self :: Node > ;
602620 /// 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 > ;
604622 /// The source node for `edge`.
605- fn source ( & ' a self , edge : & E ) -> N ;
623+ fn source ( & ' a self , edge : & Self :: Edge ) -> Self :: Node ;
606624 /// The target node for `edge`.
607- fn target ( & ' a self , edge : & E ) -> N ;
625+ fn target ( & ' a self , edge : & Self :: Edge ) -> Self :: Node ;
608626}
609627
610628#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
@@ -622,28 +640,26 @@ pub fn default_options() -> Vec<RenderOption> {
622640
623641/// Renders directed graph `g` into the writer `w` in DOT syntax.
624642/// (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+ {
633649 render_opts ( g, w, & [ ] )
634650}
635651
636652/// Renders directed graph `g` into the writer `w` in DOT syntax.
637653/// (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+ {
647663 fn writeln < W : Write > ( w : & mut W , arg : & [ & str ] ) -> io:: Result < ( ) > {
648664 for & s in arg {
649665 try!( w. write_all ( s. as_bytes ( ) ) ) ;
@@ -858,7 +874,9 @@ mod tests {
858874 Id :: new ( format ! ( "N{}" , * n) ) . unwrap ( )
859875 }
860876
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 ;
862880 fn graph_id ( & ' a self ) -> Id < ' a > {
863881 Id :: new ( & self . name [ ..] ) . unwrap ( )
864882 }
@@ -882,7 +900,9 @@ mod tests {
882900 }
883901 }
884902
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 ;
886906 fn graph_id ( & ' a self ) -> Id < ' a > {
887907 self . graph . graph_id ( )
888908 }
@@ -901,7 +921,9 @@ mod tests {
901921 }
902922 }
903923
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 ;
905927 fn nodes ( & ' a self ) -> Nodes < ' a , Node > {
906928 ( 0 ..self . node_labels . len ( ) ) . collect ( )
907929 }
@@ -916,7 +938,9 @@ mod tests {
916938 }
917939 }
918940
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 ;
920944 fn nodes ( & ' a self ) -> Nodes < ' a , Node > {
921945 self . graph . nodes ( )
922946 }
0 commit comments