diff --git a/cranelift/src/utils.rs b/cranelift/src/utils.rs index 6a962d04f7..c9cae4cd58 100644 --- a/cranelift/src/utils.rs +++ b/cranelift/src/utils.rs @@ -20,6 +20,14 @@ pub fn read_to_string>(path: P) -> io::Result { Ok(buffer) } +/// Read an entire file into a vector of bytes. +pub fn read_to_end>(path: P) -> io::Result> { + let mut file = File::open(path)?; + let mut buffer = Vec::new(); + file.read_to_end(&mut buffer)?; + Ok(buffer) +} + /// Look for a directive in a comment string. /// The directive is of the form "foo:" and should follow the leading `;` in the comment: /// diff --git a/cranelift/src/wasm.rs b/cranelift/src/wasm.rs index 460d4280f5..d6bf98110b 100644 --- a/cranelift/src/wasm.rs +++ b/cranelift/src/wasm.rs @@ -16,7 +16,7 @@ use std::path::Path; use std::process::Command; use tempdir::TempDir; use term; -use utils::{pretty_verifier_error, pretty_error, parse_sets_and_isa}; +use utils::{pretty_verifier_error, pretty_error, parse_sets_and_isa, read_to_end}; macro_rules! vprintln { ($x: expr, $($tts:tt)*) => { @@ -34,13 +34,6 @@ macro_rules! vprint { } } -fn read_wasm_file(path: PathBuf) -> Result, io::Error> { - let mut buf: Vec = Vec::new(); - let mut file = File::open(path)?; - file.read_to_end(&mut buf)?; - Ok(buf) -} - pub fn run( files: Vec, flag_verbose: bool, @@ -92,7 +85,7 @@ fn handle_module( Some(ext) => { match ext.to_str() { Some("wasm") => { - read_wasm_file(path.clone()).map_err(|err| { + read_to_end(path.clone()).map_err(|err| { String::from(err.description()) })? } @@ -110,9 +103,9 @@ fn handle_module( } else { return Err(String::from(e.description())); })?; - read_wasm_file(file_path).map_err(|err| { - String::from(err.description()) - })? + read_to_end(file_path).map_err( + |err| String::from(err.description()), + )? } None | Some(&_) => { return Err(String::from("the file extension is not wasm or wat"));