Use std::fs read/write conveniences

In addition to being more ergonomic these are much more efficient at reading
large files as they preallocate internally. This provides a nice speed boost
locally, reducing the overhead of `wasm-bindgen-test-runner` from 0.23s to
0.19s, yay!
This commit is contained in:
Alex Crichton
2018-07-25 16:06:47 -07:00
parent f3942229fe
commit 0992e45e7f
4 changed files with 17 additions and 36 deletions

View File

@ -4,8 +4,7 @@ extern crate docopt;
extern crate failure;
extern crate wasm_bindgen_cli_support;
use std::fs::File;
use std::io::{Read, Write};
use std::fs;
use std::path::PathBuf;
use std::process;
@ -58,9 +57,7 @@ fn main() {
}
fn rmain(args: &Args) -> Result<(), Error> {
let mut wasm = Vec::new();
File::open(&args.arg_input)
.and_then(|mut f| f.read_to_end(&mut wasm))
let wasm = fs::read(&args.arg_input)
.with_context(|_| format!("failed to read `{}`", args.arg_input.display()))?;
let object = wasm_bindgen_cli_support::wasm2es6js::Config::new()
@ -73,8 +70,7 @@ fn rmain(args: &Args) -> Result<(), Error> {
if let Some(ref p) = args.flag_output {
let dst = p.with_extension("d.ts");
let ts = object.typescript();
File::create(&dst)
.and_then(|mut f| f.write_all(ts.as_bytes()))
fs::write(&dst, ts)
.with_context(|_| format!("failed to write `{}`", dst.display()))?;
}
}
@ -83,8 +79,7 @@ fn rmain(args: &Args) -> Result<(), Error> {
match args.flag_output {
Some(ref p) => {
File::create(p)
.and_then(|mut f| f.write_all(js.as_bytes()))
fs::write(p, js)
.with_context(|_| format!("failed to write `{}`", p.display()))?;
}
None => {