Skip to content

Commit 57d5029

Browse files
authored
Rollup merge of rust-lang#35009 - GuillaumeGomez:dir_entry_doc, r=steveklabnik
Dir entry doc Part of rust-lang#29356. r? @steveklabnik
2 parents 905e35f + 1669963 commit 57d5029

File tree

2 files changed

+100
-12
lines changed

2 files changed

+100
-12
lines changed

src/libstd/fs.rs

+84-12
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,37 @@ pub struct File {
5858

5959
/// Metadata information about a file.
6060
///
61-
/// This structure is returned from the `metadata` function or method and
61+
/// This structure is returned from the [`metadata`] function or method and
6262
/// represents known metadata about a file such as its permissions, size,
6363
/// modification times, etc.
64+
///
65+
/// [`metadata`]: fn.metadata.html
6466
#[stable(feature = "rust1", since = "1.0.0")]
6567
#[derive(Clone)]
6668
pub struct Metadata(fs_imp::FileAttr);
6769

6870
/// Iterator over the entries in a directory.
6971
///
70-
/// This iterator is returned from the `read_dir` function of this module and
71-
/// will yield instances of `io::Result<DirEntry>`. Through a `DirEntry`
72+
/// This iterator is returned from the [`read_dir`] function of this module and
73+
/// will yield instances of `io::Result<DirEntry>`. Through a [`DirEntry`]
7274
/// information like the entry's path and possibly other metadata can be
7375
/// learned.
7476
///
77+
/// [`read_dir`]: fn.read_dir.html
78+
/// [`DirEntry`]: struct.DirEntry.html
79+
///
7580
/// # Errors
7681
///
77-
/// This `io::Result` will be an `Err` if there's some sort of intermittent
82+
/// This [`io::Result`] will be an `Err` if there's some sort of intermittent
7883
/// IO error during iteration.
84+
///
85+
/// [`io::Result`]: ../io/type.Result.html
7986
#[stable(feature = "rust1", since = "1.0.0")]
8087
pub struct ReadDir(fs_imp::ReadDir);
8188

82-
/// Entries returned by the `ReadDir` iterator.
89+
/// Entries returned by the [`ReadDir`] iterator.
90+
///
91+
/// [`ReadDir`]: struct.ReadDir.html
8392
///
8493
/// An instance of `DirEntry` represents an entry inside of a directory on the
8594
/// filesystem. Each entry can be inspected via methods to learn about the full
@@ -89,17 +98,23 @@ pub struct DirEntry(fs_imp::DirEntry);
8998

9099
/// Options and flags which can be used to configure how a file is opened.
91100
///
92-
/// This builder exposes the ability to configure how a `File` is opened and
93-
/// what operations are permitted on the open file. The `File::open` and
94-
/// `File::create` methods are aliases for commonly used options using this
101+
/// This builder exposes the ability to configure how a [`File`] is opened and
102+
/// what operations are permitted on the open file. The [`File::open`] and
103+
/// [`File::create`] methods are aliases for commonly used options using this
95104
/// builder.
96105
///
97-
/// Generally speaking, when using `OpenOptions`, you'll first call `new()`,
98-
/// then chain calls to methods to set each option, then call `open()`, passing
99-
/// the path of the file you're trying to open. This will give you a
106+
/// [`File`]: struct.File.html
107+
/// [`File::open`]: struct.File.html#method.open
108+
/// [`File::create`]: struct.File.html#method.create
109+
///
110+
/// Generally speaking, when using `OpenOptions`, you'll first call [`new()`],
111+
/// then chain calls to methods to set each option, then call [`open()`],
112+
/// passing the path of the file you're trying to open. This will give you a
100113
/// [`io::Result`][result] with a [`File`][file] inside that you can further
101114
/// operate on.
102115
///
116+
/// [`new()`]: struct.OpenOptions.html#method.new
117+
/// [`open()`]: struct.OpenOptions.html#method.open
103118
/// [result]: ../io/type.Result.html
104119
/// [file]: struct.File.html
105120
///
@@ -131,10 +146,12 @@ pub struct OpenOptions(fs_imp::OpenOptions);
131146

132147
/// Representation of the various permissions on a file.
133148
///
134-
/// This module only currently provides one bit of information, `readonly`,
149+
/// This module only currently provides one bit of information, [`readonly`],
135150
/// which is exposed on all currently supported platforms. Unix-specific
136151
/// functionality, such as mode bits, is available through the
137152
/// `os::unix::PermissionsExt` trait.
153+
///
154+
/// [`readonly`]: struct.Permissions.html#method.readonly
138155
#[derive(Clone, PartialEq, Eq, Debug)]
139156
#[stable(feature = "rust1", since = "1.0.0")]
140157
pub struct Permissions(fs_imp::FilePermissions);
@@ -829,6 +846,26 @@ impl DirEntry {
829846
/// On Windows this function is cheap to call (no extra system calls
830847
/// needed), but on Unix platforms this function is the equivalent of
831848
/// calling `symlink_metadata` on the path.
849+
///
850+
/// # Examples
851+
///
852+
/// ```
853+
/// use std::fs;
854+
///
855+
/// if let Ok(entries) = fs::read_dir(".") {
856+
/// for entry in entries {
857+
/// if let Ok(entry) = entry {
858+
/// // Here, `entry` is a `DirEntry`.
859+
/// if let Ok(metadata) = entry.metadata() {
860+
/// // Now let's show our entry's permissions!
861+
/// println!("{:?}: {:?}", entry.path(), metadata.permissions());
862+
/// } else {
863+
/// println!("Couldn't get metadata for {:?}", entry.path());
864+
/// }
865+
/// }
866+
/// }
867+
/// }
868+
/// ```
832869
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
833870
pub fn metadata(&self) -> io::Result<Metadata> {
834871
self.0.metadata().map(Metadata)
@@ -844,13 +881,48 @@ impl DirEntry {
844881
/// On Windows and most Unix platforms this function is free (no extra
845882
/// system calls needed), but some Unix platforms may require the equivalent
846883
/// call to `symlink_metadata` to learn about the target file type.
884+
///
885+
/// # Examples
886+
///
887+
/// ```
888+
/// use std::fs;
889+
///
890+
/// if let Ok(entries) = fs::read_dir(".") {
891+
/// for entry in entries {
892+
/// if let Ok(entry) = entry {
893+
/// // Here, `entry` is a `DirEntry`.
894+
/// if let Ok(file_type) = entry.file_type() {
895+
/// // Now let's show our entry's file type!
896+
/// println!("{:?}: {:?}", entry.path(), file_type);
897+
/// } else {
898+
/// println!("Couldn't get file type for {:?}", entry.path());
899+
/// }
900+
/// }
901+
/// }
902+
/// }
903+
/// ```
847904
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
848905
pub fn file_type(&self) -> io::Result<FileType> {
849906
self.0.file_type().map(FileType)
850907
}
851908

852909
/// Returns the bare file name of this directory entry without any other
853910
/// leading path component.
911+
///
912+
/// # Examples
913+
///
914+
/// ```
915+
/// use std::fs;
916+
///
917+
/// if let Ok(entries) = fs::read_dir(".") {
918+
/// for entry in entries {
919+
/// if let Ok(entry) = entry {
920+
/// // Here, `entry` is a `DirEntry`.
921+
/// println!("{:?}", entry.file_name());
922+
/// }
923+
/// }
924+
/// }
925+
/// ```
854926
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
855927
pub fn file_name(&self) -> OsString {
856928
self.0.file_name()

src/libstd/sys/unix/ext/fs.rs

+16
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@ impl FileTypeExt for fs::FileType {
196196
pub trait DirEntryExt {
197197
/// Returns the underlying `d_ino` field in the contained `dirent`
198198
/// structure.
199+
///
200+
/// # Examples
201+
///
202+
/// ```
203+
/// use std::fs;
204+
/// use std::os::unix::fs::DirEntryExt;
205+
///
206+
/// if let Ok(entries) = fs::read_dir(".") {
207+
/// for entry in entries {
208+
/// if let Ok(entry) = entry {
209+
/// // Here, `entry` is a `DirEntry`.
210+
/// println!("{:?}: {}", entry.file_name(), entry.ino());
211+
/// }
212+
/// }
213+
/// }
214+
/// ```
199215
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
200216
fn ino(&self) -> u64;
201217
}

0 commit comments

Comments
 (0)