22 lines
428 B
Rust
22 lines
428 B
Rust
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
|
|
}
|