comparison rust/hg-core/src/utils/files.rs @ 46540:0d734c0ae1cf

rust: replace read_whole_file with std::fs::read It does the same thing Differential Revision: https://phab.mercurial-scm.org/D9959
author Simon Sapin <simon.sapin@octobus.net>
date Mon, 01 Feb 2021 12:25:53 +0100
parents 95d6f31e88db
children d2e61f00ee9d
comparison
equal deleted inserted replaced
46539:05dd091dfa6a 46540:0d734c0ae1cf
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;
22 use std::iter::FusedIterator; 21 use std::iter::FusedIterator;
23 use std::ops::Deref; 22 use std::ops::Deref;
24 use std::path::{Path, PathBuf}; 23 use std::path::{Path, PathBuf};
25 24
26 pub fn get_path_from_bytes(bytes: &[u8]) -> &Path { 25 pub fn get_path_from_bytes(bytes: &[u8]) -> &Path {
307 } 306 }
308 Cow::Owned(res) 307 Cow::Owned(res)
309 } 308 }
310 } 309 }
311 310
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
323 #[cfg(test)] 311 #[cfg(test)]
324 mod tests { 312 mod tests {
325 use super::*; 313 use super::*;
326 use pretty_assertions::assert_eq; 314 use pretty_assertions::assert_eq;
327 315