rust/hg-core/src/utils/files.rs
changeset 46187 95d6f31e88db
parent 45436 1b3197047f5c
child 46481 0d734c0ae1cf
equal deleted inserted replaced
46186:5f27924a201d 46187:95d6f31e88db
    16 };
    16 };
    17 use lazy_static::lazy_static;
    17 use lazy_static::lazy_static;
    18 use same_file::is_same_file;
    18 use same_file::is_same_file;
    19 use std::borrow::{Cow, ToOwned};
    19 use std::borrow::{Cow, ToOwned};
    20 use std::fs::Metadata;
    20 use std::fs::Metadata;
       
    21 use std::io::Read;
    21 use std::iter::FusedIterator;
    22 use std::iter::FusedIterator;
    22 use std::ops::Deref;
    23 use std::ops::Deref;
    23 use std::path::{Path, PathBuf};
    24 use std::path::{Path, PathBuf};
    24 
    25 
    25 pub fn get_path_from_bytes(bytes: &[u8]) -> &Path {
    26 pub fn get_path_from_bytes(bytes: &[u8]) -> &Path {
   306         }
   307         }
   307         Cow::Owned(res)
   308         Cow::Owned(res)
   308     }
   309     }
   309 }
   310 }
   310 
   311 
       
   312 /// Reads a file in one big chunk instead of doing multiple reads
       
   313 pub fn read_whole_file(filepath: &Path) -> std::io::Result<Vec<u8>> {
       
   314     let mut file = std::fs::File::open(filepath)?;
       
   315     let size = file.metadata()?.len();
       
   316 
       
   317     let mut res = vec![0; size as usize];
       
   318     file.read_exact(&mut res)?;
       
   319 
       
   320     Ok(res)
       
   321 }
       
   322 
   311 #[cfg(test)]
   323 #[cfg(test)]
   312 mod tests {
   324 mod tests {
   313     use super::*;
   325     use super::*;
   314     use pretty_assertions::assert_eq;
   326     use pretty_assertions::assert_eq;
   315 
   327