Skip to content

Commit c67a636

Browse files
Rollup merge of rust-lang#48273 - alercah:file-warning, r=joshtriplett
Add a warning to File about mutability. Fixes rust-lang#47708.
2 parents 6078dc2 + ec90597 commit c67a636

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/libstd/fs.rs

+15
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,18 @@ use time::SystemTime;
8181
/// # }
8282
/// ```
8383
///
84+
/// Note that, although read and write methods require a `&mut File`, because
85+
/// of the interfaces for [`Read`] and [`Write`], the holder of a `&File` can
86+
/// still modify the file, either through methods that take `&File` or by
87+
/// retrieving the underlying OS object and modifying the file that way.
88+
/// Additionally, many operating systems allow concurrent modification of files
89+
/// by different processes. Avoid assuming that holding a `&File` means that the
90+
/// file will not change.
91+
///
8492
/// [`Seek`]: ../io/trait.Seek.html
8593
/// [`String`]: ../string/struct.String.html
8694
/// [`Read`]: ../io/trait.Read.html
95+
/// [`Write`]: ../io/trait.Write.html
8796
/// [`BufReader<R>`]: ../io/struct.BufReader.html
8897
#[stable(feature = "rust1", since = "1.0.0")]
8998
pub struct File {
@@ -459,6 +468,9 @@ impl File {
459468
/// # Ok(())
460469
/// # }
461470
/// ```
471+
///
472+
/// Note that this method alters the content of the underlying file, even
473+
/// though it takes `&self` rather than `&mut self`.
462474
#[stable(feature = "rust1", since = "1.0.0")]
463475
pub fn set_len(&self, size: u64) -> io::Result<()> {
464476
self.inner.truncate(size)
@@ -557,6 +569,9 @@ impl File {
557569
/// # Ok(())
558570
/// # }
559571
/// ```
572+
///
573+
/// Note that this method alters the permissions of the underlying file,
574+
/// even though it takes `&self` rather than `&mut self`.
560575
#[stable(feature = "set_permissions_atomic", since = "1.16.0")]
561576
pub fn set_permissions(&self, perm: Permissions) -> io::Result<()> {
562577
self.inner.set_permissions(perm.0)

0 commit comments

Comments
 (0)