Commit 0587b647 authored by Wohlgemuth, Jason's avatar Wohlgemuth, Jason
Browse files

feat: Make read_file thread safe

parent 81aff23d
Loading
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -853,6 +853,8 @@ where
}
/// Reads the given file and returns its contents as a string.
///
/// This function is thread-safe and can be used with rayon's parallel iterators.
///
/// # Parameters
///
/// * `path` - A `PathBuf` or string slice containing the path to the file to be read.
@@ -861,9 +863,21 @@ where
///
/// A `Result` containing the contents of the file as a string if the file is readable, or an
/// `std::io::Error` otherwise.
///
/// # Example with rayon
///
/// ```ignore
/// use rayon::prelude::*;
///
/// let paths = vec![PathBuf::from("file1.txt"), PathBuf::from("file2.txt")];
/// let contents: Vec<_> = paths
///     .par_iter()
///     .filter_map(|path| read_file(path).ok())
///     .collect();
/// ```
pub fn read_file<P>(path: P) -> Result<String, io::Error>
where
    P: Into<PathBuf> + Clone,
    P: Into<PathBuf> + Clone + Send,
{
    let path_buf = path.into();
    let filename = path_buf.file_name().unwrap_or_default().to_string_lossy().to_string();
+85 −85
Original line number Diff line number Diff line
@@ -2917,15 +2917,15 @@ FN:808,sync_get
FN:815,sync_post
FN:822,sync_put
FN:829,parent
FN:864,read_file
FN:902,standard_project_folder
FN:929,to_absolute_string
FN:950,write_file
FN:878,read_file
FN:916,standard_project_folder
FN:943,to_absolute_string
FN:964,write_file
FNF:39
FNDA:0,InputOutput::read_markdown
FNDA:0,InputOutput::write_markdown
FNDA:1729382256910270464,<impl FromCommand for SemanticVersion>::from_command
FNDA:1729382256910270464,<impl FromPath for MimeType>::from_path
FNDA:1224979098644774912,<impl FromPath for MimeType>::from_path
FNDA:288230376151711744,<impl ToStrings for Vec<P>>::to_strings
FNDA:0,<impl ToStrings for Vec<P>>::to_absolute_strings
FNDA:5836665117072162818,<impl ToAbsoluteString for PathBuf>::to_absolute_string
@@ -2976,8 +2976,8 @@ DA:169,1152921504606846976
DA:170,0
DA:173,432345564227567616
DA:176,720575940379279360
DA:192,1729382256910270464
DA:196,5188146770730811392
DA:192,1224979098644774912
DA:196,3674937295934324736
DA:200,288230376151711744
DA:201,288230376151711744
DA:202,2233785415175766016
@@ -3206,7 +3206,7 @@ DA:610,432345564227567616
DA:611,432345564227567616
DA:612,864691128455135232
DA:615,432345564227567616
DA:616,7277816997830721536
DA:616,4755801206503243776
DA:618,0
DA:632,0
DA:633,0
@@ -3300,46 +3300,46 @@ DA:845,144115188075855872
DA:846,0
DA:847,72057594037927936
DA:848,72057594037927936
DA:864,1224979098644774912
DA:868,3674937295934324736
DA:869,3674937295934324736
DA:870,2377900603251621888
DA:871,2305843009213693952
DA:872,72057594037927936
DA:873,72057594037927936
DA:874,72057594037927936
DA:877,3458764513820540928
DA:878,2305843009213693952
DA:879,2305843009213693952
DA:880,1152921504606846976
DA:881,0
DA:882,0
DA:883,0
DA:902,72057594037927936
DA:903,144115188075855872
DA:904,0
DA:905,216172782113783808
DA:906,216172782113783808
DA:907,0
DA:910,144115188075855872
DA:911,72057594037927936
DA:912,0
DA:914,144115188075855872
DA:929,6052837899185946624
DA:933,5764607523034234880
DA:934,11673330234144325632
DA:935,432345564227567616
DA:937,12105675798371893248
DA:950,360287970189639680
DA:954,720575940379279360
DA:955,1441151880758558720
DA:956,720575940379279360
DA:957,0
DA:958,0
DA:959,0
DA:962,0
DA:963,0
DA:964,0
DA:878,1224979098644774912
DA:882,3674937295934324736
DA:883,3674937295934324736
DA:884,2377900603251621888
DA:885,2305843009213693952
DA:886,72057594037927936
DA:887,72057594037927936
DA:888,72057594037927936
DA:891,3458764513820540928
DA:892,2305843009213693952
DA:893,2305843009213693952
DA:894,1152921504606846976
DA:895,0
DA:896,0
DA:897,0
DA:916,72057594037927936
DA:917,144115188075855872
DA:918,0
DA:919,216172782113783808
DA:920,216172782113783808
DA:921,0
DA:924,144115188075855872
DA:925,72057594037927936
DA:926,0
DA:928,144115188075855872
DA:943,6052837899185946624
DA:947,5764607523034234880
DA:948,11673330234144325632
DA:949,432345564227567616
DA:951,12105675798371893248
DA:964,360287970189639680
DA:968,720575940379279360
DA:969,1441151880758558720
DA:970,720575940379279360
DA:971,0
DA:972,0
DA:973,0
DA:976,0
DA:977,0
DA:978,0
LF:379
LH:180
end_of_record
@@ -5626,8 +5626,8 @@ FNDA:216172782113783808,Label::fmt_using
FNDA:504403158265495552,<impl From for License>::from
FNDA:144115188075855872,License::from_technology
FNDA:288230376151711744,License::is_open_source
FNDA:1945555039024054272,<impl From for MimeType>::from
FNDA:1513209474796486656,MimeType::file_type
FNDA:1441151880758558720,<impl From for MimeType>::from
FNDA:1008806316530991104,MimeType::file_type
FNDA:0,<impl Default for SemanticVersion>::default
FNDA:1080863910568919040,<impl From for SemanticVersion>::from
FNDA:216172782113783808,<impl StringInterpolation for T>::replace_placeholder_with_string
@@ -5641,7 +5641,7 @@ FNDA:13763000461244235776,crockford_digit
FNDA:72057594037927936,current_date
FNDA:1585267068834414592,detect_json
FNDA:1369094286720630784,detect_xml
FNDA:2810246167479189504,file_extension
FNDA:2305843009213693952,file_extension
FNDA:216172782113783808,find_first
FNDA:0,format_bytes
FNDA:360287970189639680,frontmatter_and_body
@@ -5772,44 +5772,44 @@ DA:557,14411518807585587200
DA:558,288230376151711744
DA:559,432345564227567616
DA:560,72057594037927936
DA:591,1945555039024054272
DA:592,3891110078048108544
DA:593,1945555039024054272
DA:594,3602879701896396800
DA:595,2233785415175766016
DA:596,2738188573441261568
DA:597,1801439850948198400
DA:598,1873497444986126336
DA:599,1873497444986126336
DA:600,936748722493063168
DA:601,792633534417207296
DA:602,1585267068834414592
DA:603,1224979098644774912
DA:604,360287970189639680
DA:605,360287970189639680
DA:606,432345564227567616
DA:607,936748722493063168
DA:608,144115188075855872
DA:591,1441151880758558720
DA:592,2882303761517117440
DA:593,1441151880758558720
DA:594,2594073385365405696
DA:595,1729382256910270464
DA:596,1729382256910270464
DA:597,1152921504606846976
DA:598,1152921504606846976
DA:599,1152921504606846976
DA:600,648518346341351424
DA:601,504403158265495552
DA:602,1008806316530991104
DA:603,720575940379279360
DA:604,288230376151711744
DA:605,288230376151711744
DA:606,360287970189639680
DA:607,720575940379279360
DA:608,72057594037927936
DA:610,144115188075855872
DA:623,1513209474796486656
DA:624,1513209474796486656
DA:623,1008806316530991104
DA:624,1008806316530991104
DA:625,0
DA:626,432345564227567616
DA:627,0
DA:628,216172782113783808
DA:628,72057594037927936
DA:629,0
DA:630,72057594037927936
DA:630,0
DA:631,0
DA:632,0
DA:633,0
DA:634,0
DA:635,432345564227567616
DA:635,216172782113783808
DA:636,0
DA:637,0
DA:638,0
DA:639,72057594037927936
DA:640,0
DA:641,288230376151711744
DA:641,216172782113783808
DA:647,0
DA:648,0
DA:661,1080863910568919040
@@ -5891,15 +5891,15 @@ DA:838,720575940379279360
DA:839,216172782113783808
DA:840,5476377146882523136
DA:844,432345564227567616
DA:858,2810246167479189504
DA:862,8430738502437568512
DA:863,4755801206503243776
DA:864,2810246167479189504
DA:865,7998392938210000896
DA:866,7854277750134145024
DA:867,10088063165309911040
DA:868,4971973988617027584
DA:869,2161727821137838080
DA:858,2305843009213693952
DA:862,6917529027641081856
DA:863,576460752303423488
DA:864,2305843009213693952
DA:865,6485183463413514240
DA:866,6341068275337658368
DA:867,8070450532247928832
DA:868,3963167672086036480
DA:869,1657324662872342528
DA:871,504403158265495552
DA:874,144115188075855872
DA:887,216172782113783808
@@ -5978,7 +5978,7 @@ DA:1091,0
DA:1096,7061644215716937728
DA:1097,288230376151711744
LF:324
LH:201
LH:200
end_of_record
TN:
SF:C:\Users\jason\dev\acorn\acorn-py\target\release\build\glutin_wgl_sys-7bf65d050f60911f\out\wgl_extra_bindings.rs