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

@ -13,8 +13,7 @@ extern crate failure;
use std::any::Any;
use std::collections::BTreeSet;
use std::fmt;
use std::fs::File;
use std::io::{Read, Write};
use std::fs;
use std::mem;
use std::path::{Path, PathBuf};
@ -158,9 +157,7 @@ impl Bindgen {
(module, &name[..])
}
Input::Path(ref path) => {
let mut contents = Vec::new();
File::open(&path)
.and_then(|mut f| f.read_to_end(&mut contents))
let contents = fs::read(&path)
.with_context(|_| format!("failed to read `{}`", path.display()))?;
let module = parity_wasm::deserialize_buffer::<Module>(&contents)
.context("failed to parse input file as wasm")?;
@ -232,14 +229,12 @@ impl Bindgen {
let extension = if self.nodejs_experimental_modules { "mjs" } else { "js" };
let js_path = out_dir.join(stem).with_extension(extension);
File::create(&js_path)
.and_then(|mut f| f.write_all(reset_indentation(&js).as_bytes()))
fs::write(&js_path, reset_indentation(&js))
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
if self.typescript {
let ts_path = out_dir.join(stem).with_extension("d.ts");
File::create(&ts_path)
.and_then(|mut f| f.write_all(ts.as_bytes()))
fs::write(&ts_path, ts)
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
}
@ -248,14 +243,12 @@ impl Bindgen {
if self.nodejs {
let js_path = wasm_path.with_extension(extension);
let shim = self.generate_node_wasm_import(&module, &wasm_path);
File::create(&js_path)
.and_then(|mut f| f.write_all(shim.as_bytes()))
fs::write(&js_path, shim)
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
}
let wasm_bytes = parity_wasm::serialize(module)?;
File::create(&wasm_path)
.and_then(|mut f| f.write_all(&wasm_bytes))
fs::write(&wasm_path, wasm_bytes)
.with_context(|_| format!("failed to write `{}`", wasm_path.display()))?;
Ok(())
}

View File

@ -2,8 +2,8 @@ extern crate base64;
extern crate tempfile;
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::{self, Read, Write};
use std::fs;
use std::io;
use std::process::Command;
use failure::{Error, ResultExt};
@ -375,8 +375,7 @@ impl Output {
let td = tempfile::tempdir()?;
let wasm = serialize(self.module)?;
let wasm_file = td.as_ref().join("foo.wasm");
File::create(&wasm_file)
.and_then(|mut f| f.write_all(&wasm))
fs::write(&wasm_file, wasm)
.with_context(|_| format!("failed to write wasm to `{}`", wasm_file.display()))?;
let wast_file = td.as_ref().join("foo.wast");
@ -396,9 +395,7 @@ impl Output {
"wasm2asm",
)?;
let mut asm_func = String::new();
File::open(&js_file)
.and_then(|mut f| f.read_to_string(&mut asm_func))
let asm_func = fs::read_to_string(&js_file)
.with_context(|_| format!("failed to read `{}`", js_file.display()))?;
let mut make_imports = String::from(