diff options
| author | Andy Russell <arussell123@gmail.com> | 2018-11-16 16:22:06 -0500 |
|---|---|---|
| committer | Andy Russell <arussell123@gmail.com> | 2018-12-07 12:54:11 -0500 |
| commit | 2f6226518bd5085896a0f27cfd3ea396367ecd50 (patch) | |
| tree | 713f018b5a2028f682ac9fcf84688022e5f6acfe /src/librustc_codegen_utils/codegen_backend.rs | |
| parent | 1c3236afc3560b5781acf942a4746aeb11e5374e (diff) | |
| download | rust-2f6226518bd5085896a0f27cfd3ea396367ecd50.tar.gz rust-2f6226518bd5085896a0f27cfd3ea396367ecd50.zip | |
use top level `fs` functions where appropriate
This commit replaces many usages of `File::open` and reading or writing with `fs::read_to_string`, `fs::read` and `fs::write`. This reduces code complexity, and will improve performance for most reads, since the functions allocate the buffer to be the size of the file. I believe that this commit will not impact behavior in any way, so some matches will check the error kind in case the file was not valid UTF-8. Some of these cases may not actually care about the error.
Diffstat (limited to 'src/librustc_codegen_utils/codegen_backend.rs')
| -rw-r--r-- | src/librustc_codegen_utils/codegen_backend.rs | 13 |
1 files changed, 4 insertions, 9 deletions
diff --git a/src/librustc_codegen_utils/codegen_backend.rs b/src/librustc_codegen_utils/codegen_backend.rs index b328873800e..74718460f56 100644 --- a/src/librustc_codegen_utils/codegen_backend.rs +++ b/src/librustc_codegen_utils/codegen_backend.rs @@ -22,8 +22,8 @@ #![feature(box_syntax)] use std::any::Any; -use std::io::{self, Write}; -use std::fs::File; +use std::io::Write; +use std::fs; use std::path::Path; use std::sync::{mpsc, Arc}; @@ -81,11 +81,7 @@ pub struct NoLlvmMetadataLoader; impl MetadataLoader for NoLlvmMetadataLoader { fn get_rlib_metadata(&self, _: &Target, filename: &Path) -> Result<MetadataRef, String> { - let mut file = File::open(filename) - .map_err(|e| format!("metadata file open err: {:?}", e))?; - - let mut buf = Vec::new(); - io::copy(&mut file, &mut buf).unwrap(); + let buf = fs::read(filename).map_err(|e| format!("metadata file open err: {:?}", e))?; let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf); Ok(rustc_erase_owner!(buf.map_owner_box())) } @@ -209,8 +205,7 @@ impl CodegenBackend for MetadataOnlyCodegenBackend { } else { &ongoing_codegen.metadata.raw_data }; - let mut file = File::create(&output_name).unwrap(); - file.write_all(metadata).unwrap(); + fs::write(&output_name, metadata).unwrap(); } sess.abort_if_errors(); |
