Skip to content

Commit e58cff2

Browse files
authored
Rollup merge of rust-lang#47958 - frewsxcv:frewsxcv-try-clone, r=aidanhs
Clarify shared file handler behavior of File::try_clone. Fixes rust-lang#46578.
2 parents 1439c2a + d597da3 commit e58cff2

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/libstd/fs.rs

+29-7
Original file line numberDiff line numberDiff line change
@@ -482,20 +482,42 @@ impl File {
482482
self.inner.file_attr().map(Metadata)
483483
}
484484

485-
/// Creates a new independently owned handle to the underlying file.
486-
///
487-
/// The returned `File` is a reference to the same state that this object
488-
/// references. Both handles will read and write with the same cursor
489-
/// position.
485+
/// Create a new `File` instance that shares the same underlying file handle
486+
/// as the existing `File` instance. Reads, writes, and seeks will affect
487+
/// both `File` instances simultaneously.
490488
///
491489
/// # Examples
492490
///
491+
/// Create two handles for a file named `foo.txt`:
492+
///
493493
/// ```no_run
494494
/// use std::fs::File;
495495
///
496496
/// # fn foo() -> std::io::Result<()> {
497-
/// let mut f = File::open("foo.txt")?;
498-
/// let file_copy = f.try_clone()?;
497+
/// let mut file = File::open("foo.txt")?;
498+
/// let file_copy = file.try_clone()?;
499+
/// # Ok(())
500+
/// # }
501+
/// ```
502+
///
503+
/// Assuming there’s a file named `foo.txt` with contents `abcdef\n`, create
504+
/// two handles, seek one of them, and read the remaining bytes from the
505+
/// other handle:
506+
///
507+
/// ```no_run
508+
/// use std::fs::File;
509+
/// use std::io::SeekFrom;
510+
/// use std::io::prelude::*;
511+
///
512+
/// # fn foo() -> std::io::Result<()> {
513+
/// let mut file = File::open("foo.txt")?;
514+
/// let mut file_copy = file.try_clone()?;
515+
///
516+
/// file.seek(SeekFrom::Start(3))?;
517+
///
518+
/// let mut contents = vec![];
519+
/// file_copy.read_to_end(&mut contents)?;
520+
/// assert_eq!(contents, b"def\n");
499521
/// # Ok(())
500522
/// # }
501523
/// ```

0 commit comments

Comments
 (0)