@@ -10,6 +10,7 @@ use glob_match::glob_match;
1010use paths:: Path ;
1111use rayon:: prelude:: * ;
1212use scanner:: allowed_paths:: read_dir;
13+ use std:: borrow:: Cow ;
1314use std:: fs;
1415use std:: path:: PathBuf ;
1516use std:: sync;
@@ -40,9 +41,9 @@ fn init_tracing() {
4041}
4142
4243#[ 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 > > ,
4647}
4748
4849#[ derive( Debug , Clone ) ]
@@ -111,7 +112,7 @@ impl Scanner {
111112 }
112113
113114 #[ 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 > {
115116 self . prepare ( ) ;
116117 let candidates = parse_all_blobs ( read_all_files ( changed_content) ) ;
117118
@@ -128,13 +129,13 @@ impl Scanner {
128129 }
129130
130131 #[ tracing:: instrument( skip_all) ]
131- pub fn get_candidates_with_positions (
132+ pub fn get_candidates_with_positions < ' a > (
132133 & mut self ,
133- changed_content : ChangedContent ,
134+ changed_content : ChangedContent < ' a > ,
134135 ) -> Vec < ( String , usize ) > {
135136 self . prepare ( ) ;
136137
137- let content = read_changed_content ( changed_content) . unwrap_or_default ( ) ;
138+ let content = read_changed_content ( & changed_content) . unwrap_or_default ( ) ;
138139 let extractor = Extractor :: with_positions ( & content[ ..] , Default :: default ( ) ) ;
139140
140141 let candidates: Vec < ( String , usize ) > = extractor
@@ -198,14 +199,14 @@ impl Scanner {
198199
199200 if should_scan_file {
200201 changed_content. push ( ChangedContent {
201- file : Some ( path . clone ( ) ) ,
202+ file : Some ( Cow :: Borrowed ( path ) ) ,
202203 content : None ,
203204 } ) ;
204205 }
205206 }
206207
207208 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 ( ) ) ) ;
209210 self . candidates . par_extend ( candidates) ;
210211 }
211212 }
@@ -426,16 +427,16 @@ impl Scanner {
426427 }
427428}
428429
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 ( ) ) ;
432433 }
433434
434- let Some ( file) = c. file else {
435+ let Some ( file) = & c. file else {
435436 return Default :: default ( ) ;
436437 } ;
437438
438- let Ok ( content) = std:: fs:: read ( & file) . map_err ( |e| {
439+ let Ok ( content) = std:: fs:: read ( file. as_ref ( ) ) . map_err ( |e| {
439440 event ! ( tracing:: Level :: ERROR , "Failed to read file: {:?}" , e) ;
440441 e
441442 } ) else {
@@ -460,7 +461,7 @@ fn read_changed_content(c: ChangedContent) -> Option<Vec<u8>> {
460461}
461462
462463#[ 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 > > {
464465 event ! (
465466 tracing:: Level :: INFO ,
466467 "Reading {:?} file(s)" ,
0 commit comments