# HG changeset patch # User Emmanuel Gil Peyrot # Date 1566558348 -7200 # Node ID ccb739c5b66c90d46c8d408e12ac3d32a1c28697 # Parent 2a60f12bd5bd02d85b26177b41b5cd9478d6b3db examples: factorise file reading into a buffer. diff --git a/examples/anmrenderer.rs b/examples/anmrenderer.rs --- a/examples/anmrenderer.rs +++ b/examples/anmrenderer.rs @@ -87,6 +87,14 @@ struct ShaderInterface { mvp: Uniform<[[f32; 4]; 4]>, } +fn load_file_into_vec(filename: &str) -> Vec { + let file = File::open(filename).unwrap(); + let mut file = BufReader::new(file); + let mut buf = vec![]; + file.read_to_end(&mut buf).unwrap(); + buf +} + fn main() { // Parse arguments. let args: Vec<_> = env::args().collect(); @@ -99,10 +107,7 @@ fn main() { let script: u8 = args[3].parse().expect("number"); // Open the ANM file. - let file = File::open(anm_filename).unwrap(); - let mut file = BufReader::new(file); - let mut buf = vec![]; - file.read_to_end(&mut buf).unwrap(); + let buf = load_file_into_vec(anm_filename); let anm0 = Anm0::from_slice(&buf).unwrap(); if !anm0.scripts.contains_key(&script) { diff --git a/examples/eclrenderer.rs b/examples/eclrenderer.rs --- a/examples/eclrenderer.rs +++ b/examples/eclrenderer.rs @@ -88,6 +88,14 @@ struct ShaderInterface { mvp: Uniform<[[f32; 4]; 4]>, } +fn load_file_into_vec(filename: &str) -> Vec { + let file = File::open(filename).unwrap(); + let mut file = BufReader::new(file); + let mut buf = vec![]; + file.read_to_end(&mut buf).unwrap(); + buf +} + fn main() { // Parse arguments. let args: Vec<_> = env::args().collect(); @@ -102,17 +110,11 @@ fn main() { let sub: u16 = args[5].parse().expect("number"); // Open the ECL file. - let file = File::open(ecl_filename).unwrap(); - let mut file = BufReader::new(file); - let mut buf = vec![]; - file.read_to_end(&mut buf).unwrap(); + let buf = load_file_into_vec(ecl_filename); let (_, ecl) = Ecl::from_slice(&buf).unwrap(); // Open the ANM file. - let file = File::open(anm_filename).unwrap(); - let mut file = BufReader::new(file); - let mut buf = vec![]; - file.read_to_end(&mut buf).unwrap(); + let buf = load_file_into_vec(anm_filename); let anm0 = Anm0::from_slice(&buf).unwrap(); let anm0 = Rc::new(RefCell::new(anm0)); diff --git a/examples/stdrenderer.rs b/examples/stdrenderer.rs --- a/examples/stdrenderer.rs +++ b/examples/stdrenderer.rs @@ -107,6 +107,14 @@ struct ShaderInterface { fog_color: Uniform<[f32; 4]>, } +fn load_file_into_vec(filename: &str) -> Vec { + let file = File::open(filename).unwrap(); + let mut file = BufReader::new(file); + let mut buf = vec![]; + file.read_to_end(&mut buf).unwrap(); + buf +} + fn main() { // Parse arguments. let args: Vec<_> = env::args().collect(); @@ -119,17 +127,11 @@ fn main() { let png_filename = &args[3]; // Open the STD file. - let file = File::open(std_filename).unwrap(); - let mut file = BufReader::new(file); - let mut buf = vec![]; - file.read_to_end(&mut buf).unwrap(); + let buf = load_file_into_vec(std_filename); let (_, stage) = Stage::from_slice(&buf).unwrap(); // Open the ANM file. - let file = File::open(anm_filename).unwrap(); - let mut file = BufReader::new(file); - let mut buf = vec![]; - file.read_to_end(&mut buf).unwrap(); + let buf = load_file_into_vec(anm_filename); let anm0 = Anm0::from_slice(&buf).unwrap(); // TODO: seed this PRNG with a valid seed.