@@ -10,6 +10,7 @@ use glob_match::glob_match;
10
10
use paths:: Path ;
11
11
use rayon:: prelude:: * ;
12
12
use scanner:: allowed_paths:: read_dir;
13
+ use std:: borrow:: Cow ;
13
14
use std:: fs;
14
15
use std:: path:: PathBuf ;
15
16
use std:: sync;
@@ -40,9 +41,9 @@ fn init_tracing() {
40
41
}
41
42
42
43
#[ derive( Debug , Clone ) ]
43
- pub struct ChangedContent {
44
- pub file : Option < PathBuf > ,
45
- pub content : Option < String > ,
44
+ pub struct ChangedContent < ' a > {
45
+ pub file : Option < Cow < ' a , PathBuf > > ,
46
+ pub content : Option < Cow < ' a , str > > ,
46
47
}
47
48
48
49
#[ derive( Debug , Clone ) ]
@@ -111,7 +112,7 @@ impl Scanner {
111
112
}
112
113
113
114
#[ tracing:: instrument( skip_all) ]
114
- pub fn scan_content ( & mut self , changed_content : Vec < ChangedContent > ) -> Vec < String > {
115
+ pub fn scan_content < ' a > ( & mut self , changed_content : & [ ChangedContent < ' a > ] ) -> Vec < String > {
115
116
self . prepare ( ) ;
116
117
let candidates = parse_all_blobs ( read_all_files ( changed_content) ) ;
117
118
@@ -128,13 +129,13 @@ impl Scanner {
128
129
}
129
130
130
131
#[ tracing:: instrument( skip_all) ]
131
- pub fn get_candidates_with_positions (
132
+ pub fn get_candidates_with_positions < ' a > (
132
133
& mut self ,
133
- changed_content : ChangedContent ,
134
+ changed_content : ChangedContent < ' a > ,
134
135
) -> Vec < ( String , usize ) > {
135
136
self . prepare ( ) ;
136
137
137
- let content = read_changed_content ( changed_content) . unwrap_or_default ( ) ;
138
+ let content = read_changed_content ( & changed_content) . unwrap_or_default ( ) ;
138
139
let extractor = Extractor :: with_positions ( & content[ ..] , Default :: default ( ) ) ;
139
140
140
141
let candidates: Vec < ( String , usize ) > = extractor
@@ -198,14 +199,14 @@ impl Scanner {
198
199
199
200
if should_scan_file {
200
201
changed_content. push ( ChangedContent {
201
- file : Some ( path . clone ( ) ) ,
202
+ file : Some ( Cow :: Borrowed ( path ) ) ,
202
203
content : None ,
203
204
} ) ;
204
205
}
205
206
}
206
207
207
208
if !changed_content. is_empty ( ) {
208
- let candidates = parse_all_blobs ( read_all_files ( changed_content) ) ;
209
+ let candidates = parse_all_blobs ( read_all_files ( changed_content. as_slice ( ) ) ) ;
209
210
self . candidates . par_extend ( candidates) ;
210
211
}
211
212
}
@@ -426,16 +427,16 @@ impl Scanner {
426
427
}
427
428
}
428
429
429
- fn read_changed_content ( c : ChangedContent ) -> Option < Vec < u8 > > {
430
- if let Some ( content) = c. content {
431
- return Some ( content. into_bytes ( ) ) ;
430
+ fn read_changed_content < ' a > ( c : & ChangedContent < ' a > ) -> Option < Vec < u8 > > {
431
+ if let Some ( content) = & c. content {
432
+ return Some ( content. as_bytes ( ) . to_vec ( ) ) ;
432
433
}
433
434
434
- let Some ( file) = c. file else {
435
+ let Some ( file) = & c. file else {
435
436
return Default :: default ( ) ;
436
437
} ;
437
438
438
- let Ok ( content) = std:: fs:: read ( & file) . map_err ( |e| {
439
+ let Ok ( content) = std:: fs:: read ( file. as_ref ( ) ) . map_err ( |e| {
439
440
event ! ( tracing:: Level :: ERROR , "Failed to read file: {:?}" , e) ;
440
441
e
441
442
} ) else {
@@ -460,7 +461,7 @@ fn read_changed_content(c: ChangedContent) -> Option<Vec<u8>> {
460
461
}
461
462
462
463
#[ tracing:: instrument( skip_all) ]
463
- fn read_all_files ( changed_content : Vec < ChangedContent > ) -> Vec < Vec < u8 > > {
464
+ fn read_all_files < ' a > ( changed_content : & [ ChangedContent < ' a > ] ) -> Vec < Vec < u8 > > {
464
465
event ! (
465
466
tracing:: Level :: INFO ,
466
467
"Reading {:?} file(s)" ,
0 commit comments