mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
Do not open every source file twice when reading it
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
//! within the `SourceMap`, which upon request can be converted to line and column
|
||||
//! information, source code snippets, etc.
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{self, BorrowedBuf, Read};
|
||||
use std::{fs, path};
|
||||
|
||||
@@ -115,13 +116,18 @@ fn file_exists(&self, path: &Path) -> bool {
|
||||
}
|
||||
|
||||
fn read_file(&self, path: &Path) -> io::Result<String> {
|
||||
if path.metadata().is_ok_and(|metadata| metadata.len() > SourceFile::MAX_FILE_SIZE.into()) {
|
||||
let mut file = File::open(path)?;
|
||||
let size = file.metadata().map(|metadata| metadata.len()).ok().unwrap_or(0);
|
||||
|
||||
if size > SourceFile::MAX_FILE_SIZE.into() {
|
||||
return Err(io::Error::other(format!(
|
||||
"text files larger than {} bytes are unsupported",
|
||||
SourceFile::MAX_FILE_SIZE
|
||||
)));
|
||||
}
|
||||
fs::read_to_string(path)
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)?;
|
||||
Ok(contents)
|
||||
}
|
||||
|
||||
fn read_binary_file(&self, path: &Path) -> io::Result<Arc<[u8]>> {
|
||||
|
||||
Reference in New Issue
Block a user