Skip to content

Commit c670424

Browse files
committed
Move File::{read,write}_contents to fs::{read,write} free functions.
1 parent fd518ac commit c670424

File tree

2 files changed

+72
-71
lines changed

2 files changed

+72
-71
lines changed

src/libstd/fs.rs

Lines changed: 71 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,74 @@ pub struct DirBuilder {
211211
recursive: bool,
212212
}
213213

214+
/// Read the entire contents of a file into a bytes vector.
215+
///
216+
/// This is a convenience function for using [`File::open`] and [`read_to_end`]
217+
/// with fewer imports and without an intermediate variable.
218+
///
219+
/// [`File::open`]: struct.File.html#method.open
220+
/// [`read_to_end`]: ../io/trait.Read.html#method.read_to_end
221+
///
222+
/// # Errors
223+
///
224+
/// This function will return an error if `path` does not already exist.
225+
/// Other errors may also be returned according to [`OpenOptions::open`].
226+
///
227+
/// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
228+
///
229+
/// It will also return an error if it encounters while reading an error
230+
/// of a kind other than [`ErrorKind::Interrupted`].
231+
///
232+
/// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
233+
///
234+
/// # Examples
235+
///
236+
/// ```no_run
237+
/// #![feature(fs_read_write)]
238+
///
239+
/// use std::fs;
240+
/// use std::net::SocketAddr;
241+
///
242+
/// # fn foo() -> Result<(), Box<std::error::Error + 'static>> {
243+
/// let foo: SocketAddr = String::from_utf8_lossy(&fs::read("address.txt")?).parse()?;
244+
/// # Ok(())
245+
/// # }
246+
/// ```
247+
#[unstable(feature = "fs_read_write", issue = /* FIXME */ "0")]
248+
pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
249+
let mut bytes = Vec::new();
250+
File::open(path)?.read_to_end(&mut bytes)?;
251+
Ok(bytes)
252+
}
253+
254+
/// Write a slice as the entire contents of a file.
255+
///
256+
/// This function will create a file if it does not exist,
257+
/// and will entirely replace its contents if it does.
258+
///
259+
/// This is a convenience function for using [`File::create`] and [`write_all`]
260+
/// with fewer imports.
261+
///
262+
/// [`File::create`]: struct.File.html#method.create
263+
/// [`write_all`]: ../io/trait.Write.html#method.write_all
264+
///
265+
/// # Examples
266+
///
267+
/// ```no_run
268+
/// #![feature(fs_read_write)]
269+
///
270+
/// use std::fs;
271+
///
272+
/// # fn foo() -> std::io::Result<()> {
273+
/// fs::write("foo.txt", b"Lorem ipsum")?;
274+
/// # Ok(())
275+
/// # }
276+
/// ```
277+
#[unstable(feature = "fs_read_write", issue = /* FIXME */ "0")]
278+
pub fn write<P: AsRef<Path>>(path: P, contents: &[u8]) -> io::Result<()> {
279+
File::create(path)?.write_all(contents)
280+
}
281+
214282
impl File {
215283
/// Attempts to open a file in read-only mode.
216284
///
@@ -262,73 +330,6 @@ impl File {
262330
OpenOptions::new().write(true).create(true).truncate(true).open(path.as_ref())
263331
}
264332

265-
/// Read the entire contents of a file into a bytes vector.
266-
///
267-
/// This is a convenience function for using [`File::open`] and [`read_to_end`]
268-
/// with fewer imports and without an intermediate variable.
269-
///
270-
/// [`File::open`]: struct.File.html#method.open
271-
/// [`read_to_end`]: ../io/trait.Read.html#method.read_to_end
272-
///
273-
/// # Errors
274-
///
275-
/// This function will return an error if `path` does not already exist.
276-
/// Other errors may also be returned according to [`OpenOptions::open`].
277-
///
278-
/// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
279-
///
280-
/// It will also return an error if it encounters while reading an error
281-
/// of a kind other than [`ErrorKind::Interrupted`].
282-
///
283-
/// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
284-
///
285-
/// # Examples
286-
///
287-
/// ```no_run
288-
/// #![feature(file_read_write_contents)]
289-
///
290-
/// use std::fs::File;
291-
///
292-
/// # fn foo() -> Result<(), Box<std::error::Error + 'static>> {
293-
/// let foo = String::from_utf8(File::read_contents("foo.txt")?)?;
294-
/// # Ok(())
295-
/// # }
296-
/// ```
297-
#[unstable(feature = "file_read_write_contents", issue = /* FIXME */ "0")]
298-
pub fn read_contents<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
299-
let mut bytes = Vec::new();
300-
File::open(path)?.read_to_end(&mut bytes)?;
301-
Ok(bytes)
302-
}
303-
304-
/// Write the give contents to a file.
305-
///
306-
/// This function will create a file if it does not exist,
307-
/// and will entirely replace its contents if it does.
308-
///
309-
/// This is a convenience function for using [`File::create`] and [`write_all`]
310-
/// with fewer imports.
311-
///
312-
/// [`File::create`]: struct.File.html#method.create
313-
/// [`write_all`]: ../io/trait.Write.html#method.write_all
314-
///
315-
/// # Examples
316-
///
317-
/// ```no_run
318-
/// #![feature(file_read_write_contents)]
319-
///
320-
/// use std::fs::File;
321-
///
322-
/// # fn foo() -> std::io::Result<()> {
323-
/// File::write_contents("foo.txt", b"Lorem ipsum")?;
324-
/// # Ok(())
325-
/// # }
326-
/// ```
327-
#[unstable(feature = "file_read_write_contents", issue = /* FIXME */ "0")]
328-
pub fn write_contents<P: AsRef<Path>>(path: P, contents: &[u8]) -> io::Result<()> {
329-
File::create(path)?.write_all(contents)
330-
}
331-
332333
/// Attempts to sync all OS-internal metadata to disk.
333334
///
334335
/// This function will attempt to ensure that all in-core data reaches the
@@ -2989,14 +2990,14 @@ mod tests {
29892990
}
29902991

29912992
#[test]
2992-
fn write_contents_then_read_contents() {
2993+
fn write_then_read() {
29932994
let mut bytes = [0; 1024];
29942995
StdRng::new().unwrap().fill_bytes(&mut bytes);
29952996

29962997
let tmpdir = tmpdir();
29972998

2998-
check!(File::write_contents(&tmpdir.join("test"), &bytes));
2999-
let v = check!(File::read_contents(&tmpdir.join("test")));
2999+
check!(fs::write(&tmpdir.join("test"), &bytes));
3000+
let v = check!(fs::read(&tmpdir.join("test")));
30003001
assert!(v == &bytes[..]);
30013002
}
30023003

src/libstd/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
#![feature(core_intrinsics)]
270270
#![feature(dropck_eyepatch)]
271271
#![feature(exact_size_is_empty)]
272-
#![feature(file_read_write_contents)]
272+
#![feature(fs_read_write)]
273273
#![feature(fixed_size_array)]
274274
#![feature(float_from_str_radix)]
275275
#![feature(fn_traits)]

0 commit comments

Comments
 (0)