Skip to content

Commit e9c75a8

Browse files
author
Alexis Hunt
committed
Add a warning to File about mutability.
Fixes #47708.
1 parent 5570cdc commit e9c75a8

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`], it is still possible to
86+
/// modify a file through a `&File`, either through methods that take `&File`
87+
/// or by retrieving a raw OS filehandle and modifying the file that way.
88+
/// Additionally, many operating systems allow concurrent modification of files
89+
/// by different processes. Care should be taken not to assume that holding a
90+
/// `&File` means that the 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)