|
1 | 1 | use std::env; |
2 | 2 | use std::ffi::{OsStr, OsString}; |
3 | | -use std::fs::{self, File, OpenOptions}; |
| 3 | +use std::fs::{self, OpenOptions}; |
4 | 4 | use std::io; |
5 | 5 | use std::io::prelude::*; |
6 | 6 | use std::iter; |
@@ -118,27 +118,12 @@ pub fn read(path: &Path) -> CargoResult<String> { |
118 | 118 | } |
119 | 119 |
|
120 | 120 | pub fn read_bytes(path: &Path) -> CargoResult<Vec<u8>> { |
121 | | - let res = (|| -> CargoResult<_> { |
122 | | - let mut ret = Vec::new(); |
123 | | - let mut f = File::open(path)?; |
124 | | - if let Ok(m) = f.metadata() { |
125 | | - ret.reserve(m.len() as usize + 1); |
126 | | - } |
127 | | - f.read_to_end(&mut ret)?; |
128 | | - Ok(ret) |
129 | | - })() |
130 | | - .chain_err(|| format!("failed to read `{}`", path.display()))?; |
131 | | - Ok(res) |
| 121 | + fs::read(path).chain_err(|| format!("failed to read `{}`", path.display())) |
132 | 122 | } |
133 | 123 |
|
134 | | -pub fn write(path: &Path, contents: &[u8]) -> CargoResult<()> { |
135 | | - (|| -> CargoResult<()> { |
136 | | - let mut f = File::create(path)?; |
137 | | - f.write_all(contents)?; |
138 | | - Ok(()) |
139 | | - })() |
140 | | - .chain_err(|| format!("failed to write `{}`", path.display()))?; |
141 | | - Ok(()) |
| 124 | +pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> CargoResult<()> { |
| 125 | + let path = path.as_ref(); |
| 126 | + fs::write(path, contents.as_ref()).chain_err(|| format!("failed to write `{}`", path.display())) |
142 | 127 | } |
143 | 128 |
|
144 | 129 | pub fn write_if_changed<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> CargoResult<()> { |
@@ -190,7 +175,7 @@ pub fn set_invocation_time(path: &Path) -> CargoResult<FileTime> { |
190 | 175 | let timestamp = path.join("invoked.timestamp"); |
191 | 176 | write( |
192 | 177 | ×tamp, |
193 | | - b"This file has an mtime of when this was started.", |
| 178 | + "This file has an mtime of when this was started.", |
194 | 179 | )?; |
195 | 180 | let ft = mtime(×tamp)?; |
196 | 181 | log::debug!("invocation time for {:?} is {}", path, ft); |
|
0 commit comments