aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/fs.rs
diff options
context:
space:
mode:
authorFenrir <[email protected]>2018-04-14 20:02:05 -0600
committerFenrir <[email protected]>2018-04-21 16:35:01 -0600
commitb330206f5590d88a2f995321d2ea847ded951d1d (patch)
tree4fecd0ca00b754c494e96b13e9837db48de93109 /ctr-std/src/fs.rs
parentMove more implementation details to `imp` module (diff)
downloadarchived-ctru-rs-b330206f5590d88a2f995321d2ea847ded951d1d.tar.xz
archived-ctru-rs-b330206f5590d88a2f995321d2ea847ded951d1d.zip
Update for Rust nightly 2018-04-19
Diffstat (limited to 'ctr-std/src/fs.rs')
-rw-r--r--ctr-std/src/fs.rs620
1 files changed, 346 insertions, 274 deletions
diff --git a/ctr-std/src/fs.rs b/ctr-std/src/fs.rs
index d1f3ccb..7bd1adc 100644
--- a/ctr-std/src/fs.rs
+++ b/ctr-std/src/fs.rs
@@ -41,11 +41,11 @@ use time::SystemTime;
/// use std::fs::File;
/// use std::io::prelude::*;
///
-/// # fn foo() -> std::io::Result<()> {
-/// let mut file = File::create("foo.txt")?;
-/// file.write_all(b"Hello, world!")?;
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// let mut file = File::create("foo.txt")?;
+/// file.write_all(b"Hello, world!")?;
+/// Ok(())
+/// }
/// ```
///
/// Read the contents of a file into a [`String`]:
@@ -54,13 +54,13 @@ use time::SystemTime;
/// use std::fs::File;
/// use std::io::prelude::*;
///
-/// # fn foo() -> std::io::Result<()> {
-/// let mut file = File::open("foo.txt")?;
-/// let mut contents = String::new();
-/// file.read_to_string(&mut contents)?;
-/// assert_eq!(contents, "Hello, world!");
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// let mut file = File::open("foo.txt")?;
+/// let mut contents = String::new();
+/// file.read_to_string(&mut contents)?;
+/// assert_eq!(contents, "Hello, world!");
+/// Ok(())
+/// }
/// ```
///
/// It can be more efficient to read the contents of a file with a buffered
@@ -71,19 +71,28 @@ use time::SystemTime;
/// use std::io::BufReader;
/// use std::io::prelude::*;
///
-/// # fn foo() -> std::io::Result<()> {
-/// let file = File::open("foo.txt")?;
-/// let mut buf_reader = BufReader::new(file);
-/// let mut contents = String::new();
-/// buf_reader.read_to_string(&mut contents)?;
-/// assert_eq!(contents, "Hello, world!");
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// let file = File::open("foo.txt")?;
+/// let mut buf_reader = BufReader::new(file);
+/// let mut contents = String::new();
+/// buf_reader.read_to_string(&mut contents)?;
+/// assert_eq!(contents, "Hello, world!");
+/// Ok(())
+/// }
/// ```
///
+/// Note that, although read and write methods require a `&mut File`, because
+/// of the interfaces for [`Read`] and [`Write`], the holder of a `&File` can
+/// still modify the file, either through methods that take `&File` or by
+/// retrieving the underlying OS object and modifying the file that way.
+/// Additionally, many operating systems allow concurrent modification of files
+/// by different processes. Avoid assuming that holding a `&File` means that the
+/// file will not change.
+///
/// [`Seek`]: ../io/trait.Seek.html
/// [`String`]: ../string/struct.String.html
/// [`Read`]: ../io/trait.Read.html
+/// [`Write`]: ../io/trait.Write.html
/// [`BufReader<R>`]: ../io/struct.BufReader.html
#[stable(feature = "rust1", since = "1.0.0")]
pub struct File {
@@ -222,7 +231,9 @@ fn initial_buffer_size(file: &File) -> usize {
/// Read the entire contents of a file into a bytes vector.
///
/// This is a convenience function for using [`File::open`] and [`read_to_end`]
-/// with fewer imports and without an intermediate variable.
+/// with fewer imports and without an intermediate variable. It pre-allocates a
+/// buffer based on the file size when available, so it is generally faster than
+/// reading into a vector created with `Vec::new()`.
///
/// [`File::open`]: struct.File.html#method.open
/// [`read_to_end`]: ../io/trait.Read.html#method.read_to_end
@@ -242,17 +253,15 @@ fn initial_buffer_size(file: &File) -> usize {
/// # Examples
///
/// ```no_run
-/// #![feature(fs_read_write)]
-///
/// use std::fs;
/// use std::net::SocketAddr;
///
-/// # fn foo() -> Result<(), Box<std::error::Error + 'static>> {
-/// let foo: SocketAddr = String::from_utf8_lossy(&fs::read("address.txt")?).parse()?;
-/// # Ok(())
-/// # }
+/// fn main() -> Result<(), Box<std::error::Error + 'static>> {
+/// let foo: SocketAddr = String::from_utf8_lossy(&fs::read("address.txt")?).parse()?;
+/// Ok(())
+/// }
/// ```
-#[unstable(feature = "fs_read_write", issue = "46588")]
+#[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
let mut file = File::open(path)?;
let mut bytes = Vec::with_capacity(initial_buffer_size(&file));
@@ -263,7 +272,9 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
/// Read the entire contents of a file into a string.
///
/// This is a convenience function for using [`File::open`] and [`read_to_string`]
-/// with fewer imports and without an intermediate variable.
+/// with fewer imports and without an intermediate variable. It pre-allocates a
+/// buffer based on the file size when available, so it is generally faster than
+/// reading into a string created with `String::new()`.
///
/// [`File::open`]: struct.File.html#method.open
/// [`read_to_string`]: ../io/trait.Read.html#method.read_to_string
@@ -289,13 +300,13 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
/// use std::fs;
/// use std::net::SocketAddr;
///
-/// # fn foo() -> Result<(), Box<std::error::Error + 'static>> {
-/// let foo: SocketAddr = fs::read_string("address.txt")?.parse()?;
-/// # Ok(())
-/// # }
+/// fn main() -> Result<(), Box<std::error::Error + 'static>> {
+/// 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)?;
@@ -316,16 +327,14 @@ pub fn read_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
/// # Examples
///
/// ```no_run
-/// #![feature(fs_read_write)]
-///
/// use std::fs;
///
-/// # fn foo() -> std::io::Result<()> {
-/// fs::write("foo.txt", b"Lorem ipsum")?;
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// fs::write("foo.txt", b"Lorem ipsum")?;
+/// Ok(())
+/// }
/// ```
-#[unstable(feature = "fs_read_write", issue = "46588")]
+#[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
File::create(path)?.write_all(contents.as_ref())
}
@@ -347,7 +356,7 @@ impl File {
/// ```no_run
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
+ /// fn main() -> std::io::Result<()> {
/// let mut f = File::open("foo.txt")?;
/// # Ok(())
/// # }
@@ -371,10 +380,10 @@ impl File {
/// ```no_run
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let mut f = File::create("foo.txt")?;
- /// # Ok(())
- /// # }
+ /// fn main() -> std::io::Result<()> {
+ /// let mut f = File::create("foo.txt")?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn create<P: AsRef<Path>>(path: P) -> io::Result<File> {
@@ -392,13 +401,13 @@ impl File {
/// use std::fs::File;
/// use std::io::prelude::*;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let mut f = File::create("foo.txt")?;
- /// f.write_all(b"Hello, world!")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let mut f = File::create("foo.txt")?;
+ /// f.write_all(b"Hello, world!")?;
///
- /// f.sync_all()?;
- /// # Ok(())
- /// # }
+ /// f.sync_all()?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn sync_all(&self) -> io::Result<()> {
@@ -423,13 +432,13 @@ impl File {
/// use std::fs::File;
/// use std::io::prelude::*;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let mut f = File::create("foo.txt")?;
- /// f.write_all(b"Hello, world!")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let mut f = File::create("foo.txt")?;
+ /// f.write_all(b"Hello, world!")?;
///
- /// f.sync_data()?;
- /// # Ok(())
- /// # }
+ /// f.sync_data()?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn sync_data(&self) -> io::Result<()> {
@@ -444,6 +453,10 @@ impl File {
/// will be extended to `size` and have all of the intermediate data filled
/// in with 0s.
///
+ /// The file's cursor isn't changed. In particular, if the cursor was at the
+ /// end and the file is shrunk using this operation, the cursor will now be
+ /// past the end.
+ ///
/// # Errors
///
/// This function will return an error if the file is not opened for writing.
@@ -453,12 +466,15 @@ impl File {
/// ```no_run
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let mut f = File::create("foo.txt")?;
- /// f.set_len(10)?;
- /// # Ok(())
- /// # }
+ /// fn main() -> std::io::Result<()> {
+ /// let mut f = File::create("foo.txt")?;
+ /// f.set_len(10)?;
+ /// Ok(())
+ /// }
/// ```
+ ///
+ /// Note that this method alters the content of the underlying file, even
+ /// though it takes `&self` rather than `&mut self`.
#[stable(feature = "rust1", since = "1.0.0")]
pub fn set_len(&self, size: u64) -> io::Result<()> {
self.inner.truncate(size)
@@ -471,33 +487,55 @@ impl File {
/// ```no_run
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let mut f = File::open("foo.txt")?;
- /// let metadata = f.metadata()?;
- /// # Ok(())
- /// # }
+ /// fn main() -> std::io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ /// let metadata = f.metadata()?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn metadata(&self) -> io::Result<Metadata> {
self.inner.file_attr().map(Metadata)
}
- /// Creates a new independently owned handle to the underlying file.
- ///
- /// The returned `File` is a reference to the same state that this object
- /// references. Both handles will read and write with the same cursor
- /// position.
+ /// Create a new `File` instance that shares the same underlying file handle
+ /// as the existing `File` instance. Reads, writes, and seeks will affect
+ /// both `File` instances simultaneously.
///
/// # Examples
///
+ /// Create two handles for a file named `foo.txt`:
+ ///
/// ```no_run
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let mut f = File::open("foo.txt")?;
- /// let file_copy = f.try_clone()?;
- /// # Ok(())
- /// # }
+ /// fn main() -> std::io::Result<()> {
+ /// let mut file = File::open("foo.txt")?;
+ /// let file_copy = file.try_clone()?;
+ /// Ok(())
+ /// }
+ /// ```
+ ///
+ /// Assuming there’s a file named `foo.txt` with contents `abcdef\n`, create
+ /// two handles, seek one of them, and read the remaining bytes from the
+ /// other handle:
+ ///
+ /// ```no_run
+ /// use std::fs::File;
+ /// use std::io::SeekFrom;
+ /// use std::io::prelude::*;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let mut file = File::open("foo.txt")?;
+ /// let mut file_copy = file.try_clone()?;
+ ///
+ /// file.seek(SeekFrom::Start(3))?;
+ ///
+ /// let mut contents = vec![];
+ /// file_copy.read_to_end(&mut contents)?;
+ /// assert_eq!(contents, b"def\n");
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "file_try_clone", since = "1.9.0")]
pub fn try_clone(&self) -> io::Result<File> {
@@ -524,17 +562,20 @@ impl File {
///
/// # Examples
///
+ /// ```no_run
+ /// fn main() -> std::io::Result<()> {
+ /// use std::fs::File;
+ ///
+ /// let file = File::open("foo.txt")?;
+ /// let mut perms = file.metadata()?.permissions();
+ /// perms.set_readonly(true);
+ /// file.set_permissions(perms)?;
+ /// Ok(())
+ /// }
/// ```
- /// # fn foo() -> std::io::Result<()> {
- /// use std::fs::File;
///
- /// let file = File::open("foo.txt")?;
- /// let mut perms = file.metadata()?.permissions();
- /// perms.set_readonly(true);
- /// file.set_permissions(perms)?;
- /// # Ok(())
- /// # }
- /// ```
+ /// Note that this method alters the permissions of the underlying file,
+ /// even though it takes `&self` rather than `&mut self`.
#[stable(feature = "set_permissions_atomic", since = "1.16.0")]
pub fn set_permissions(&self, perm: Permissions) -> io::Result<()> {
self.inner.set_permissions(perm.0)
@@ -850,51 +891,63 @@ impl Metadata {
///
/// # Examples
///
- /// ```
- /// # fn foo() -> std::io::Result<()> {
- /// use std::fs;
+ /// ```no_run
+ /// fn main() -> std::io::Result<()> {
+ /// use std::fs;
///
- /// let metadata = fs::metadata("foo.txt")?;
+ /// let metadata = fs::metadata("foo.txt")?;
///
- /// println!("{:?}", metadata.file_type());
- /// # Ok(())
- /// # }
+ /// println!("{:?}", metadata.file_type());
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "file_type", since = "1.1.0")]
pub fn file_type(&self) -> FileType {
FileType(self.0.file_type())
}
- /// Returns whether this metadata is for a directory.
+ /// Returns whether this metadata is for a directory. The
+ /// result is mutually exclusive to the result of
+ /// [`is_file`], and will be false for symlink metadata
+ /// obtained from [`symlink_metadata`].
+ ///
+ /// [`is_file`]: struct.Metadata.html#method.is_file
+ /// [`symlink_metadata`]: fn.symlink_metadata.html
///
/// # Examples
///
- /// ```
- /// # fn foo() -> std::io::Result<()> {
- /// use std::fs;
+ /// ```no_run
+ /// fn main() -> std::io::Result<()> {
+ /// use std::fs;
///
- /// let metadata = fs::metadata("foo.txt")?;
+ /// let metadata = fs::metadata("foo.txt")?;
///
- /// assert!(!metadata.is_dir());
- /// # Ok(())
- /// # }
+ /// assert!(!metadata.is_dir());
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_dir(&self) -> bool { self.file_type().is_dir() }
- /// Returns whether this metadata is for a regular file.
+ /// Returns whether this metadata is for a regular file. The
+ /// result is mutually exclusive to the result of
+ /// [`is_dir`], and will be false for symlink metadata
+ /// obtained from [`symlink_metadata`].
+ ///
+ /// [`is_dir`]: struct.Metadata.html#method.is_dir
+ /// [`symlink_metadata`]: fn.symlink_metadata.html
///
/// # Examples
///
- /// ```
- /// # fn foo() -> std::io::Result<()> {
+ /// ```no_run
/// use std::fs;
///
- /// let metadata = fs::metadata("foo.txt")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let metadata = fs::metadata("foo.txt")?;
///
- /// assert!(metadata.is_file());
- /// # Ok(())
- /// # }
+ /// assert!(metadata.is_file());
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_file(&self) -> bool { self.file_type().is_file() }
@@ -903,15 +956,15 @@ impl Metadata {
///
/// # Examples
///
- /// ```
- /// # fn foo() -> std::io::Result<()> {
+ /// ```no_run
/// use std::fs;
///
- /// let metadata = fs::metadata("foo.txt")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let metadata = fs::metadata("foo.txt")?;
///
- /// assert_eq!(0, metadata.len());
- /// # Ok(())
- /// # }
+ /// assert_eq!(0, metadata.len());
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> u64 { self.0.size() }
@@ -920,15 +973,15 @@ impl Metadata {
///
/// # Examples
///
- /// ```
- /// # fn foo() -> std::io::Result<()> {
+ /// ```no_run
/// use std::fs;
///
- /// let metadata = fs::metadata("foo.txt")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let metadata = fs::metadata("foo.txt")?;
///
- /// assert!(!metadata.permissions().readonly());
- /// # Ok(())
- /// # }
+ /// assert!(!metadata.permissions().readonly());
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn permissions(&self) -> Permissions {
@@ -947,19 +1000,19 @@ impl Metadata {
///
/// # Examples
///
- /// ```
- /// # fn foo() -> std::io::Result<()> {
+ /// ```no_run
/// use std::fs;
///
- /// let metadata = fs::metadata("foo.txt")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let metadata = fs::metadata("foo.txt")?;
///
- /// if let Ok(time) = metadata.modified() {
- /// println!("{:?}", time);
- /// } else {
- /// println!("Not supported on this platform");
+ /// if let Ok(time) = metadata.modified() {
+ /// println!("{:?}", time);
+ /// } else {
+ /// println!("Not supported on this platform");
+ /// }
+ /// Ok(())
/// }
- /// # Ok(())
- /// # }
/// ```
#[stable(feature = "fs_time", since = "1.10.0")]
pub fn modified(&self) -> io::Result<SystemTime> {
@@ -982,26 +1035,26 @@ impl Metadata {
///
/// # Examples
///
- /// ```
- /// # fn foo() -> std::io::Result<()> {
+ /// ```no_run
/// use std::fs;
///
- /// let metadata = fs::metadata("foo.txt")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let metadata = fs::metadata("foo.txt")?;
///
- /// if let Ok(time) = metadata.accessed() {
- /// println!("{:?}", time);
- /// } else {
- /// println!("Not supported on this platform");
+ /// if let Ok(time) = metadata.accessed() {
+ /// println!("{:?}", time);
+ /// } else {
+ /// println!("Not supported on this platform");
+ /// }
+ /// Ok(())
/// }
- /// # Ok(())
- /// # }
/// ```
#[stable(feature = "fs_time", since = "1.10.0")]
pub fn accessed(&self) -> io::Result<SystemTime> {
self.0.accessed().map(FromInner::from_inner)
}
- /// Returns the creation time listed in the this metadata.
+ /// Returns the creation time listed in this metadata.
///
/// The returned value corresponds to the `birthtime` field of `stat` on
/// Unix platforms and the `ftCreationTime` field on Windows platforms.
@@ -1013,19 +1066,19 @@ impl Metadata {
///
/// # Examples
///
- /// ```
- /// # fn foo() -> std::io::Result<()> {
+ /// ```no_run
/// use std::fs;
///
- /// let metadata = fs::metadata("foo.txt")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let metadata = fs::metadata("foo.txt")?;
///
- /// if let Ok(time) = metadata.created() {
- /// println!("{:?}", time);
- /// } else {
- /// println!("Not supported on this platform");
+ /// if let Ok(time) = metadata.created() {
+ /// println!("{:?}", time);
+ /// } else {
+ /// println!("Not supported on this platform");
+ /// }
+ /// Ok(())
/// }
- /// # Ok(())
- /// # }
/// ```
#[stable(feature = "fs_time", since = "1.10.0")]
pub fn created(&self) -> io::Result<SystemTime> {
@@ -1057,16 +1110,16 @@ impl Permissions {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let mut f = File::create("foo.txt")?;
- /// let metadata = f.metadata()?;
+ /// fn main() -> std::io::Result<()> {
+ /// let mut f = File::create("foo.txt")?;
+ /// let metadata = f.metadata()?;
///
- /// assert_eq!(false, metadata.permissions().readonly());
- /// # Ok(())
- /// # }
+ /// assert_eq!(false, metadata.permissions().readonly());
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn readonly(&self) -> bool { self.0.readonly() }
@@ -1082,23 +1135,23 @@ impl Permissions {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let f = File::create("foo.txt")?;
- /// let metadata = f.metadata()?;
- /// let mut permissions = metadata.permissions();
+ /// fn main() -> std::io::Result<()> {
+ /// let f = File::create("foo.txt")?;
+ /// let metadata = f.metadata()?;
+ /// let mut permissions = metadata.permissions();
///
- /// permissions.set_readonly(true);
+ /// permissions.set_readonly(true);
///
- /// // filesystem doesn't change
- /// assert_eq!(false, metadata.permissions().readonly());
+ /// // filesystem doesn't change
+ /// assert_eq!(false, metadata.permissions().readonly());
///
- /// // just this particular `permissions`.
- /// assert_eq!(true, permissions.readonly());
- /// # Ok(())
- /// # }
+ /// // just this particular `permissions`.
+ /// assert_eq!(true, permissions.readonly());
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn set_readonly(&mut self, readonly: bool) {
@@ -1107,43 +1160,58 @@ impl Permissions {
}
impl FileType {
- /// Test whether this file type represents a directory.
+ /// Test whether this file type represents a directory. The
+ /// result is mutually exclusive to the results of
+ /// [`is_file`] and [`is_symlink`]; only zero or one of these
+ /// tests may pass.
+ ///
+ /// [`is_file`]: struct.FileType.html#method.is_file
+ /// [`is_symlink`]: struct.FileType.html#method.is_symlink
///
/// # Examples
///
- /// ```
- /// # fn foo() -> std::io::Result<()> {
- /// use std::fs;
+ /// ```no_run
+ /// fn main() -> std::io::Result<()> {
+ /// use std::fs;
///
- /// let metadata = fs::metadata("foo.txt")?;
- /// let file_type = metadata.file_type();
+ /// let metadata = fs::metadata("foo.txt")?;
+ /// let file_type = metadata.file_type();
///
- /// assert_eq!(file_type.is_dir(), false);
- /// # Ok(())
- /// # }
+ /// assert_eq!(file_type.is_dir(), false);
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "file_type", since = "1.1.0")]
pub fn is_dir(&self) -> bool { self.0.is_dir() }
/// Test whether this file type represents a regular file.
+ /// The result is mutually exclusive to the results of
+ /// [`is_dir`] and [`is_symlink`]; only zero or one of these
+ /// tests may pass.
+ ///
+ /// [`is_dir`]: struct.FileType.html#method.is_dir
+ /// [`is_symlink`]: struct.FileType.html#method.is_symlink
///
/// # Examples
///
- /// ```
- /// # fn foo() -> std::io::Result<()> {
- /// use std::fs;
+ /// ```no_run
+ /// fn main() -> std::io::Result<()> {
+ /// use std::fs;
///
- /// let metadata = fs::metadata("foo.txt")?;
- /// let file_type = metadata.file_type();
+ /// let metadata = fs::metadata("foo.txt")?;
+ /// let file_type = metadata.file_type();
///
- /// assert_eq!(file_type.is_file(), true);
- /// # Ok(())
- /// # }
+ /// assert_eq!(file_type.is_file(), true);
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "file_type", since = "1.1.0")]
pub fn is_file(&self) -> bool { self.0.is_file() }
/// Test whether this file type represents a symbolic link.
+ /// The result is mutually exclusive to the results of
+ /// [`is_dir`] and [`is_file`]; only zero or one of these
+ /// tests may pass.
///
/// The underlying [`Metadata`] struct needs to be retrieved
/// with the [`fs::symlink_metadata`] function and not the
@@ -1154,20 +1222,22 @@ impl FileType {
/// [`Metadata`]: struct.Metadata.html
/// [`fs::metadata`]: fn.metadata.html
/// [`fs::symlink_metadata`]: fn.symlink_metadata.html
+ /// [`is_dir`]: struct.FileType.html#method.is_dir
+ /// [`is_file`]: struct.FileType.html#method.is_file
/// [`is_symlink`]: struct.FileType.html#method.is_symlink
///
/// # Examples
///
- /// ```
- /// # fn foo() -> std::io::Result<()> {
+ /// ```no_run
/// use std::fs;
///
- /// let metadata = fs::symlink_metadata("foo.txt")?;
- /// let file_type = metadata.file_type();
+ /// fn main() -> std::io::Result<()> {
+ /// let metadata = fs::symlink_metadata("foo.txt")?;
+ /// let file_type = metadata.file_type();
///
- /// assert_eq!(file_type.is_symlink(), false);
- /// # Ok(())
- /// # }
+ /// assert_eq!(file_type.is_symlink(), false);
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "file_type", since = "1.1.0")]
pub fn is_symlink(&self) -> bool { self.0.is_symlink() }
@@ -1204,15 +1274,16 @@ impl DirEntry {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::fs;
- /// # fn foo() -> std::io::Result<()> {
- /// for entry in fs::read_dir(".")? {
- /// let dir = entry?;
- /// println!("{:?}", dir.path());
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// for entry in fs::read_dir(".")? {
+ /// let dir = entry?;
+ /// println!("{:?}", dir.path());
+ /// }
+ /// Ok(())
/// }
- /// # Ok(())
- /// # }
/// ```
///
/// This prints output like:
@@ -1357,13 +1428,13 @@ impl AsInner<fs_imp::DirEntry> for DirEntry {
///
/// # Examples
///
-/// ```
+/// ```no_run
/// use std::fs;
///
-/// # fn foo() -> std::io::Result<()> {
-/// fs::remove_file("a.txt")?;
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// fs::remove_file("a.txt")?;
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
@@ -1394,14 +1465,14 @@ pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
///
/// # Examples
///
-/// ```rust
-/// # fn foo() -> std::io::Result<()> {
+/// ```rust,no_run
/// use std::fs;
///
-/// let attr = fs::metadata("/some/file/path.txt")?;
-/// // inspect attr ...
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// let attr = fs::metadata("/some/file/path.txt")?;
+/// // inspect attr ...
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
@@ -1428,14 +1499,14 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
///
/// # Examples
///
-/// ```rust
-/// # fn foo() -> std::io::Result<()> {
+/// ```rust,no_run
/// use std::fs;
///
-/// let attr = fs::symlink_metadata("/some/file/path.txt")?;
-/// // inspect attr ...
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// let attr = fs::symlink_metadata("/some/file/path.txt")?;
+/// // inspect attr ...
+/// Ok(())
+/// }
/// ```
#[stable(feature = "symlink_metadata", since = "1.1.0")]
pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
@@ -1472,13 +1543,13 @@ pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
///
/// # Examples
///
-/// ```
+/// ```no_run
/// use std::fs;
///
-/// # fn foo() -> std::io::Result<()> {
-/// fs::rename("a.txt", "b.txt")?; // Rename a.txt to b.txt
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// fs::rename("a.txt", "b.txt")?; // Rename a.txt to b.txt
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
@@ -1523,9 +1594,10 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
/// ```no_run
/// use std::fs;
///
-/// # fn foo() -> std::io::Result<()> {
-/// fs::copy("foo.txt", "bar.txt")?; // Copy foo.txt to bar.txt
-/// # Ok(()) }
+/// fn main() -> std::io::Result<()> {
+/// fs::copy("foo.txt", "bar.txt")?; // Copy foo.txt to bar.txt
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
@@ -1554,13 +1626,13 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
///
/// # Examples
///
-/// ```
+/// ```no_run
/// use std::fs;
///
-/// # fn foo() -> std::io::Result<()> {
-/// fs::hard_link("a.txt", "b.txt")?; // Hard link a.txt to b.txt
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// fs::hard_link("a.txt", "b.txt")?; // Hard link a.txt to b.txt
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
@@ -1577,13 +1649,13 @@ pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<(
///
/// # Examples
///
-/// ```
+/// ```no_run
/// use std::fs;
///
-/// # fn foo() -> std::io::Result<()> {
-/// fs::soft_link("a.txt", "b.txt")?;
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// fs::soft_link("a.txt", "b.txt")?;
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_deprecated(since = "1.1.0",
@@ -1614,13 +1686,13 @@ pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<(
///
/// # Examples
///
-/// ```
+/// ```no_run
/// use std::fs;
///
-/// # fn foo() -> std::io::Result<()> {
-/// let path = fs::read_link("a.txt")?;
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// let path = fs::read_link("a.txt")?;
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
@@ -1648,13 +1720,13 @@ pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
///
/// # Examples
///
-/// ```
+/// ```no_run
/// use std::fs;
///
-/// # fn foo() -> std::io::Result<()> {
-/// let path = fs::canonicalize("../a/../foo.txt")?;
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// let path = fs::canonicalize("../a/../foo.txt")?;
+/// Ok(())
+/// }
/// ```
#[stable(feature = "fs_canonicalize", since = "1.5.0")]
pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
@@ -1681,13 +1753,13 @@ pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
///
/// # Examples
///
-/// ```
+/// ```no_run
/// use std::fs;
///
-/// # fn foo() -> std::io::Result<()> {
-/// fs::create_dir("/some/dir")?;
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// fs::create_dir("/some/dir")?;
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
@@ -1723,13 +1795,13 @@ pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
///
/// # Examples
///
-/// ```
+/// ```no_run
/// use std::fs;
///
-/// # fn foo() -> std::io::Result<()> {
-/// fs::create_dir_all("/some/dir")?;
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// fs::create_dir_all("/some/dir")?;
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
@@ -1756,13 +1828,13 @@ pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
///
/// # Examples
///
-/// ```
+/// ```no_run
/// use std::fs;
///
-/// # fn foo() -> std::io::Result<()> {
-/// fs::remove_dir("/some/dir")?;
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// fs::remove_dir("/some/dir")?;
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
@@ -1790,13 +1862,13 @@ pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
///
/// # Examples
///
-/// ```
+/// ```no_run
/// use std::fs;
///
-/// # fn foo() -> std::io::Result<()> {
-/// fs::remove_dir_all("/some/dir")?;
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// fs::remove_dir_all("/some/dir")?;
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
@@ -1876,15 +1948,15 @@ pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
///
/// # Examples
///
-/// ```
-/// # fn foo() -> std::io::Result<()> {
+/// ```no_run
/// use std::fs;
///
-/// let mut perms = fs::metadata("foo.txt")?.permissions();
-/// perms.set_readonly(true);
-/// fs::set_permissions("foo.txt", perms)?;
-/// # Ok(())
-/// # }
+/// fn main() -> std::io::Result<()> {
+/// let mut perms = fs::metadata("foo.txt")?.permissions();
+/// perms.set_readonly(true);
+/// fs::set_permissions("foo.txt", perms)?;
+/// Ok(())
+/// }
/// ```
#[stable(feature = "set_permissions", since = "1.1.0")]
pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions)
@@ -3054,12 +3126,12 @@ mod tests {
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);
}