Skip to content

Commit 1afe77f

Browse files
author
lukaramu
committed
Cleaned up throughout std::path's docs
Part of #29368. * added missing links * updated method summaries to use 3rd person style * added missing periods in `Component`'s variant summaries * use standard iterator boilerplate in `Components`' and `Iter`'s docs * added example to `Iter::as_path`, adapted from `Components::as_path`'s example * consolidated examples for `Path::file_name` * some other small fixes
1 parent ae23e65 commit 1afe77f

File tree

1 file changed

+78
-47
lines changed

1 file changed

+78
-47
lines changed

src/libstd/path.rs

Lines changed: 78 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,15 @@ pub struct PrefixComponent<'a> {
370370
}
371371

372372
impl<'a> PrefixComponent<'a> {
373-
/// The parsed prefix data.
373+
/// Returns the parsed prefix data.
374374
#[stable(feature = "rust1", since = "1.0.0")]
375375
pub fn kind(&self) -> Prefix<'a> {
376376
self.parsed
377377
}
378378

379-
/// The raw `OsStr` slice for this prefix.
379+
/// Returns the raw [`OsStr`] slice for this prefix.
380+
///
381+
/// [`OsStr`]: ../../std/ffi/struct.OsStr.html
380382
#[stable(feature = "rust1", since = "1.0.0")]
381383
pub fn as_os_str(&self) -> &'a OsStr {
382384
self.raw
@@ -446,25 +448,25 @@ pub enum Component<'a> {
446448
#[stable(feature = "rust1", since = "1.0.0")] PrefixComponent<'a>
447449
),
448450

449-
/// The root directory component, appears after any prefix and before anything else
451+
/// The root directory component, appears after any prefix and before anything else.
450452
#[stable(feature = "rust1", since = "1.0.0")]
451453
RootDir,
452454

453-
/// A reference to the current directory, i.e. `.`
455+
/// A reference to the current directory, i.e. `.`.
454456
#[stable(feature = "rust1", since = "1.0.0")]
455457
CurDir,
456458

457-
/// A reference to the parent directory, i.e. `..`
459+
/// A reference to the parent directory, i.e. `..`.
458460
#[stable(feature = "rust1", since = "1.0.0")]
459461
ParentDir,
460462

461-
/// A normal component, i.e. `a` and `b` in `a/b`
463+
/// A normal component, e.g. `a` and `b` in `a/b`.
462464
#[stable(feature = "rust1", since = "1.0.0")]
463465
Normal(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
464466
}
465467

466468
impl<'a> Component<'a> {
467-
/// Extracts the underlying `OsStr` slice.
469+
/// Extracts the underlying [`OsStr`] slice.
468470
///
469471
/// # Examples
470472
///
@@ -475,6 +477,8 @@ impl<'a> Component<'a> {
475477
/// let components: Vec<_> = path.components().map(|comp| comp.as_os_str()).collect();
476478
/// assert_eq!(&components, &[".", "tmp", "foo", "bar.txt"]);
477479
/// ```
480+
///
481+
/// [`OsStr`]: ../../std/ffi/struct.OsStr.html
478482
#[stable(feature = "rust1", since = "1.0.0")]
479483
pub fn as_os_str(self) -> &'a OsStr {
480484
match self {
@@ -494,12 +498,10 @@ impl<'a> AsRef<OsStr> for Component<'a> {
494498
}
495499
}
496500

497-
/// The core iterator giving the components of a path.
501+
/// An interator over the [`Component`]s of a [`Path`].
498502
///
499-
/// See the module documentation for an in-depth explanation of components and
500-
/// their role in the API.
501-
///
502-
/// This `struct` is created by the [`path::Path::components`] method.
503+
/// This `struct` is created by the [`components`] method on [`Path`].
504+
/// See its documentation for more.
503505
///
504506
/// # Examples
505507
///
@@ -513,7 +515,9 @@ impl<'a> AsRef<OsStr> for Component<'a> {
513515
/// }
514516
/// ```
515517
///
516-
/// [`path::Path::components`]: struct.Path.html#method.components
518+
/// [`Component`]: enum.Component.html
519+
/// [`components`]: struct.Path.html#method.components
520+
/// [`Path`]: struct.Path.html
517521
#[derive(Clone)]
518522
#[stable(feature = "rust1", since = "1.0.0")]
519523
pub struct Components<'a> {
@@ -534,9 +538,15 @@ pub struct Components<'a> {
534538
back: State,
535539
}
536540

537-
/// An iterator over the components of a path, as [`OsStr`] slices.
541+
/// An iterator over the [`Component`]s of a [`Path`], as [`OsStr`] slices.
538542
///
543+
/// This `struct` is created by the [`iter`] method on [`Path`].
544+
/// See its documentation for more.
545+
///
546+
/// [`Component`]: enum.Component.html
547+
/// [`iter`]: struct.Path.html#method.iter
539548
/// [`OsStr`]: ../../std/ffi/struct.OsStr.html
549+
/// [`Path`]: struct.Path.html
540550
#[derive(Clone)]
541551
#[stable(feature = "rust1", since = "1.0.0")]
542552
pub struct Iter<'a> {
@@ -762,6 +772,18 @@ impl<'a> fmt::Debug for Iter<'a> {
762772

763773
impl<'a> Iter<'a> {
764774
/// Extracts a slice corresponding to the portion of the path remaining for iteration.
775+
///
776+
/// # Examples
777+
///
778+
/// ```
779+
/// use std::path::Path;
780+
///
781+
/// let mut iter = Path::new("/tmp/foo/bar.txt").iter();
782+
/// iter.next();
783+
/// iter.next();
784+
///
785+
/// assert_eq!(Path::new("foo/bar.txt"), iter.as_path());
786+
/// ```
765787
#[stable(feature = "rust1", since = "1.0.0")]
766788
pub fn as_path(&self) -> &'a Path {
767789
self.inner.as_path()
@@ -1067,9 +1089,10 @@ impl PathBuf {
10671089

10681090
/// Truncate `self` to [`self.parent`].
10691091
///
1070-
/// Returns false and does nothing if [`self.file_name`] is `None`.
1092+
/// Returns `false` and does nothing if [`self.file_name`] is [`None`].
10711093
/// Otherwise, returns `true`.
10721094
///
1095+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
10731096
/// [`self.parent`]: struct.PathBuf.html#method.parent
10741097
/// [`self.file_name`]: struct.PathBuf.html#method.file_name
10751098
///
@@ -1132,10 +1155,11 @@ impl PathBuf {
11321155

11331156
/// Updates [`self.extension`] to `extension`.
11341157
///
1135-
/// If [`self.file_name`] is `None`, does nothing and returns `false`.
1158+
/// Returns `false` and does nothing if [`self.file_name`] is [`None`],
1159+
/// returns `true` and updates the extension otherwise.
11361160
///
1137-
/// Otherwise, returns `true`; if [`self.extension`] is [`None`], the
1138-
/// extension is added; otherwise it is replaced.
1161+
/// If [`self.extension`] is [`None`], the extension is added; otherwise
1162+
/// it is replaced.
11391163
///
11401164
/// [`self.file_name`]: struct.PathBuf.html#method.file_name
11411165
/// [`self.extension`]: struct.PathBuf.html#method.extension
@@ -1195,7 +1219,10 @@ impl PathBuf {
11951219
self.inner
11961220
}
11971221

1198-
/// Converts this `PathBuf` into a boxed `Path`.
1222+
/// Converts this `PathBuf` into a [boxed][`Box`] [`Path`].
1223+
///
1224+
/// [`Box`]: ../../std/boxed/struct.Box.html
1225+
/// [`Path`]: struct.Path.html
11991226
#[unstable(feature = "into_boxed_path", issue = "40380")]
12001227
pub fn into_boxed_path(self) -> Box<Path> {
12011228
unsafe { mem::transmute(self.inner.into_boxed_os_str()) }
@@ -1402,10 +1429,14 @@ pub struct Path {
14021429
inner: OsStr,
14031430
}
14041431

1405-
/// An error returned from the [`Path::strip_prefix`] method indicating that the
1406-
/// prefix was not found in `self`.
1432+
/// An error returned from [`Path::strip_prefix`][`strip_prefix`] if the prefix
1433+
/// was not found.
1434+
///
1435+
/// This `struct` is created by the [`strip_prefix`] method on [`Path`].
1436+
/// See its documentation for more.
14071437
///
1408-
/// [`Path::strip_prefix`]: struct.Path.html#method.strip_prefix
1438+
/// [`strip_prefix`]: struct.Path.html#method.strip_prefix
1439+
/// [`Path`]: struct.Path.html
14091440
#[derive(Debug, Clone, PartialEq, Eq)]
14101441
#[stable(since = "1.7.0", feature = "strip_prefix")]
14111442
pub struct StripPrefixError(());
@@ -1421,7 +1452,7 @@ impl Path {
14211452
os_str_as_u8_slice(&self.inner)
14221453
}
14231454

1424-
/// Directly wrap a string slice as a `Path` slice.
1455+
/// Directly wraps a string slice as a `Path` slice.
14251456
///
14261457
/// This is a cost-free conversion.
14271458
///
@@ -1525,10 +1556,11 @@ impl Path {
15251556
PathBuf::from(self.inner.to_os_string())
15261557
}
15271558

1528-
/// A path is *absolute* if it is independent of the current directory.
1559+
/// Returns `true` if the `Path` is absolute, i.e. if it is independent of
1560+
/// the current directory.
15291561
///
15301562
/// * On Unix, a path is absolute if it starts with the root, so
1531-
/// `is_absolute` and `has_root` are equivalent.
1563+
/// `is_absolute` and [`has_root`] are equivalent.
15321564
///
15331565
/// * On Windows, a path is absolute if it has a prefix and starts with the
15341566
/// root: `c:\windows` is absolute, while `c:temp` and `\temp` are not.
@@ -1540,14 +1572,18 @@ impl Path {
15401572
///
15411573
/// assert!(!Path::new("foo.txt").is_absolute());
15421574
/// ```
1575+
///
1576+
/// [`has_root`]: #method.has_root
15431577
#[stable(feature = "rust1", since = "1.0.0")]
15441578
#[allow(deprecated)]
15451579
pub fn is_absolute(&self) -> bool {
15461580
// FIXME: Remove target_os = "redox" and allow Redox prefixes
15471581
self.has_root() && (cfg!(unix) || cfg!(target_os = "redox") || self.prefix().is_some())
15481582
}
15491583

1550-
/// A path is *relative* if it is not absolute.
1584+
/// Return `false` if the `Path` is relative, i.e. not absolute.
1585+
///
1586+
/// See [`is_absolute`]'s documentation for more details.
15511587
///
15521588
/// # Examples
15531589
///
@@ -1556,6 +1592,8 @@ impl Path {
15561592
///
15571593
/// assert!(Path::new("foo.txt").is_relative());
15581594
/// ```
1595+
///
1596+
/// [`is_absolute`]: #method.is_absolute
15591597
#[stable(feature = "rust1", since = "1.0.0")]
15601598
pub fn is_relative(&self) -> bool {
15611599
!self.is_absolute()
@@ -1565,7 +1603,7 @@ impl Path {
15651603
self.components().prefix
15661604
}
15671605

1568-
/// A path has a root if the body of the path begins with the directory separator.
1606+
/// Returns `true` if the `Path` has a root.
15691607
///
15701608
/// * On Unix, a path has a root if it begins with `/`.
15711609
///
@@ -1586,7 +1624,7 @@ impl Path {
15861624
self.components().has_root()
15871625
}
15881626

1589-
/// The path without its final component, if any.
1627+
/// Returns the `Path` without its final component, if there is one.
15901628
///
15911629
/// Returns [`None`] if the path terminates in a root or prefix.
15921630
///
@@ -1619,9 +1657,9 @@ impl Path {
16191657
})
16201658
}
16211659

1622-
/// The final component of the path, if it is a normal file.
1660+
/// Returns the final component of the `Path`, if it is a normal file.
16231661
///
1624-
/// If the path terminates in `..`, `file_name` will return [`None`].
1662+
/// Returns [`None`] If the path terminates in `..`.
16251663
///
16261664
/// [`None`]: ../../std/option/enum.Option.html#variant.None
16271665
///
@@ -1631,18 +1669,7 @@ impl Path {
16311669
/// use std::path::Path;
16321670
/// use std::ffi::OsStr;
16331671
///
1634-
/// let path = Path::new("foo.txt");
1635-
/// let os_str = OsStr::new("foo.txt");
1636-
///
1637-
/// assert_eq!(Some(os_str), path.file_name());
1638-
/// ```
1639-
///
1640-
/// # Other examples
1641-
///
1642-
/// ```
1643-
/// use std::path::Path;
1644-
/// use std::ffi::OsStr;
1645-
///
1672+
/// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt").file_name());
16461673
/// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.").file_name());
16471674
/// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.//").file_name());
16481675
/// assert_eq!(None, Path::new("foo.txt/..").file_name());
@@ -1869,7 +1896,7 @@ impl Path {
18691896
buf
18701897
}
18711898

1872-
/// Produce an iterator over the components of the path.
1899+
/// Produces an iterator over the components of the path.
18731900
///
18741901
/// # Examples
18751902
///
@@ -1896,7 +1923,7 @@ impl Path {
18961923
}
18971924
}
18981925

1899-
/// Produce an iterator over the path's components viewed as [`OsStr`] slices.
1926+
/// Produces an iterator over the path's components viewed as [`OsStr`] slices.
19001927
///
19011928
/// [`OsStr`]: ../ffi/struct.OsStr.html
19021929
///
@@ -1936,7 +1963,7 @@ impl Path {
19361963
Display { path: self }
19371964
}
19381965

1939-
/// Query the file system to get information about a file, directory, etc.
1966+
/// Queries the file system to get information about a file, directory, etc.
19401967
///
19411968
/// This function will traverse symbolic links to query information about the
19421969
/// destination file.
@@ -1959,7 +1986,7 @@ impl Path {
19591986
fs::metadata(self)
19601987
}
19611988

1962-
/// Query the metadata about a file without following symlinks.
1989+
/// Queries the metadata about a file without following symlinks.
19631990
///
19641991
/// This is an alias to [`fs::symlink_metadata`].
19651992
///
@@ -2096,7 +2123,11 @@ impl Path {
20962123
fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
20972124
}
20982125

2099-
/// Converts a `Box<Path>` into a `PathBuf` without copying or allocating.
2126+
/// Converts a [`Box<Path>`][`Box`] into a [`PathBuf`] without copying or
2127+
/// allocating.
2128+
///
2129+
/// [`Box`]: ../../std/boxed/struct.Box.html
2130+
/// [`PathBuf`]: struct.PathBuf.html
21002131
#[unstable(feature = "into_boxed_path", issue = "40380")]
21012132
pub fn into_path_buf(self: Box<Path>) -> PathBuf {
21022133
let inner: Box<OsStr> = unsafe { mem::transmute(self) };

0 commit comments

Comments
 (0)