Skip to content

Commit baeae78

Browse files
committed
Switch libgraphviz from type params to associated types for Node/Edge.
1 parent 7ec8f5c commit baeae78

File tree

5 files changed

+90
-48
lines changed

5 files changed

+90
-48
lines changed

src/libgraphviz/lib.rs

+63-39
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,19 @@
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;
@@ -167,7 +171,9 @@
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()
@@ -180,7 +186,9 @@
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 }
@@ -225,7 +233,9 @@
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()
@@ -239,7 +249,9 @@
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
}

src/librustc/middle/cfg/graphviz.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ fn replace_newline_with_backslash_l(s: String) -> String {
5252
}
5353
}
5454

55-
impl<'a, 'ast> dot::Labeller<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast> {
55+
impl<'a, 'ast> dot::Labeller<'a> for LabelledCFG<'a, 'ast> {
56+
type Node = Node<'a>;
57+
type Edge = Edge<'a>;
5658
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(&self.name[..]).unwrap() }
5759

5860
fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> {
@@ -97,7 +99,9 @@ impl<'a, 'ast> dot::Labeller<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast> {
9799
}
98100
}
99101

100-
impl<'a> dot::GraphWalk<'a, Node<'a>, Edge<'a>> for &'a cfg::CFG {
102+
impl<'a> dot::GraphWalk<'a> for &'a cfg::CFG {
103+
type Node = Node<'a>;
104+
type Edge = Edge<'a>;
101105
fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> {
102106
let mut v = Vec::new();
103107
self.graph.each_node(|i, nd| { v.push((i, nd)); true });
@@ -116,8 +120,10 @@ impl<'a> dot::GraphWalk<'a, Node<'a>, Edge<'a>> for &'a cfg::CFG {
116120
}
117121
}
118122

119-
impl<'a, 'ast> dot::GraphWalk<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast>
123+
impl<'a, 'ast> dot::GraphWalk<'a> for LabelledCFG<'a, 'ast>
120124
{
125+
type Node = Node<'a>;
126+
type Edge = Edge<'a>;
121127
fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> { self.cfg.nodes() }
122128
fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> { self.cfg.edges() }
123129
fn source(&'a self, edge: &Edge<'a>) -> Node<'a> { self.cfg.source(edge) }

src/librustc/middle/infer/region_inference/graphviz.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ impl<'a, 'tcx> ConstraintGraph<'a, 'tcx> {
173173
}
174174
}
175175

176-
impl<'a, 'tcx> dot::Labeller<'a, Node, Edge> for ConstraintGraph<'a, 'tcx> {
176+
impl<'a, 'tcx> dot::Labeller<'a> for ConstraintGraph<'a, 'tcx> {
177+
type Node = Node;
178+
type Edge = Edge;
177179
fn graph_id(&self) -> dot::Id {
178180
dot::Id::new(&*self.graph_name).unwrap()
179181
}
@@ -224,7 +226,9 @@ fn edge_to_nodes(e: &Edge) -> (Node, Node) {
224226
}
225227
}
226228

227-
impl<'a, 'tcx> dot::GraphWalk<'a, Node, Edge> for ConstraintGraph<'a, 'tcx> {
229+
impl<'a, 'tcx> dot::GraphWalk<'a> for ConstraintGraph<'a, 'tcx> {
230+
type Node = Node;
231+
type Edge = Edge;
228232
fn nodes(&self) -> dot::Nodes<Node> {
229233
let mut set = FnvHashSet();
230234
for node in self.node_ids.keys() {

src/librustc_borrowck/graphviz.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ impl<'a, 'tcx> DataflowLabeller<'a, 'tcx> {
129129
}
130130
}
131131

132-
impl<'a, 'tcx> dot::Labeller<'a, Node<'a>, Edge<'a>> for DataflowLabeller<'a, 'tcx> {
132+
impl<'a, 'tcx> dot::Labeller<'a> for DataflowLabeller<'a, 'tcx> {
133+
type Node = Node<'a>;
134+
type Edge = Edge<'a>;
133135
fn graph_id(&'a self) -> dot::Id<'a> { self.inner.graph_id() }
134136
fn node_id(&'a self, n: &Node<'a>) -> dot::Id<'a> { self.inner.node_id(n) }
135137
fn node_label(&'a self, n: &Node<'a>) -> dot::LabelText<'a> {
@@ -143,7 +145,9 @@ impl<'a, 'tcx> dot::Labeller<'a, Node<'a>, Edge<'a>> for DataflowLabeller<'a, 't
143145
fn edge_label(&'a self, e: &Edge<'a>) -> dot::LabelText<'a> { self.inner.edge_label(e) }
144146
}
145147

146-
impl<'a, 'tcx> dot::GraphWalk<'a, Node<'a>, Edge<'a>> for DataflowLabeller<'a, 'tcx> {
148+
impl<'a, 'tcx> dot::GraphWalk<'a> for DataflowLabeller<'a, 'tcx> {
149+
type Node = Node<'a>;
150+
type Edge = Edge<'a>;
147151
fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> { self.inner.nodes() }
148152
fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> { self.inner.edges() }
149153
fn source(&'a self, edge: &Edge<'a>) -> Node<'a> { self.inner.source(edge) }

src/librustc_trans/trans/assert_dep_graph.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ fn dump_graph(tcx: &TyCtxt) {
253253

254254
pub struct GraphvizDepGraph(FnvHashSet<DepNode>, Vec<(DepNode, DepNode)>);
255255

256-
impl<'a, 'tcx> dot::GraphWalk<'a, DepNode, (DepNode, DepNode)> for GraphvizDepGraph {
256+
impl<'a, 'tcx> dot::GraphWalk<'a> for GraphvizDepGraph {
257+
type Node = DepNode;
258+
type Edge = (DepNode, DepNode);
257259
fn nodes(&self) -> dot::Nodes<DepNode> {
258260
let nodes: Vec<_> = self.0.iter().cloned().collect();
259261
nodes.into_cow()
@@ -269,7 +271,9 @@ impl<'a, 'tcx> dot::GraphWalk<'a, DepNode, (DepNode, DepNode)> for GraphvizDepGr
269271
}
270272
}
271273

272-
impl<'a, 'tcx> dot::Labeller<'a, DepNode, (DepNode, DepNode)> for GraphvizDepGraph {
274+
impl<'a, 'tcx> dot::Labeller<'a> for GraphvizDepGraph {
275+
type Node = DepNode;
276+
type Edge = (DepNode, DepNode);
273277
fn graph_id(&self) -> dot::Id {
274278
dot::Id::new("DependencyGraph").unwrap()
275279
}

0 commit comments

Comments
 (0)