Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e206d6a

Browse files
authoredJan 24, 2025
Overhaul examples for PermissionsExt
This fixes #91707 by including one overarching example, instead of the small examples that can be misleading.
1 parent 8231e85 commit e206d6a

File tree

1 file changed

+59
-49
lines changed
  • library/std/src/os/unix

1 file changed

+59
-49
lines changed
 

‎library/std/src/os/unix/fs.rs

+59-49
Original file line numberDiff line numberDiff line change
@@ -276,63 +276,73 @@ impl FileExt for fs::File {
276276
}
277277

278278
/// Unix-specific extensions to [`fs::Permissions`].
279+
///
280+
/// # Examples
281+
///
282+
/// ```no_run
283+
/// use std::fs::{File, Permissions};
284+
/// use std::io::{ErrorKind, Result as IoResult};
285+
/// use std::os::unix::fs::PermissionsExt;
286+
///
287+
/// fn main() -> IoResult<()> {
288+
/// let name = "test_file_for_permissions";
289+
///
290+
/// // make sure file does not exist
291+
/// let _ = std::fs::remove_file(name);
292+
/// assert_eq!(
293+
/// File::open(name).unwrap_err().kind(),
294+
/// ErrorKind::NotFound,
295+
/// "file already exists"
296+
/// );
297+
///
298+
/// // file mode that we want to add in
299+
/// let my_mode = 0o007;
300+
///
301+
/// // create new file with specified permissions
302+
/// {
303+
/// let file = File::create(name)?;
304+
/// let mut permissions = file.metadata()?.permissions();
305+
/// eprintln!("Current permissions: {:o}", permissions.mode());
306+
///
307+
/// // make sure new permission is not already present
308+
/// assert!(
309+
/// permissions.mode() & my_mode != my_mode,
310+
/// "permission already present"
311+
/// );
312+
///
313+
/// // either set read/write/execute for owner using set_mode
314+
/// permissions.set_mode(permissions.mode() | my_mode);
315+
///
316+
/// // or use from_mode to construct new Permissions
317+
/// permissions = Permissions::from_mode(permissions.mode() | my_mode);
318+
///
319+
/// // write new permissions to file
320+
/// file.set_permissions(permissions)?;
321+
/// }
322+
///
323+
/// let permissions = File::open(name)?.metadata()?.permissions();
324+
/// eprintln!("New permissions: {:o}", permissions.mode());
325+
///
326+
/// // assert new permissions were set
327+
/// assert_eq!(
328+
/// permissions.mode() & my_mode,
329+
/// my_mode,
330+
/// "new permission not set"
331+
/// );
332+
/// Ok(())
333+
/// }
334+
/// ```
279335
#[stable(feature = "fs_ext", since = "1.1.0")]
280336
pub trait PermissionsExt {
281-
/// Returns the underlying raw `st_mode` bits that contain the standard
282-
/// Unix permissions for this file.
283-
///
284-
/// # Examples
285-
///
286-
/// ```no_run
287-
/// use std::fs::File;
288-
/// use std::os::unix::fs::PermissionsExt;
289-
///
290-
/// fn main() -> std::io::Result<()> {
291-
/// let f = File::create("foo.txt")?;
292-
/// let metadata = f.metadata()?;
293-
/// let permissions = metadata.permissions();
294-
///
295-
/// println!("permissions: {:o}", permissions.mode());
296-
/// Ok(())
297-
/// }
298-
/// ```
337+
/// Returns the raw mode permission bits
299338
#[stable(feature = "fs_ext", since = "1.1.0")]
300339
fn mode(&self) -> u32;
301340

302-
/// Sets the underlying raw bits for this set of permissions.
303-
///
304-
/// # Examples
305-
///
306-
/// ```no_run
307-
/// use std::fs::File;
308-
/// use std::os::unix::fs::PermissionsExt;
309-
///
310-
/// fn main() -> std::io::Result<()> {
311-
/// let f = File::create("foo.txt")?;
312-
/// let metadata = f.metadata()?;
313-
/// let mut permissions = metadata.permissions();
314-
///
315-
/// permissions.set_mode(0o644); // Read/write for owner and read for others.
316-
/// assert_eq!(permissions.mode(), 0o644);
317-
/// Ok(())
318-
/// }
319-
/// ```
341+
/// Sets the underlying raw mode permission bits.
320342
#[stable(feature = "fs_ext", since = "1.1.0")]
321343
fn set_mode(&mut self, mode: u32);
322344

323-
/// Creates a new instance of `Permissions` from the given set of Unix
324-
/// permission bits.
325-
///
326-
/// # Examples
327-
///
328-
/// ```
329-
/// use std::fs::Permissions;
330-
/// use std::os::unix::fs::PermissionsExt;
331-
///
332-
/// // Read/write for owner and read for others.
333-
/// let permissions = Permissions::from_mode(0o644);
334-
/// assert_eq!(permissions.mode(), 0o644);
335-
/// ```
345+
/// Creates a new instance from the given raw mode permission bits.
336346
#[stable(feature = "fs_ext", since = "1.1.0")]
337347
#[cfg_attr(not(test), rustc_diagnostic_item = "permissions_from_mode")]
338348
fn from_mode(mode: u32) -> Self;

0 commit comments

Comments
 (0)
Please sign in to comment.