Skip to content

Commit 51ca5fe

Browse files
author
lukaramu
committed
Restructure and redistribute std::path's module docs
Part of #29368. * Added a new summary paragraph about std::path's parsing facilities * Slightly exanded `Component`'s docs * removed the now redundant section on component types from the module docs * moved the section on path normalization during parsing to the docs on `Path::components` * Clarified difference between `Prefix` and `PrefixComponent` in their respecive summary sentences
1 parent 8c21b60 commit 51ca5fe

File tree

1 file changed

+55
-64
lines changed

1 file changed

+55
-64
lines changed

src/libstd/path.rs

Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@
1010

1111
//! Cross-platform path manipulation.
1212
//!
13-
//! This module provides two types, [`PathBuf`] and [`Path`][`Path`] (akin to [`String`]
13+
//! This module provides two types, [`PathBuf`] and [`Path`] (akin to [`String`]
1414
//! and [`str`]), for working with paths abstractly. These types are thin wrappers
1515
//! around [`OsString`] and [`OsStr`] respectively, meaning that they work directly
1616
//! on strings according to the local platform's path syntax.
1717
//!
18+
//! Paths can be parsed into [`Component`]s by iterating over the structure
19+
//! returned by the [`components`] method on [`Path`]. [`Component`]s roughly
20+
//! correspond to the substrings between path separators (`/` or `\`). You can
21+
//! reconstruct an equivalent path from components with the [`push`] method on
22+
//! [`PathBuf`]; note that the paths may differ syntactically by the
23+
//! normalization described in the documentation for the [`components`] method.
24+
//!
1825
//! ## Simple usage
1926
//!
2027
//! Path manipulation includes both parsing components from slices and building
@@ -50,62 +57,11 @@
5057
//! path.set_extension("dll");
5158
//! ```
5259
//!
53-
//! ## Path components and normalization
54-
//!
55-
//! The path APIs are built around the notion of "components", which roughly
56-
//! correspond to the substrings between path separators (`/` and, on Windows,
57-
//! `\`). The APIs for path parsing are largely specified in terms of the path's
58-
//! components, so it's important to clearly understand how those are
59-
//! determined.
60-
//!
61-
//! A path can always be reconstructed into an *equivalent* path by
62-
//! putting together its components via `push`. Syntactically, the
63-
//! paths may differ by the normalization described below.
64-
//!
65-
//! ### Component types
66-
//!
67-
//! Components come in several types:
68-
//!
69-
//! * Normal components are the default: standard references to files or
70-
//! directories. The path `a/b` has two normal components, `a` and `b`.
71-
//!
72-
//! * Current directory components represent the `.` character. For example,
73-
//! `./a` has a current directory component and a normal component `a`.
74-
//!
75-
//! * The root directory component represents a separator that designates
76-
//! starting from root. For example, `/a/b` has a root directory component
77-
//! followed by normal components `a` and `b`.
78-
//!
79-
//! On Windows, an additional component type comes into play:
80-
//!
81-
//! * Prefix components, of which there is a large variety. For example, `C:`
82-
//! and `\\server\share` are prefixes. The path `C:windows` has a prefix
83-
//! component `C:` and a normal component `windows`; the path `C:\windows` has a
84-
//! prefix component `C:`, a root directory component, and a normal component
85-
//! `windows`.
86-
//!
87-
//! ### Normalization
88-
//!
89-
//! Aside from splitting on the separator(s), there is a small amount of
90-
//! "normalization":
91-
//!
92-
//! * Repeated separators are ignored: `a/b` and `a//b` both have components `a`
93-
//! and `b`.
94-
//!
95-
//! * Occurrences of `.` are normalized away, *except* if they are at
96-
//! the beginning of the path (in which case they are often meaningful
97-
//! in terms of path searching). So, for example, `a/./b`, `a/b/`,
98-
//! `/a/b/.` and `a/b` all have components `a` and `b`, but `./a/b`
99-
//! has a leading current directory component.
100-
//!
101-
//! No other normalization takes place by default. In particular,
102-
//! `a/c` and `a/b/../c` are distinct, to account for the possibility
103-
//! that `b` is a symbolic link (so its parent isn't `a`). Further
104-
//! normalization is possible to build on top of the components APIs,
105-
//! and will be included in this library in the near future.
106-
//!
60+
//! [`Component`]: ../../std/path/enum.Component.html
61+
//! [`components`]: ../../std/path/struct.Path.html#method.components
10762
//! [`PathBuf`]: ../../std/path/struct.PathBuf.html
10863
//! [`Path`]: ../../std/path/struct.Path.html
64+
//! [`push`]: ../../std/path/struct.PathBuf.html#method.push
10965
//! [`String`]: ../../std/string/struct.String.html
11066
//! [`str`]: ../../std/primitive.str.html
11167
//! [`OsString`]: ../../std/ffi/struct.OsString.html
@@ -143,7 +99,7 @@ use sys::path::{is_sep_byte, is_verbatim_sep, MAIN_SEP_STR, parse_prefix};
14399
// Windows Prefixes
144100
////////////////////////////////////////////////////////////////////////////////
145101

146-
/// Path prefixes (Windows only).
102+
/// Windows path prefixes, e.g. `C:` or `\\server\share`.
147103
///
148104
/// Windows uses a variety of path prefix styles, including references to drive
149105
/// volumes (like `C:`), network shared folders (like `\\server\share`), and
@@ -415,7 +371,8 @@ enum State {
415371
Done = 3,
416372
}
417373

418-
/// A Windows path prefix, e.g. `C:` or `\\server\share`.
374+
/// A structure wrapping a Windows path prefix as well as its unparsed string
375+
/// representation.
419376
///
420377
/// In addition to the parsed [`Prefix`] information returned by [`kind`],
421378
/// `PrefixComponent` also holds the raw and unparsed [`OsStr`] slice,
@@ -511,11 +468,11 @@ impl<'a> Hash for PrefixComponent<'a> {
511468

512469
/// A single component of a path.
513470
///
514-
/// See the module documentation for an in-depth explanation of components and
515-
/// their role in the API.
471+
/// A `Component` roughtly corresponds to a substring between path separators
472+
/// (`/` or `\`).
516473
///
517-
/// This `enum` is created from iterating over the [`path::Components`]
518-
/// `struct`.
474+
/// This `enum` is created by iterating over [`Components`], which in turn is
475+
/// created by the [`components`][`Path::components`] method on [`Path`].
519476
///
520477
/// # Examples
521478
///
@@ -532,19 +489,28 @@ impl<'a> Hash for PrefixComponent<'a> {
532489
/// ]);
533490
/// ```
534491
///
535-
/// [`path::Components`]: struct.Components.html
492+
/// [`Components`]: struct.Components.html
493+
/// [`Path`]: struct.Path.html
494+
/// [`Path::components`]: struct.Path.html#method.components
536495
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
537496
#[stable(feature = "rust1", since = "1.0.0")]
538497
pub enum Component<'a> {
539498
/// A Windows path prefix, e.g. `C:` or `\\server\share`.
540499
///
500+
/// There is a large variety of prefix types, see [`Prefix`]'s documentation
501+
/// for more.
502+
///
541503
/// Does not occur on Unix.
504+
///
505+
/// [`Prefix`]: enum.Prefix.html
542506
#[stable(feature = "rust1", since = "1.0.0")]
543507
Prefix(
544508
#[stable(feature = "rust1", since = "1.0.0")] PrefixComponent<'a>
545509
),
546510

547511
/// The root directory component, appears after any prefix and before anything else.
512+
///
513+
/// It represents a deperator that designates that a path starts from root.
548514
#[stable(feature = "rust1", since = "1.0.0")]
549515
RootDir,
550516

@@ -557,6 +523,9 @@ pub enum Component<'a> {
557523
ParentDir,
558524

559525
/// A normal component, e.g. `a` and `b` in `a/b`.
526+
///
527+
/// This variant is the most common one, it represents references to files
528+
/// or directories.
560529
#[stable(feature = "rust1", since = "1.0.0")]
561530
Normal(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
562531
}
@@ -1992,7 +1961,21 @@ impl Path {
19921961
buf
19931962
}
19941963

1995-
/// Produces an iterator over the components of the path.
1964+
/// Produces an iterator over the [`Component`]s of the path.
1965+
///
1966+
/// When parsing the path, there is a small amount of normalization:
1967+
///
1968+
/// * Repeated seperators are ignored, so `a/b` and `a//b` both have
1969+
/// `a` and `b` as components.
1970+
///
1971+
/// * Occurentces of `.` are normalized away, exept if they are at the
1972+
/// beginning of the path. For example, `a/./b`, `a/b/`, `a/b/.` and
1973+
/// `a/b` all have `a` and `b` as components, but `./a/b` starts with
1974+
/// an additional [`CurDir`] component.
1975+
///
1976+
/// Note that no other normalization takes place; in particular, `a/c`
1977+
/// and `a/b/../c` are distinct, to account for the possibility that `b`
1978+
/// is a symbolic link (so its parent isn't `a`).
19961979
///
19971980
/// # Examples
19981981
///
@@ -2007,6 +1990,9 @@ impl Path {
20071990
/// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("foo.txt"))));
20081991
/// assert_eq!(components.next(), None)
20091992
/// ```
1993+
///
1994+
/// [`Component`]: enum.Component.html
1995+
/// [`CurDir`]: enum.Component.html#variant.CurDir
20101996
#[stable(feature = "rust1", since = "1.0.0")]
20111997
pub fn components(&self) -> Components {
20121998
let prefix = parse_prefix(self.as_os_str());
@@ -2019,8 +2005,13 @@ impl Path {
20192005
}
20202006
}
20212007

2022-
/// Produces an iterator over the path's components viewed as [`OsStr`] slices.
2008+
/// Produces an iterator over the path's components viewed as [`OsStr`]
2009+
/// slices.
2010+
///
2011+
/// For more information about the particulars of how the path is separated
2012+
/// into components, see [`components`].
20232013
///
2014+
/// [`components`]: #method.components
20242015
/// [`OsStr`]: ../ffi/struct.OsStr.html
20252016
///
20262017
/// # Examples

0 commit comments

Comments
 (0)