10
10
11
11
//! Cross-platform path manipulation.
12
12
//!
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`]
14
14
//! and [`str`]), for working with paths abstractly. These types are thin wrappers
15
15
//! around [`OsString`] and [`OsStr`] respectively, meaning that they work directly
16
16
//! on strings according to the local platform's path syntax.
17
17
//!
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
+ //!
18
25
//! ## Simple usage
19
26
//!
20
27
//! Path manipulation includes both parsing components from slices and building
50
57
//! path.set_extension("dll");
51
58
//! ```
52
59
//!
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
107
62
//! [`PathBuf`]: ../../std/path/struct.PathBuf.html
108
63
//! [`Path`]: ../../std/path/struct.Path.html
64
+ //! [`push`]: ../../std/path/struct.PathBuf.html#method.push
109
65
//! [`String`]: ../../std/string/struct.String.html
110
66
//! [`str`]: ../../std/primitive.str.html
111
67
//! [`OsString`]: ../../std/ffi/struct.OsString.html
@@ -143,7 +99,7 @@ use sys::path::{is_sep_byte, is_verbatim_sep, MAIN_SEP_STR, parse_prefix};
143
99
// Windows Prefixes
144
100
////////////////////////////////////////////////////////////////////////////////
145
101
146
- /// Path prefixes (Windows only) .
102
+ /// Windows path prefixes, e.g. `C:` or `\\server\share` .
147
103
///
148
104
/// Windows uses a variety of path prefix styles, including references to drive
149
105
/// volumes (like `C:`), network shared folders (like `\\server\share`), and
@@ -415,7 +371,8 @@ enum State {
415
371
Done = 3 ,
416
372
}
417
373
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.
419
376
///
420
377
/// In addition to the parsed [`Prefix`] information returned by [`kind`],
421
378
/// `PrefixComponent` also holds the raw and unparsed [`OsStr`] slice,
@@ -511,11 +468,11 @@ impl<'a> Hash for PrefixComponent<'a> {
511
468
512
469
/// A single component of a path.
513
470
///
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 `\`) .
516
473
///
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`] .
519
476
///
520
477
/// # Examples
521
478
///
@@ -532,19 +489,28 @@ impl<'a> Hash for PrefixComponent<'a> {
532
489
/// ]);
533
490
/// ```
534
491
///
535
- /// [`path::Components`]: struct.Components.html
492
+ /// [`Components`]: struct.Components.html
493
+ /// [`Path`]: struct.Path.html
494
+ /// [`Path::components`]: struct.Path.html#method.components
536
495
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug ) ]
537
496
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
538
497
pub enum Component < ' a > {
539
498
/// A Windows path prefix, e.g. `C:` or `\\server\share`.
540
499
///
500
+ /// There is a large variety of prefix types, see [`Prefix`]'s documentation
501
+ /// for more.
502
+ ///
541
503
/// Does not occur on Unix.
504
+ ///
505
+ /// [`Prefix`]: enum.Prefix.html
542
506
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
543
507
Prefix (
544
508
#[ stable( feature = "rust1" , since = "1.0.0" ) ] PrefixComponent < ' a >
545
509
) ,
546
510
547
511
/// 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.
548
514
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
549
515
RootDir ,
550
516
@@ -557,6 +523,9 @@ pub enum Component<'a> {
557
523
ParentDir ,
558
524
559
525
/// 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.
560
529
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
561
530
Normal ( #[ stable( feature = "rust1" , since = "1.0.0" ) ] & ' a OsStr ) ,
562
531
}
@@ -1992,7 +1961,21 @@ impl Path {
1992
1961
buf
1993
1962
}
1994
1963
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`).
1996
1979
///
1997
1980
/// # Examples
1998
1981
///
@@ -2007,6 +1990,9 @@ impl Path {
2007
1990
/// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("foo.txt"))));
2008
1991
/// assert_eq!(components.next(), None)
2009
1992
/// ```
1993
+ ///
1994
+ /// [`Component`]: enum.Component.html
1995
+ /// [`CurDir`]: enum.Component.html#variant.CurDir
2010
1996
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2011
1997
pub fn components ( & self ) -> Components {
2012
1998
let prefix = parse_prefix ( self . as_os_str ( ) ) ;
@@ -2019,8 +2005,13 @@ impl Path {
2019
2005
}
2020
2006
}
2021
2007
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`].
2023
2013
///
2014
+ /// [`components`]: #method.components
2024
2015
/// [`OsStr`]: ../ffi/struct.OsStr.html
2025
2016
///
2026
2017
/// # Examples
0 commit comments