2021-32-bit-holiday-jam/src/file.rs

22 lines
428 B
Rust
Raw Normal View History

2020-02-16 23:21:32 +00:00
use std::convert::TryInto;
use std::fs::File;
use std::io::Read;
use std::path::Path;
pub fn load_small_file <P> (name: P, max_size: u64) -> Vec <u8>
where P: AsRef <Path>
{
let mut f = File::open (name).unwrap ();
let len = f.metadata ().unwrap ().len ();
if len > max_size {
panic! ("File is too big");
}
let mut data = vec! [0u8; len.try_into ().unwrap ()];
f.read_exact (&mut data [..]).unwrap ();
data
}