about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-02-04 23:28:59 +0800
committerGitHub <noreply@github.com>2018-02-04 23:28:59 +0800
commite58cff2a4d28d50a83cca7adeda54f1f671ab5f3 (patch)
tree03ff492be8dbce826bc331a206e42345c3a72dac /src/libstd
parent1439c2ac35e2e2b03fdc336dc108865387565bd7 (diff)
parentd597da32672805644b6dc76cfffeca6b8c4d8e62 (diff)
downloadrust-e58cff2a4d28d50a83cca7adeda54f1f671ab5f3.tar.gz
rust-e58cff2a4d28d50a83cca7adeda54f1f671ab5f3.zip
Rollup merge of #47958 - frewsxcv:frewsxcv-try-clone, r=aidanhs
Clarify shared file handler behavior of File::try_clone.

Fixes https://github.com/rust-lang/rust/issues/46578.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/fs.rs36
1 files changed, 29 insertions, 7 deletions
diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs
index d1f3ccbd2c6..594c9d0ff5a 100644
--- a/src/libstd/fs.rs
+++ b/src/libstd/fs.rs
@@ -482,20 +482,42 @@ impl File {
         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()?;
+    /// 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 foo() -> 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(())
     /// # }
     /// ```