1
1
use std:: ffi:: OsStr ;
2
2
3
- use crate :: path:: PathBuf ;
3
+ use crate :: path:: { Components , PathBuf } ;
4
4
use crate :: { fs, io} ;
5
5
6
6
/// This struct is an async version of [`std::path::Path`].
@@ -28,7 +28,7 @@ impl Path {
28
28
/// # Examples
29
29
///
30
30
/// ```no_run
31
- /// use crate ::path::{Path, PathBuf};
31
+ /// use async_std ::path::{Path, PathBuf};
32
32
///
33
33
/// let path = Path::new("/foo/test/../test/bar.rs");
34
34
/// assert_eq!(path.canonicalize().unwrap(), PathBuf::from("/foo/test/bar.rs"));
@@ -37,22 +37,61 @@ impl Path {
37
37
fs:: canonicalize ( self ) . await
38
38
}
39
39
40
+ /// Produces an iterator over the [`Component`]s of the path.
41
+ ///
42
+ /// When parsing the path, there is a small amount of normalization:
43
+ ///
44
+ /// * Repeated separators are ignored, so `a/b` and `a//b` both have
45
+ /// `a` and `b` as components.
46
+ ///
47
+ /// * Occurrences of `.` are normalized away, except if they are at the
48
+ /// beginning of the path. For example, `a/./b`, `a/b/`, `a/b/.` and
49
+ /// `a/b` all have `a` and `b` as components, but `./a/b` starts with
50
+ /// an additional [`CurDir`] component.
51
+ ///
52
+ /// * A trailing slash is normalized away, `/a/b` and `/a/b/` are equivalent.
53
+ ///
54
+ /// Note that no other normalization takes place; in particular, `a/c`
55
+ /// and `a/b/../c` are distinct, to account for the possibility that `b`
56
+ /// is a symbolic link (so its parent isn't `a`).
57
+ ///
58
+ /// # Examples
59
+ ///
60
+ /// ```
61
+ /// use async_std::path::{Path, Component};
62
+ /// use std::ffi::OsStr;
63
+ ///
64
+ /// let mut components = Path::new("/tmp/foo.txt").components();
65
+ ///
66
+ /// assert_eq!(components.next(), Some(Component::RootDir));
67
+ /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("tmp"))));
68
+ /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("foo.txt"))));
69
+ /// assert_eq!(components.next(), None)
70
+ /// ```
71
+ ///
72
+ /// [`Component`]: enum.Component.html
73
+ /// [`CurDir`]: enum.Component.html#variant.CurDir
74
+ pub fn components ( & self ) -> Components < ' _ > {
75
+ let path: & std:: path:: Path = self . into ( ) ;
76
+ path. components ( )
77
+ }
78
+
40
79
/// Directly wraps a string slice as a `Path` slice.
41
80
///
42
81
/// This is a cost-free conversion.
43
82
///
44
83
/// # Examples
45
84
///
46
85
/// ```
47
- /// use crate ::path::Path;
86
+ /// use async_std ::path::Path;
48
87
///
49
88
/// Path::new("foo.txt");
50
89
/// ```
51
90
///
52
91
/// You can create `Path`s from `String`s, or even other `Path`s:
53
92
///
54
93
/// ```
55
- /// use crate ::path::Path;
94
+ /// use async_std ::path::Path;
56
95
///
57
96
/// let string = String::from("foo.txt");
58
97
/// let from_string = Path::new(&string);
@@ -70,7 +109,7 @@ impl Path {
70
109
/// # Examples
71
110
///
72
111
/// ```
73
- /// use crate ::path::{Path, PathBuf};
112
+ /// use async_std ::path::{Path, PathBuf};
74
113
///
75
114
/// let path_buf = Path::new("foo.txt").to_path_buf();
76
115
/// assert_eq!(path_buf, PathBuf::from("foo.txt"));
@@ -98,8 +137,40 @@ impl AsRef<Path> for Path {
98
137
}
99
138
}
100
139
140
+ impl AsRef < Path > for OsStr {
141
+ fn as_ref ( & self ) -> & Path {
142
+ Path :: new ( self )
143
+ }
144
+ }
145
+
146
+ impl AsRef < Path > for str {
147
+ fn as_ref ( & self ) -> & Path {
148
+ Path :: new ( self )
149
+ }
150
+ }
151
+
101
152
impl std:: fmt:: Debug for Path {
102
153
fn fmt ( & self , formatter : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
103
154
std:: fmt:: Debug :: fmt ( & self . inner , formatter)
104
155
}
105
156
}
157
+
158
+ impl std:: cmp:: PartialEq for Path {
159
+ fn eq ( & self , other : & Path ) -> bool {
160
+ self . components ( ) . eq ( other. components ( ) )
161
+ }
162
+ }
163
+
164
+ impl std:: cmp:: Eq for Path { }
165
+
166
+ impl std:: cmp:: PartialOrd for Path {
167
+ fn partial_cmp ( & self , other : & Path ) -> Option < std:: cmp:: Ordering > {
168
+ self . components ( ) . partial_cmp ( other. components ( ) )
169
+ }
170
+ }
171
+
172
+ impl std:: cmp:: Ord for Path {
173
+ fn cmp ( & self , other : & Path ) -> std:: cmp:: Ordering {
174
+ self . components ( ) . cmp ( other. components ( ) )
175
+ }
176
+ }
0 commit comments