@@ -145,34 +145,79 @@ use sys::path::{is_sep_byte, is_verbatim_sep, MAIN_SEP_STR, parse_prefix};
145
145
146
146
/// Path prefixes (Windows only).
147
147
///
148
- /// Windows uses a variety of path styles, including references to drive
149
- /// volumes (like `C:`), network shared folders (like `\\server\share`) and
150
- /// others. In addition, some path prefixes are "verbatim", in which case
151
- /// `/` is *not* treated as a separator and essentially no normalization is
152
- /// performed.
148
+ /// Windows uses a variety of path prefix styles, including references to drive
149
+ /// volumes (like `C:`), network shared folders (like `\\server\share`), and
150
+ /// others. In addition, some path prefixes are "verbatim" (i.e. prefixed with
151
+ /// `\\?\`, in which case `/` is *not* treated as a separator and essentially no
152
+ /// normalization is performed.
153
+ ///
154
+ /// # Examples
155
+ ///
156
+ /// ```
157
+ /// use std::path::{Component, Path, Prefix};
158
+ /// use std::path::Prefix::*;
159
+ /// use std::ffi::OsStr;
160
+ ///
161
+ /// fn get_path_prefix(s: &str) -> Prefix {
162
+ /// let path = Path::new(s);
163
+ /// match path.components().next().unwrap() {
164
+ /// Component::Prefix(prefix_component) => prefix_component.kind(),
165
+ /// _ => panic!(),
166
+ /// }
167
+ /// }
168
+ ///
169
+ /// # if cfg!(windows) {
170
+ /// assert_eq!(Verbatim(OsStr::new("pictures")),
171
+ /// get_path_prefix(r"\\?\pictures\kittens"));
172
+ /// assert_eq!(VerbatimUNC(OsStr::new("server"), OsStr::new("share")),
173
+ /// get_path_prefix(r"\\?\UNC\server\share"));
174
+ /// assert_eq!(VerbatimDisk('C' as u8), get_path_prefix(r"\\?\c:\"));
175
+ /// assert_eq!(DeviceNS(OsStr::new("BrainInterface")),
176
+ /// get_path_prefix(r"\\.\BrainInterface"));
177
+ /// assert_eq!(UNC(OsStr::new("server"), OsStr::new("share")),
178
+ /// get_path_prefix(r"\\server\share"));
179
+ /// assert_eq!(Disk('C' as u8), get_path_prefix(r"C:\Users\Rust\Pictures\Ferris"));
180
+ /// # }
181
+ /// ```
153
182
#[ derive( Copy , Clone , Debug , Hash , PartialOrd , Ord , PartialEq , Eq ) ]
154
183
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
155
184
pub enum Prefix < ' a > {
156
- /// Prefix `\\?\`, together with the given component immediately following it.
185
+ /// Verbatim prefix, e.g. `\\?\cat_pics`.
186
+ ///
187
+ /// Verbatim prefixes consist of `\\?\` immediately followed by the given
188
+ /// component.
157
189
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
158
190
Verbatim ( #[ stable( feature = "rust1" , since = "1.0.0" ) ] & ' a OsStr ) ,
159
191
160
- /// Prefix `\\?\UNC\`, with the "server" and "share" components following it.
192
+ /// Verbatim prefix using Windows' _**U**niform **N**aming **C**onvention_,
193
+ /// e.g. `\\?\UNC\server\share`.
194
+ ///
195
+ /// Verbatim UNC prefixes consist of `\\?\UNC\` immediately followed by the
196
+ /// server's hostname and a share name.
161
197
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
162
198
VerbatimUNC (
163
199
#[ stable( feature = "rust1" , since = "1.0.0" ) ] & ' a OsStr ,
164
200
#[ stable( feature = "rust1" , since = "1.0.0" ) ] & ' a OsStr ,
165
201
) ,
166
202
167
- /// Prefix like `\\?\C:\`, for the given drive letter
203
+ /// Verbatim disk prefix, e.g. `\\?\C:\`.
204
+ ///
205
+ /// Verbatim disk prefixes consist of `\\?\` immediately followed by the
206
+ /// drive letter and `:\`.
168
207
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
169
208
VerbatimDisk ( #[ stable( feature = "rust1" , since = "1.0.0" ) ] u8 ) ,
170
209
171
- /// Prefix `\\.\`, together with the given component immediately following it.
210
+ /// Device namespace prefix, e.g. `\\.\COM42`.
211
+ ///
212
+ /// Device namespace prefixes consist of `\\.\` immediately followed by the
213
+ /// device name.
172
214
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
173
215
DeviceNS ( #[ stable( feature = "rust1" , since = "1.0.0" ) ] & ' a OsStr ) ,
174
216
175
- /// Prefix `\\server\share`, with the given "server" and "share" components.
217
+ /// Prefix using Windows' _**U**niform **N**aming **C**onvention_, e.g.
218
+ /// `\\server\share`.
219
+ ///
220
+ /// UNC prefixes consist of the server's hostname and a share name.
176
221
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
177
222
UNC (
178
223
#[ stable( feature = "rust1" , since = "1.0.0" ) ] & ' a OsStr ,
@@ -217,6 +262,20 @@ impl<'a> Prefix<'a> {
217
262
}
218
263
219
264
/// Determines if the prefix is verbatim, i.e. begins with `\\?\`.
265
+ ///
266
+ /// # Examples
267
+ ///
268
+ /// ```
269
+ /// use std::path::Prefix::*;
270
+ /// use std::ffi::OsStr;
271
+ ///
272
+ /// assert!(Verbatim(OsStr::new("pictures")).is_verbatim());
273
+ /// assert!(VerbatimUNC(OsStr::new("server"), OsStr::new("share")).is_verbatim());
274
+ /// assert!(VerbatimDisk('C' as u8).is_verbatim());
275
+ /// assert!(!DeviceNS(OsStr::new("BrainInterface")).is_verbatim());
276
+ /// assert!(!UNC(OsStr::new("server"), OsStr::new("share")).is_verbatim());
277
+ /// assert!(!Disk('C' as u8).is_verbatim());
278
+ /// ```
220
279
#[ inline]
221
280
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
222
281
pub fn is_verbatim ( & self ) -> bool {
@@ -358,7 +417,39 @@ enum State {
358
417
359
418
/// A Windows path prefix, e.g. `C:` or `\\server\share`.
360
419
///
420
+ /// In addition to the parsed [`Prefix`] information returned by [`kind`],
421
+ /// `PrefixComponent` also holds the raw and unparsed [`OsStr`] slice,
422
+ /// returned by [`as_os_str`].
423
+ ///
424
+ /// Instances of this `struct` can be obtained by matching against the
425
+ /// [`Prefix` variant] on [`Component`].
426
+ ///
361
427
/// Does not occur on Unix.
428
+ ///
429
+ /// # Examples
430
+ ///
431
+ /// ```
432
+ /// # if cfg!(windows) {
433
+ /// use std::path::{Component, Path, Prefix};
434
+ /// use std::ffi::OsStr;
435
+ ///
436
+ /// let path = Path::new(r"c:\you\later\");
437
+ /// match path.components().next().unwrap() {
438
+ /// Component::Prefix(prefix_component) => {
439
+ /// assert_eq!(Prefix::Disk('C' as u8), prefix_component.kind());
440
+ /// assert_eq!(OsStr::new("c:"), prefix_component.as_os_str());
441
+ /// }
442
+ /// _ => unreachable!(),
443
+ /// }
444
+ /// # }
445
+ /// ```
446
+ ///
447
+ /// [`as_os_str`]: #method.as_os_str
448
+ /// [`Component`]: enum.Component.html
449
+ /// [`kind`]: #method.kind
450
+ /// [`OsStr`]: ../../std/ffi/struct.OsStr.html
451
+ /// [`Prefix` variant]: enum.Component.html#variant.Prefix
452
+ /// [`Prefix`]: enum.Prefix.html
362
453
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
363
454
#[ derive( Copy , Clone , Eq , Debug ) ]
364
455
pub struct PrefixComponent < ' a > {
@@ -371,6 +462,11 @@ pub struct PrefixComponent<'a> {
371
462
372
463
impl < ' a > PrefixComponent < ' a > {
373
464
/// Returns the parsed prefix data.
465
+ ///
466
+ /// See [`Prefix`]'s documentation for more information on the different
467
+ /// kinds of prefixes.
468
+ ///
469
+ /// [`Prefix`]: enum.Prefix.html
374
470
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
375
471
pub fn kind ( & self ) -> Prefix < ' a > {
376
472
self . parsed
0 commit comments