Skip to content

Commit 8c21b60

Browse files
author
lukaramu
committed
Expand and add examples to std::path::{Prefix, PrefixComponent}'s docs
Part of #29368.
1 parent 1afe77f commit 8c21b60

File tree

1 file changed

+106
-10
lines changed

1 file changed

+106
-10
lines changed

src/libstd/path.rs

Lines changed: 106 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,34 +145,79 @@ use sys::path::{is_sep_byte, is_verbatim_sep, MAIN_SEP_STR, parse_prefix};
145145

146146
/// Path prefixes (Windows only).
147147
///
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+
/// ```
153182
#[derive(Copy, Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
154183
#[stable(feature = "rust1", since = "1.0.0")]
155184
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.
157189
#[stable(feature = "rust1", since = "1.0.0")]
158190
Verbatim(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
159191

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.
161197
#[stable(feature = "rust1", since = "1.0.0")]
162198
VerbatimUNC(
163199
#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
164200
#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
165201
),
166202

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 `:\`.
168207
#[stable(feature = "rust1", since = "1.0.0")]
169208
VerbatimDisk(#[stable(feature = "rust1", since = "1.0.0")] u8),
170209

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.
172214
#[stable(feature = "rust1", since = "1.0.0")]
173215
DeviceNS(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
174216

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.
176221
#[stable(feature = "rust1", since = "1.0.0")]
177222
UNC(
178223
#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
@@ -217,6 +262,20 @@ impl<'a> Prefix<'a> {
217262
}
218263

219264
/// 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+
/// ```
220279
#[inline]
221280
#[stable(feature = "rust1", since = "1.0.0")]
222281
pub fn is_verbatim(&self) -> bool {
@@ -358,7 +417,39 @@ enum State {
358417

359418
/// A Windows path prefix, e.g. `C:` or `\\server\share`.
360419
///
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+
///
361427
/// 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
362453
#[stable(feature = "rust1", since = "1.0.0")]
363454
#[derive(Copy, Clone, Eq, Debug)]
364455
pub struct PrefixComponent<'a> {
@@ -371,6 +462,11 @@ pub struct PrefixComponent<'a> {
371462

372463
impl<'a> PrefixComponent<'a> {
373464
/// 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
374470
#[stable(feature = "rust1", since = "1.0.0")]
375471
pub fn kind(&self) -> Prefix<'a> {
376472
self.parsed

0 commit comments

Comments
 (0)