提交 9ceaa567 编写于 作者: B bors

Auto merge of #49522 - mbrubeck:fs_read, r=SimonSapin

Rename fs::read_string to read_to_string and stabilize

As approved in https://github.com/rust-lang/rust/issues/46588#issuecomment-377530365

Closes #46588.
......@@ -244,7 +244,8 @@ fn get_resident() -> Option<usize> {
use std::fs;
let field = 1;
let contents = fs::read_string("/proc/self/statm").ok()?;
let contents = fs::read("/proc/self/statm").ok()?;
let contents = String::from_utf8(contents).ok()?;
let s = contents.split_whitespace().nth(field)?;
let npages = s.parse::<usize>().ok()?;
Some(npages * 4096)
......
......@@ -1067,7 +1067,7 @@ fn emit_source(&mut self, filename: &FileName) -> io::Result<()> {
return Ok(());
}
let contents = fs::read_string(&p)?;
let contents = fs::read_to_string(&p)?;
// Remove the utf-8 BOM if any
let contents = if contents.starts_with("\u{feff}") {
......
......@@ -297,12 +297,12 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
/// use std::net::SocketAddr;
///
/// fn main() -> Result<(), Box<std::error::Error + 'static>> {
/// let foo: SocketAddr = fs::read_string("address.txt")?.parse()?;
/// let foo: SocketAddr = fs::read_to_string("address.txt")?.parse()?;
/// Ok(())
/// }
/// ```
#[unstable(feature = "fs_read_write", issue = "46588")]
pub fn read_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
#[stable(feature = "fs_read_write", since = "1.26.0")]
pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
let mut file = File::open(path)?;
let mut string = String::with_capacity(initial_buffer_size(&file));
file.read_to_string(&mut string)?;
......@@ -3122,12 +3122,12 @@ fn write_then_read() {
assert!(v == &bytes[..]);
check!(fs::write(&tmpdir.join("not-utf8"), &[0xFF]));
error_contains!(fs::read_string(&tmpdir.join("not-utf8")),
error_contains!(fs::read_to_string(&tmpdir.join("not-utf8")),
"stream did not contain valid UTF-8");
let s = "𐁁𐀓𐀠𐀴𐀍";
check!(fs::write(&tmpdir.join("utf8"), s.as_bytes()));
let string = check!(fs::read_string(&tmpdir.join("utf8")));
let string = check!(fs::read_to_string(&tmpdir.join("utf8")));
assert_eq!(string, s);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册