1- use std:: borrow:: Cow ;
21use std:: fmt;
32use std:: slice;
43use std:: str;
54use std:: sync:: atomic:: AtomicUsize ;
65use std:: sync:: atomic:: Ordering ;
6+ use std:: sync:: Arc ;
77use std:: sync:: Mutex ;
88
99use if_chain:: if_chain;
@@ -14,19 +14,13 @@ use crate::js_identifiers::{get_javascript_token, is_valid_javascript_identifier
1414use crate :: types:: { idx_from_token, sourcemap_from_token, Token } ;
1515
1616/// An iterator that iterates over tokens in reverse.
17- pub struct RevTokenIter < ' view , ' viewbase , ' map >
18- where
19- ' viewbase : ' view ,
20- {
21- sv : & ' view SourceView < ' viewbase > ,
17+ pub struct RevTokenIter < ' view , ' map > {
18+ sv : & ' view SourceView ,
2219 token : Option < Token < ' map > > ,
2320 source_line : Option < ( & ' view str , usize , usize , usize ) > ,
2421}
2522
26- impl < ' view , ' viewbase , ' map > Iterator for RevTokenIter < ' view , ' viewbase , ' map >
27- where
28- ' viewbase : ' view ,
29- {
23+ impl < ' view , ' map > Iterator for RevTokenIter < ' view , ' map > {
3024 type Item = ( Token < ' map > , Option < & ' view str > ) ;
3125
3226 fn next ( & mut self ) -> Option < ( Token < ' map > , Option < & ' view str > ) > {
@@ -118,7 +112,7 @@ where
118112}
119113
120114pub struct Lines < ' a > {
121- sv : & ' a SourceView < ' a > ,
115+ sv : & ' a SourceView ,
122116 idx : u32 ,
123117}
124118
@@ -139,14 +133,14 @@ impl<'a> Iterator for Lines<'a> {
139133///
140134/// This type is used to implement fairly efficient source mapping
141135/// operations.
142- pub struct SourceView < ' a > {
143- source : Cow < ' a , str > ,
136+ pub struct SourceView {
137+ source : Arc < str > ,
144138 processed_until : AtomicUsize ,
145139 lines : Mutex < Vec < & ' static str > > ,
146140}
147141
148- impl < ' a > Clone for SourceView < ' a > {
149- fn clone ( & self ) -> SourceView < ' a > {
142+ impl Clone for SourceView {
143+ fn clone ( & self ) -> SourceView {
150144 SourceView {
151145 source : self . source . clone ( ) ,
152146 processed_until : AtomicUsize :: new ( 0 ) ,
@@ -155,28 +149,28 @@ impl<'a> Clone for SourceView<'a> {
155149 }
156150}
157151
158- impl < ' a > fmt:: Debug for SourceView < ' a > {
152+ impl fmt:: Debug for SourceView {
159153 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
160154 f. debug_struct ( "SourceView" )
161155 . field ( "source" , & self . source ( ) )
162156 . finish ( )
163157 }
164158}
165159
166- impl < ' a > SourceView < ' a > {
160+ impl SourceView {
167161 /// Creates an optimized view of a given source.
168- pub fn new ( source : & ' a str ) -> SourceView < ' a > {
162+ pub fn new ( source : Arc < str > ) -> SourceView {
169163 SourceView {
170- source : Cow :: Borrowed ( source ) ,
164+ source,
171165 processed_until : AtomicUsize :: new ( 0 ) ,
172166 lines : Mutex :: new ( vec ! [ ] ) ,
173167 }
174168 }
175169
176170 /// Creates an optimized view from a given source string
177- pub fn from_string ( source : String ) -> SourceView < ' static > {
171+ pub fn from_string ( source : String ) -> SourceView {
178172 SourceView {
179- source : Cow :: Owned ( source) ,
173+ source : source. into ( ) ,
180174 processed_until : AtomicUsize :: new ( 0 ) ,
181175 lines : Mutex :: new ( vec ! [ ] ) ,
182176 }
@@ -264,7 +258,7 @@ impl<'a> SourceView<'a> {
264258 }
265259
266260 /// Returns an iterator over all lines.
267- pub fn lines ( & ' a self ) -> Lines < ' a > {
261+ pub fn lines ( & self ) -> Lines {
268262 Lines { sv : self , idx : 0 }
269263 }
270264
@@ -273,10 +267,7 @@ impl<'a> SourceView<'a> {
273267 & self . source
274268 }
275269
276- fn rev_token_iter < ' this , ' map > (
277- & ' this self ,
278- token : Token < ' map > ,
279- ) -> RevTokenIter < ' this , ' a , ' map > {
270+ fn rev_token_iter < ' this , ' map > ( & ' this self , token : Token < ' map > ) -> RevTokenIter < ' this , ' map > {
280271 RevTokenIter {
281272 sv : self ,
282273 token : Some ( token) ,
@@ -332,7 +323,7 @@ impl<'a> SourceView<'a> {
332323#[ test]
333324#[ allow( clippy:: cognitive_complexity) ]
334325fn test_minified_source_view ( ) {
335- let view = SourceView :: new ( "a\n b\n c" ) ;
326+ let view = SourceView :: new ( "a\n b\n c" . into ( ) ) ;
336327 assert_eq ! ( view. get_line( 0 ) , Some ( "a" ) ) ;
337328 assert_eq ! ( view. get_line( 0 ) , Some ( "a" ) ) ;
338329 assert_eq ! ( view. get_line( 2 ) , Some ( "c" ) ) ;
@@ -341,7 +332,7 @@ fn test_minified_source_view() {
341332
342333 assert_eq ! ( view. line_count( ) , 3 ) ;
343334
344- let view = SourceView :: new ( "a\r \n b\r \n c" ) ;
335+ let view = SourceView :: new ( "a\r \n b\r \n c" . into ( ) ) ;
345336 assert_eq ! ( view. get_line( 0 ) , Some ( "a" ) ) ;
346337 assert_eq ! ( view. get_line( 0 ) , Some ( "a" ) ) ;
347338 assert_eq ! ( view. get_line( 2 ) , Some ( "c" ) ) ;
@@ -350,7 +341,7 @@ fn test_minified_source_view() {
350341
351342 assert_eq ! ( view. line_count( ) , 3 ) ;
352343
353- let view = SourceView :: new ( "abc👌def\n blah" ) ;
344+ let view = SourceView :: new ( "abc👌def\n blah" . into ( ) ) ;
354345 assert_eq ! ( view. get_line_slice( 0 , 0 , 3 ) , Some ( "abc" ) ) ;
355346 assert_eq ! ( view. get_line_slice( 0 , 3 , 1 ) , Some ( "👌" ) ) ;
356347 assert_eq ! ( view. get_line_slice( 0 , 3 , 2 ) , Some ( "👌" ) ) ;
@@ -362,7 +353,7 @@ fn test_minified_source_view() {
362353 assert_eq ! ( view. get_line_slice( 1 , 0 , 5 ) , None ) ;
363354 assert_eq ! ( view. get_line_slice( 1 , 0 , 12 ) , None ) ;
364355
365- let view = SourceView :: new ( "a\n b\n c\n " ) ;
356+ let view = SourceView :: new ( "a\n b\n c\n " . into ( ) ) ;
366357 assert_eq ! ( view. get_line( 0 ) , Some ( "a" ) ) ;
367358 assert_eq ! ( view. get_line( 1 ) , Some ( "b" ) ) ;
368359 assert_eq ! ( view. get_line( 2 ) , Some ( "c" ) ) ;
@@ -371,6 +362,6 @@ fn test_minified_source_view() {
371362
372363 fn is_send < T : Send > ( ) { }
373364 fn is_sync < T : Sync > ( ) { }
374- is_send :: < SourceView < ' static > > ( ) ;
375- is_sync :: < SourceView < ' static > > ( ) ;
365+ is_send :: < SourceView > ( ) ;
366+ is_sync :: < SourceView > ( ) ;
376367}
0 commit comments