Improve mmap failure error message with file size context

This commit is contained in:
NK
2026-03-21 02:39:33 +01:00
parent e4c943ea4f
commit 044f3b28da
+15 -1
View File
@@ -110,7 +110,21 @@ pub fn mmap_file(file: &std::fs::File) -> std::io::Result<memmap2::Mmap> {
"Empty file or unreadable device", "Empty file or unreadable device",
)); ));
} }
unsafe { memmap2::MmapOptions::new().len(size as usize).map(file) } unsafe {
memmap2::MmapOptions::new()
.len(size as usize)
.map(file)
.map_err(|e| {
std::io::Error::new(
e.kind(),
format!(
"Failed to memory-map file ({:.1} MB): {}",
size as f64 / (1024.0 * 1024.0),
e
),
)
})
}
} }
/// Decode UTF-16LE bytes to a String without intermediate Vec<u16> allocation. /// Decode UTF-16LE bytes to a String without intermediate Vec<u16> allocation.