@@ -37,7 +37,7 @@ pairs of ints, representing the edges (the node set is implicit).
37
37
Each node label is derived directly from the int representing the node,
38
38
while the edge labels are all empty strings.
39
39
40
- This example also illustrates how to use `MaybeOwnedVector ` to return
40
+ This example also illustrates how to use `CowVec ` to return
41
41
an owned vector or a borrowed slice as appropriate: we construct the
42
42
node vector from scratch, but borrow the edge list (rather than
43
43
constructing a copy of all the edges from scratch).
@@ -48,7 +48,6 @@ which is cyclic.
48
48
49
49
```rust
50
50
use graphviz as dot;
51
- use graphviz::maybe_owned_vec::IntoMaybeOwnedVector;
52
51
53
52
type Nd = int;
54
53
type Ed = (int,int);
@@ -77,12 +76,12 @@ impl<'a> dot::GraphWalk<'a, Nd, Ed> for Edges {
77
76
}
78
77
nodes.sort();
79
78
nodes.dedup();
80
- nodes.into_maybe_owned ()
79
+ nodes.into_cow ()
81
80
}
82
81
83
82
fn edges(&'a self) -> dot::Edges<'a,Ed> {
84
83
let &Edges(ref edges) = self;
85
- edges.as_slice().into_maybe_owned ()
84
+ edges.as_slice().into_cow ()
86
85
}
87
86
88
87
fn source(&self, e: &Ed) -> Nd { let &(s,_) = e; s }
@@ -137,8 +136,8 @@ edges stored in `self`.
137
136
Since both the set of nodes and the set of edges are always
138
137
constructed from scratch via iterators, we use the `collect()` method
139
138
from the `Iterator` trait to collect the nodes and edges into freshly
140
- constructed growable `Vec` values (rather use the `into_maybe_owned `
141
- from the `IntoMaybeOwnedVector ` trait as was used in the first example
139
+ constructed growable `Vec` values (rather use the `into_cow `
140
+ from the `IntoCow ` trait as was used in the first example
142
141
above).
143
142
144
143
The output from this example renders four nodes that make up the
@@ -148,7 +147,6 @@ entity `&sube`).
148
147
149
148
```rust
150
149
use graphviz as dot;
151
- use std::str;
152
150
153
151
type Nd = uint;
154
152
type Ed<'a> = &'a (uint, uint);
@@ -168,10 +166,10 @@ impl<'a> dot::Labeller<'a, Nd, Ed<'a>> for Graph {
168
166
dot::Id::new(format!("N{}", n)).unwrap()
169
167
}
170
168
fn node_label<'a>(&'a self, n: &Nd) -> dot::LabelText<'a> {
171
- dot::LabelStr(str::Slice( self.nodes[*n].as_slice()))
169
+ dot::LabelStr(self.nodes[*n].as_slice().into_cow( ))
172
170
}
173
171
fn edge_label<'a>(&'a self, _: &Ed) -> dot::LabelText<'a> {
174
- dot::LabelStr(str::Slice( "⊆"))
172
+ dot::LabelStr("⊆".into_cow( ))
175
173
}
176
174
}
177
175
@@ -204,7 +202,6 @@ Hasse-diagram for the subsets of the set `{x, y}`.
204
202
205
203
```rust
206
204
use graphviz as dot;
207
- use std::str;
208
205
209
206
type Nd<'a> = (uint, &'a str);
210
207
type Ed<'a> = (Nd<'a>, Nd<'a>);
@@ -225,10 +222,10 @@ impl<'a> dot::Labeller<'a, Nd<'a>, Ed<'a>> for Graph {
225
222
}
226
223
fn node_label<'a>(&'a self, n: &Nd<'a>) -> dot::LabelText<'a> {
227
224
let &(i, _) = n;
228
- dot::LabelStr(str::Slice( self.nodes[i].as_slice()))
225
+ dot::LabelStr(self.nodes[i].as_slice().into_cow( ))
229
226
}
230
227
fn edge_label<'a>(&'a self, _: &Ed<'a>) -> dot::LabelText<'a> {
231
- dot::LabelStr(str::Slice( "⊆"))
228
+ dot::LabelStr("⊆".into_cow( ))
232
229
}
233
230
}
234
231
@@ -279,8 +276,8 @@ pub fn main() {
279
276
pub use self :: LabelText :: * ;
280
277
281
278
use std:: io;
282
- use std:: str;
283
- use self :: maybe_owned_vec :: MaybeOwnedVector ;
279
+ use std:: str:: CowString ;
280
+ use std :: vec :: CowVec ;
284
281
285
282
pub mod maybe_owned_vec;
286
283
@@ -290,7 +287,7 @@ pub enum LabelText<'a> {
290
287
///
291
288
/// Occurrences of backslashes (`\`) are escaped, and thus appear
292
289
/// as backslashes in the rendered label.
293
- LabelStr ( str :: MaybeOwned < ' a > ) ,
290
+ LabelStr ( CowString < ' a > ) ,
294
291
295
292
/// This kind of label uses the graphviz label escString type:
296
293
/// http://www.graphviz.org/content/attrs#kescString
@@ -302,7 +299,7 @@ pub enum LabelText<'a> {
302
299
/// to break a line (centering the line preceding the `\n`), there
303
300
/// are also the escape sequences `\l` which left-justifies the
304
301
/// preceding line and `\r` which right-justifies it.
305
- EscStr ( str :: MaybeOwned < ' a > ) ,
302
+ EscStr ( CowString < ' a > ) ,
306
303
}
307
304
308
305
// There is a tension in the design of the labelling API.
@@ -339,7 +336,7 @@ pub enum LabelText<'a> {
339
336
340
337
/// `Id` is a Graphviz `ID`.
341
338
pub struct Id < ' a > {
342
- name : str :: MaybeOwned < ' a > ,
339
+ name : CowString < ' a > ,
343
340
}
344
341
345
342
impl < ' a > Id < ' a > {
@@ -357,10 +354,10 @@ impl<'a> Id<'a> {
357
354
///
358
355
/// Passing an invalid string (containing spaces, brackets,
359
356
/// quotes, ...) will return an empty `Err` value.
360
- pub fn new < Name : str :: IntoMaybeOwned < ' a > > ( name : Name ) -> Result < Id < ' a > , ( ) > {
361
- let name = name. into_maybe_owned ( ) ;
357
+ pub fn new < Name : IntoCow < ' a , String , str > > ( name : Name ) -> Result < Id < ' a > , ( ) > {
358
+ let name = name. into_cow ( ) ;
362
359
{
363
- let mut chars = name. as_slice ( ) . chars ( ) ;
360
+ let mut chars = name. chars ( ) ;
364
361
match chars. next ( ) {
365
362
Some ( c) if is_letter_or_underscore ( c) => { ; } ,
366
363
_ => return Err ( ( ) )
@@ -383,10 +380,10 @@ impl<'a> Id<'a> {
383
380
}
384
381
385
382
pub fn as_slice ( & ' a self ) -> & ' a str {
386
- self . name . as_slice ( )
383
+ & * self . name
387
384
}
388
385
389
- pub fn name ( self ) -> str :: MaybeOwned < ' a > {
386
+ pub fn name ( self ) -> CowString < ' a > {
390
387
self . name
391
388
}
392
389
}
@@ -421,7 +418,7 @@ pub trait Labeller<'a,N,E> {
421
418
/// default is in fact the empty string.
422
419
fn edge_label ( & ' a self , e : & E ) -> LabelText < ' a > {
423
420
let _ignored = e;
424
- LabelStr ( str :: Slice ( "" ) )
421
+ LabelStr ( "" . into_cow ( ) )
425
422
}
426
423
}
427
424
@@ -454,11 +451,11 @@ impl<'a> LabelText<'a> {
454
451
/// yields same content as self. The result obeys the law
455
452
/// render(`lt`) == render(`EscStr(lt.pre_escaped_content())`) for
456
453
/// all `lt: LabelText`.
457
- fn pre_escaped_content ( self ) -> str :: MaybeOwned < ' a > {
454
+ fn pre_escaped_content ( self ) -> CowString < ' a > {
458
455
match self {
459
456
EscStr ( s) => s,
460
- LabelStr ( s) => if s. as_slice ( ) . contains_char ( '\\' ) {
461
- str :: Owned ( s . as_slice ( ) . escape_default ( ) )
457
+ LabelStr ( s) => if s. contains_char ( '\\' ) {
458
+ s . escape_default ( ) . into_cow ( )
462
459
} else {
463
460
s
464
461
} ,
@@ -476,12 +473,12 @@ impl<'a> LabelText<'a> {
476
473
let suffix = suffix. pre_escaped_content ( ) ;
477
474
prefix. push_str ( r"\n\n" ) ;
478
475
prefix. push_str ( suffix. as_slice ( ) ) ;
479
- EscStr ( str :: Owned ( prefix) )
476
+ EscStr ( prefix. into_cow ( ) )
480
477
}
481
478
}
482
479
483
- pub type Nodes < ' a , N > = MaybeOwnedVector < ' a , N > ;
484
- pub type Edges < ' a , E > = MaybeOwnedVector < ' a , E > ;
480
+ pub type Nodes < ' a , N > = CowVec < ' a , N > ;
481
+ pub type Edges < ' a , E > = CowVec < ' a , E > ;
485
482
486
483
// (The type parameters in GraphWalk should be associated items,
487
484
// when/if Rust supports such.)
@@ -496,7 +493,7 @@ pub type Edges<'a,E> = MaybeOwnedVector<'a,E>;
496
493
/// that is bound by the self lifetime `'a`.
497
494
///
498
495
/// The `nodes` and `edges` method each return instantiations of
499
- /// `MaybeOwnedVector ` to leave implementers the freedom to create
496
+ /// `CowVec ` to leave implementers the freedom to create
500
497
/// entirely new vectors or to pass back slices into internally owned
501
498
/// vectors.
502
499
pub trait GraphWalk < ' a , N , E > {
@@ -512,7 +509,7 @@ pub trait GraphWalk<'a, N, E> {
512
509
513
510
/// Renders directed graph `g` into the writer `w` in DOT syntax.
514
511
/// (Main entry point for the library.)
515
- pub fn render < ' a , N : ' a , E : ' a , G : Labeller < ' a , N , E > +GraphWalk < ' a , N , E > , W : Writer > (
512
+ pub fn render < ' a , N : Clone + ' a , E : Clone + ' a , G : Labeller < ' a , N , E > +GraphWalk < ' a , N , E > , W : Writer > (
516
513
g : & ' a G ,
517
514
w : & mut W ) -> io:: IoResult < ( ) >
518
515
{
@@ -647,12 +644,12 @@ mod tests {
647
644
}
648
645
fn node_label ( & ' a self , n : & Node ) -> LabelText < ' a > {
649
646
match self . node_labels [ * n] {
650
- Some ( ref l) => LabelStr ( str :: Slice ( l . as_slice ( ) ) ) ,
647
+ Some ( ref l) => LabelStr ( l . into_cow ( ) ) ,
651
648
None => LabelStr ( id_name ( n) . name ( ) ) ,
652
649
}
653
650
}
654
651
fn edge_label ( & ' a self , e : & & ' a Edge ) -> LabelText < ' a > {
655
- LabelStr ( str :: Slice ( e. label . as_slice ( ) ) )
652
+ LabelStr ( e. label . into_cow ( ) )
656
653
}
657
654
}
658
655
0 commit comments