@@ -64,21 +64,21 @@ pub struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [(String, String)]);
64
64
/// All lines are used in documentation tests.
65
65
enum Line < ' a > {
66
66
Hidden ( & ' a str ) ,
67
- Shown ( & ' a str ) ,
67
+ Shown ( Cow < ' a , str > ) ,
68
68
}
69
69
70
70
impl < ' a > Line < ' a > {
71
- fn for_html ( self ) -> Option < & ' a str > {
71
+ fn for_html ( self ) -> Option < Cow < ' a , str > > {
72
72
match self {
73
73
Line :: Shown ( l) => Some ( l) ,
74
74
Line :: Hidden ( _) => None ,
75
75
}
76
76
}
77
77
78
- fn for_code ( self ) -> & ' a str {
78
+ fn for_code ( self ) -> Cow < ' a , str > {
79
79
match self {
80
- Line :: Shown ( l) |
81
- Line :: Hidden ( l) => l ,
80
+ Line :: Shown ( l) => l ,
81
+ Line :: Hidden ( l) => Cow :: Borrowed ( l ) ,
82
82
}
83
83
}
84
84
}
@@ -91,15 +91,15 @@ impl<'a> Line<'a> {
91
91
fn map_line ( s : & str ) -> Line {
92
92
let trimmed = s. trim ( ) ;
93
93
if trimmed. starts_with ( "##" ) {
94
- Line :: Shown ( & trimmed [ 1 .. ] )
94
+ Line :: Shown ( Cow :: Owned ( s . replacen ( "##" , "#" , 1 ) ) )
95
95
} else if trimmed. starts_with ( "# " ) {
96
96
// # text
97
97
Line :: Hidden ( & trimmed[ 2 ..] )
98
98
} else if trimmed == "#" {
99
99
// We cannot handle '#text' because it could be #[attr].
100
100
Line :: Hidden ( "" )
101
101
} else {
102
- Line :: Shown ( s )
102
+ Line :: Shown ( Cow :: Borrowed ( s ) )
103
103
}
104
104
}
105
105
@@ -168,7 +168,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
168
168
}
169
169
}
170
170
let lines = origtext. lines ( ) . filter_map ( |l| map_line ( l) . for_html ( ) ) ;
171
- let text = lines. collect :: < Vec < & str > > ( ) . join ( "\n " ) ;
171
+ let text = lines. collect :: < Vec < Cow < str > > > ( ) . join ( "\n " ) ;
172
172
PLAYGROUND . with ( |play| {
173
173
// insert newline to clearly separate it from the
174
174
// previous block so we can shorten the html output
@@ -179,7 +179,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
179
179
}
180
180
let test = origtext. lines ( )
181
181
. map ( |l| map_line ( l) . for_code ( ) )
182
- . collect :: < Vec < & str > > ( ) . join ( "\n " ) ;
182
+ . collect :: < Vec < Cow < str > > > ( ) . join ( "\n " ) ;
183
183
let krate = krate. as_ref ( ) . map ( |s| & * * s) ;
184
184
let ( test, _) = test:: make_test ( & test, krate, false ,
185
185
& Default :: default ( ) ) ;
@@ -477,7 +477,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Sp
477
477
}
478
478
if let Some ( offset) = offset {
479
479
let lines = test_s. lines ( ) . map ( |l| map_line ( l) . for_code ( ) ) ;
480
- let text = lines. collect :: < Vec < & str > > ( ) . join ( "\n " ) ;
480
+ let text = lines. collect :: < Vec < Cow < str > > > ( ) . join ( "\n " ) ;
481
481
nb_lines += doc[ prev_offset..offset] . lines ( ) . count ( ) ;
482
482
let line = tests. get_line ( ) + ( nb_lines - 1 ) ;
483
483
let filename = tests. get_filename ( ) ;
0 commit comments