提交 7ffdabd4 编写于 作者: K kennytm 提交者: GitHub

Rollup merge of #47328 - mbrubeck:fs_read, r=sfackler

Use the new fs_read_write functions in rustc internals

Uses `fs::read` and `fs::write` (added by #45837) where appropriate, to simplify code and dog-food these new APIs.  This also improves performance, when combined with #47324.
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
#![feature(drain_filter)] #![feature(drain_filter)]
#![feature(dyn_trait)] #![feature(dyn_trait)]
#![feature(from_ref)] #![feature(from_ref)]
#![feature(fs_read_write)]
#![feature(i128)] #![feature(i128)]
#![feature(i128_type)] #![feature(i128_type)]
#![feature(inclusive_range)] #![feature(inclusive_range)]
......
...@@ -218,13 +218,10 @@ pub fn record_time<T, F>(accu: &Cell<Duration>, f: F) -> T where ...@@ -218,13 +218,10 @@ pub fn record_time<T, F>(accu: &Cell<Duration>, f: F) -> T where
// Memory reporting // Memory reporting
#[cfg(unix)] #[cfg(unix)]
fn get_resident() -> Option<usize> { fn get_resident() -> Option<usize> {
use std::fs::File; use std::fs;
use std::io::Read;
let field = 1; let field = 1;
let mut f = File::open("/proc/self/statm").ok()?; let contents = fs::read_string("/proc/self/statm").ok()?;
let mut contents = String::new();
f.read_to_string(&mut contents).ok()?;
let s = contents.split_whitespace().nth(field)?; let s = contents.split_whitespace().nth(field)?;
let npages = s.parse::<usize>().ok()?; let npages = s.parse::<usize>().ok()?;
Some(npages * 4096) Some(npages * 4096)
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(const_fn)] #![feature(const_fn)]
#![feature(fs_read_write)]
extern crate syntax; extern crate syntax;
extern crate rand; extern crate rand;
......
...@@ -47,7 +47,6 @@ ...@@ -47,7 +47,6 @@
use serialize::json::{Json, ToJson}; use serialize::json::{Json, ToJson};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::default::Default; use std::default::Default;
use std::io::prelude::*;
use syntax::abi::{Abi, lookup as lookup_abi}; use syntax::abi::{Abi, lookup as lookup_abi};
use {LinkerFlavor, PanicStrategy, RelroLevel}; use {LinkerFlavor, PanicStrategy, RelroLevel};
...@@ -811,14 +810,12 @@ pub fn from_json(obj: Json) -> TargetResult { ...@@ -811,14 +810,12 @@ pub fn from_json(obj: Json) -> TargetResult {
pub fn search(target: &str) -> Result<Target, String> { pub fn search(target: &str) -> Result<Target, String> {
use std::env; use std::env;
use std::ffi::OsString; use std::ffi::OsString;
use std::fs::File; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use serialize::json; use serialize::json;
fn load_file(path: &Path) -> Result<Target, String> { fn load_file(path: &Path) -> Result<Target, String> {
let mut f = File::open(path).map_err(|e| e.to_string())?; let contents = fs::read(path).map_err(|e| e.to_string())?;
let mut contents = Vec::new();
f.read_to_end(&mut contents).map_err(|e| e.to_string())?;
let obj = json::from_reader(&mut &contents[..]) let obj = json::from_reader(&mut &contents[..])
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
Target::from_json(obj) Target::from_json(obj)
......
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
use rustc::ich::{ATTR_IF_THIS_CHANGED, ATTR_THEN_THIS_WOULD_NEED}; use rustc::ich::{ATTR_IF_THIS_CHANGED, ATTR_THEN_THIS_WOULD_NEED};
use graphviz::IntoCow; use graphviz::IntoCow;
use std::env; use std::env;
use std::fs::File; use std::fs::{self, File};
use std::io::Write; use std::io::Write;
use syntax::ast; use syntax::ast;
use syntax_pos::Span; use syntax_pos::Span;
...@@ -260,7 +260,7 @@ fn dump_graph(tcx: TyCtxt) { ...@@ -260,7 +260,7 @@ fn dump_graph(tcx: TyCtxt) {
let dot_path = format!("{}.dot", path); let dot_path = format!("{}.dot", path);
let mut v = Vec::new(); let mut v = Vec::new();
dot::render(&GraphvizDepGraph(nodes, edges), &mut v).unwrap(); dot::render(&GraphvizDepGraph(nodes, edges), &mut v).unwrap();
File::create(&dot_path).and_then(|mut f| f.write_all(&v)).unwrap(); fs::write(dot_path, v).unwrap();
} }
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#![deny(warnings)] #![deny(warnings)]
#![feature(conservative_impl_trait)] #![feature(conservative_impl_trait)]
#![feature(fs_read_write)]
#![feature(i128_type)] #![feature(i128_type)]
#![feature(inclusive_range_syntax)] #![feature(inclusive_range_syntax)]
#![feature(specialization)] #![feature(specialization)]
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
use std::io::{self, Read}; use std::io::{self, Read};
use std::path::Path; use std::path::Path;
use std::fs::File; use std::fs;
use std::env; use std::env;
use rustc::session::config::nightly_options; use rustc::session::config::nightly_options;
...@@ -66,11 +66,7 @@ pub fn read_file(report_incremental_info: bool, path: &Path) ...@@ -66,11 +66,7 @@ pub fn read_file(report_incremental_info: bool, path: &Path)
return Ok(None); return Ok(None);
} }
let mut file = File::open(path)?; let data = fs::read(path)?;
let file_size = file.metadata()?.len() as usize;
let mut data = Vec::with_capacity(file_size);
file.read_to_end(&mut data)?;
let mut file = io::Cursor::new(data); let mut file = io::Cursor::new(data);
......
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_serialize::Encodable as RustcEncodable; use rustc_serialize::Encodable as RustcEncodable;
use rustc_serialize::opaque::Encoder; use rustc_serialize::opaque::Encoder;
use std::io::{self, Cursor, Write}; use std::io::{self, Cursor};
use std::fs::{self, File}; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use super::data::*; use super::data::*;
...@@ -125,7 +125,7 @@ fn save_in<F>(sess: &Session, path_buf: PathBuf, encode: F) ...@@ -125,7 +125,7 @@ fn save_in<F>(sess: &Session, path_buf: PathBuf, encode: F)
// write the data out // write the data out
let data = wr.into_inner(); let data = wr.into_inner();
match File::create(&path_buf).and_then(|mut file| file.write_all(&data)) { match fs::write(&path_buf, data) {
Ok(_) => { Ok(_) => {
debug!("save: data written to disk successfully"); debug!("save: data written to disk successfully");
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#![feature(box_patterns)] #![feature(box_patterns)]
#![feature(conservative_impl_trait)] #![feature(conservative_impl_trait)]
#![feature(fs_read_write)]
#![feature(i128_type)] #![feature(i128_type)]
#![feature(libc)] #![feature(libc)]
#![feature(proc_macro_internals)] #![feature(proc_macro_internals)]
......
...@@ -237,7 +237,7 @@ ...@@ -237,7 +237,7 @@
use std::cmp; use std::cmp;
use std::fmt; use std::fmt;
use std::fs::{self, File}; use std::fs;
use std::io::{self, Read}; use std::io::{self, Read};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::Instant; use std::time::Instant;
...@@ -870,10 +870,7 @@ fn get_metadata_section_imp(target: &Target, ...@@ -870,10 +870,7 @@ fn get_metadata_section_imp(target: &Target,
} }
} }
CrateFlavor::Rmeta => { CrateFlavor::Rmeta => {
let mut file = File::open(filename).map_err(|_| let buf = fs::read(filename).map_err(|_|
format!("could not open file: '{}'", filename.display()))?;
let mut buf = vec![];
file.read_to_end(&mut buf).map_err(|_|
format!("failed to read rmeta metadata: '{}'", filename.display()))?; format!("failed to read rmeta metadata: '{}'", filename.display()))?;
OwningRef::new(buf).map_owner_box().erase_owner() OwningRef::new(buf).map_owner_box().erase_owner()
} }
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
use dot; use dot;
use dot::IntoCow; use dot::IntoCow;
use std::fs::File; use std::fs;
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use std::marker::PhantomData; use std::marker::PhantomData;
...@@ -67,7 +67,7 @@ pub(crate) fn print_borrowck_graph_to<'a, 'tcx, BD, P>( ...@@ -67,7 +67,7 @@ pub(crate) fn print_borrowck_graph_to<'a, 'tcx, BD, P>(
dot::render(&g, &mut v)?; dot::render(&g, &mut v)?;
debug!("print_borrowck_graph_to path: {} node_id: {}", debug!("print_borrowck_graph_to path: {} node_id: {}",
path.display(), mbcx.node_id); path.display(), mbcx.node_id);
File::create(path).and_then(|mut f| f.write_all(&v)) fs::write(path, v)
} }
pub type Node = BasicBlock; pub type Node = BasicBlock;
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#![feature(core_intrinsics)] #![feature(core_intrinsics)]
#![feature(decl_macro)] #![feature(decl_macro)]
#![feature(dyn_trait)] #![feature(dyn_trait)]
#![feature(fs_read_write)]
#![feature(i128_type)] #![feature(i128_type)]
#![feature(inclusive_range_syntax)] #![feature(inclusive_range_syntax)]
#![feature(inclusive_range)] #![feature(inclusive_range)]
......
...@@ -342,9 +342,7 @@ fn archive_config<'a>(sess: &'a Session, ...@@ -342,9 +342,7 @@ fn archive_config<'a>(sess: &'a Session,
fn emit_metadata<'a>(sess: &'a Session, trans: &CrateTranslation, tmpdir: &TempDir) fn emit_metadata<'a>(sess: &'a Session, trans: &CrateTranslation, tmpdir: &TempDir)
-> PathBuf { -> PathBuf {
let out_filename = tmpdir.path().join(METADATA_FILENAME); let out_filename = tmpdir.path().join(METADATA_FILENAME);
let result = fs::File::create(&out_filename).and_then(|mut f| { let result = fs::write(&out_filename, &trans.metadata.raw_data);
f.write_all(&trans.metadata.raw_data)
});
if let Err(e) = result { if let Err(e) = result {
sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e)); sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
......
...@@ -46,9 +46,8 @@ ...@@ -46,9 +46,8 @@
use std::any::Any; use std::any::Any;
use std::ffi::{CString, CStr}; use std::ffi::{CString, CStr};
use std::fs::{self, File}; use std::fs;
use std::io; use std::io::{self, Write};
use std::io::{Read, Write};
use std::mem; use std::mem;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str; use std::str;
...@@ -666,7 +665,7 @@ unsafe fn with_codegen<F, R>(tm: TargetMachineRef, ...@@ -666,7 +665,7 @@ unsafe fn with_codegen<F, R>(tm: TargetMachineRef,
timeline.record("make-bc"); timeline.record("make-bc");
if write_bc { if write_bc {
if let Err(e) = File::create(&bc_out).and_then(|mut f| f.write_all(data)) { if let Err(e) = fs::write(&bc_out, data) {
diag_handler.err(&format!("failed to write bytecode: {}", e)); diag_handler.err(&format!("failed to write bytecode: {}", e));
} }
timeline.record("write-bc"); timeline.record("write-bc");
...@@ -675,7 +674,7 @@ unsafe fn with_codegen<F, R>(tm: TargetMachineRef, ...@@ -675,7 +674,7 @@ unsafe fn with_codegen<F, R>(tm: TargetMachineRef,
if config.emit_bc_compressed { if config.emit_bc_compressed {
let dst = bc_out.with_extension(RLIB_BYTECODE_EXTENSION); let dst = bc_out.with_extension(RLIB_BYTECODE_EXTENSION);
let data = bytecode::encode(&mtrans.llmod_id, data); let data = bytecode::encode(&mtrans.llmod_id, data);
if let Err(e) = File::create(&dst).and_then(|mut f| f.write_all(&data)) { if let Err(e) = fs::write(&dst, data) {
diag_handler.err(&format!("failed to write bytecode: {}", e)); diag_handler.err(&format!("failed to write bytecode: {}", e));
} }
timeline.record("compress-bc"); timeline.record("compress-bc");
...@@ -799,9 +798,7 @@ fn binaryen_assemble(cgcx: &CodegenContext, ...@@ -799,9 +798,7 @@ fn binaryen_assemble(cgcx: &CodegenContext,
object: &Path) { object: &Path) {
use rustc_binaryen::{Module, ModuleOptions}; use rustc_binaryen::{Module, ModuleOptions};
let input = File::open(&assembly).and_then(|mut f| { let input = fs::read(&assembly).and_then(|contents| {
let mut contents = Vec::new();
f.read_to_end(&mut contents)?;
Ok(CString::new(contents)?) Ok(CString::new(contents)?)
}); });
let mut options = ModuleOptions::new(); let mut options = ModuleOptions::new();
...@@ -818,7 +815,7 @@ fn binaryen_assemble(cgcx: &CodegenContext, ...@@ -818,7 +815,7 @@ fn binaryen_assemble(cgcx: &CodegenContext,
.map_err(|e| io::Error::new(io::ErrorKind::Other, e)) .map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}); });
let err = assembled.and_then(|binary| { let err = assembled.and_then(|binary| {
File::create(&object).and_then(|mut f| f.write_all(binary.data())) fs::write(&object, binary.data())
}); });
if let Err(e) = err { if let Err(e) = err {
handler.err(&format!("failed to run binaryen assembler: {}", e)); handler.err(&format!("failed to run binaryen assembler: {}", e));
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#![feature(box_patterns)] #![feature(box_patterns)]
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(custom_attribute)] #![feature(custom_attribute)]
#![feature(fs_read_write)]
#![allow(unused_attributes)] #![allow(unused_attributes)]
#![feature(i128_type)] #![feature(i128_type)]
#![feature(i128)] #![feature(i128)]
......
...@@ -8,8 +8,7 @@ ...@@ -8,8 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use std::fs::File; use std::fs;
use std::io::prelude::*;
use std::path::Path; use std::path::Path;
use std::str; use std::str;
use html::markdown::{Markdown, RenderType}; use html::markdown::{Markdown, RenderType};
...@@ -65,13 +64,13 @@ pub enum LoadStringError { ...@@ -65,13 +64,13 @@ pub enum LoadStringError {
pub fn load_string<P: AsRef<Path>>(file_path: P) -> Result<String, LoadStringError> { pub fn load_string<P: AsRef<Path>>(file_path: P) -> Result<String, LoadStringError> {
let file_path = file_path.as_ref(); let file_path = file_path.as_ref();
let mut contents = vec![]; let contents = match fs::read(file_path) {
let result = File::open(file_path) Ok(bytes) => bytes,
.and_then(|mut f| f.read_to_end(&mut contents)); Err(e) => {
if let Err(e) = result {
eprintln!("error reading `{}`: {}", file_path.display(), e); eprintln!("error reading `{}`: {}", file_path.display(), e);
return Err(LoadStringError::ReadFail); return Err(LoadStringError::ReadFail);
} }
};
match str::from_utf8(&contents) { match str::from_utf8(&contents) {
Ok(s) => Ok(s.to_string()), Ok(s) => Ok(s.to_string()),
Err(_) => { Err(_) => {
......
...@@ -866,15 +866,8 @@ fn write_shared(cx: &Context, ...@@ -866,15 +866,8 @@ fn write_shared(cx: &Context,
write(cx.dst.join("main.css"), write(cx.dst.join("main.css"),
include_bytes!("static/styles/main.css"))?; include_bytes!("static/styles/main.css"))?;
if let Some(ref css) = cx.shared.css_file_extension { if let Some(ref css) = cx.shared.css_file_extension {
let mut content = String::new(); let out = cx.dst.join("theme.css");
let css = css.as_path(); try_err!(fs::copy(css, out), css);
let mut f = try_err!(File::open(css), css);
try_err!(f.read_to_string(&mut content), css);
let css = cx.dst.join("theme.css");
let css = css.as_path();
let mut f = try_err!(File::create(css), css);
try_err!(write!(f, "{}", &content), css);
} }
write(cx.dst.join("normalize.css"), write(cx.dst.join("normalize.css"),
include_bytes!("static/normalize.css"))?; include_bytes!("static/normalize.css"))?;
...@@ -1027,7 +1020,7 @@ fn render_sources(dst: &Path, scx: &mut SharedContext, ...@@ -1027,7 +1020,7 @@ fn render_sources(dst: &Path, scx: &mut SharedContext,
/// Writes the entire contents of a string to a destination, not attempting to /// Writes the entire contents of a string to a destination, not attempting to
/// catch any errors. /// catch any errors.
fn write(dst: PathBuf, contents: &[u8]) -> Result<(), Error> { fn write(dst: PathBuf, contents: &[u8]) -> Result<(), Error> {
Ok(try_err!(try_err!(File::create(&dst), &dst).write_all(contents), &dst)) Ok(try_err!(fs::write(&dst, contents), &dst))
} }
/// Takes a path to a source file and cleans the path to it. This canonicalizes /// Takes a path to a source file and cleans the path to it. This canonicalizes
...@@ -1124,16 +1117,13 @@ fn emit_source(&mut self, filename: &FileName) -> io::Result<()> { ...@@ -1124,16 +1117,13 @@ fn emit_source(&mut self, filename: &FileName) -> io::Result<()> {
return Ok(()); return Ok(());
} }
let mut contents = Vec::new(); let contents = fs::read_string(&p)?;
File::open(&p).and_then(|mut f| f.read_to_end(&mut contents))?;
let contents = str::from_utf8(&contents).unwrap();
// Remove the utf-8 BOM if any // Remove the utf-8 BOM if any
let contents = if contents.starts_with("\u{feff}") { let contents = if contents.starts_with("\u{feff}") {
&contents[3..] &contents[3..]
} else { } else {
contents &contents[..]
}; };
// Create the intermediate directories // Create the intermediate directories
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#![feature(rustc_private)] #![feature(rustc_private)]
#![feature(box_patterns)] #![feature(box_patterns)]
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(fs_read_write)]
#![feature(libc)] #![feature(libc)]
#![feature(set_stdio)] #![feature(set_stdio)]
#![feature(slice_patterns)] #![feature(slice_patterns)]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册