std/path.rs
1//! Cross-platform path manipulation.
2//!
3//! This module provides two types, [`PathBuf`] and [`Path`] (akin to [`String`]
4//! and [`str`]), for working with paths abstractly. These types are thin wrappers
5//! around [`OsString`] and [`OsStr`] respectively, meaning that they work directly
6//! on strings according to the local platform's path syntax.
7//!
8//! Paths can be parsed into [`Component`]s by iterating over the structure
9//! returned by the [`components`] method on [`Path`]. [`Component`]s roughly
10//! correspond to the substrings between path separators (`/` or `\`). You can
11//! reconstruct an equivalent path from components with the [`push`] method on
12//! [`PathBuf`]; note that the paths may differ syntactically by the
13//! normalization described in the documentation for the [`components`] method.
14//!
15//! ## Case sensitivity
16//!
17//! Unless otherwise indicated path methods that do not access the filesystem,
18//! such as [`Path::starts_with`] and [`Path::ends_with`], are case sensitive no
19//! matter the platform or filesystem. An exception to this is made for Windows
20//! drive letters.
21//!
22//! ## Simple usage
23//!
24//! Path manipulation includes both parsing components from slices and building
25//! new owned paths.
26//!
27//! To parse a path, you can create a [`Path`] slice from a [`str`]
28//! slice and start asking questions:
29//!
30//! ```
31//! use std::path::Path;
32//! use std::ffi::OsStr;
33//!
34//! let path = Path::new("/tmp/foo/bar.txt");
35//!
36//! let parent = path.parent();
37//! assert_eq!(parent, Some(Path::new("/tmp/foo")));
38//!
39//! let file_stem = path.file_stem();
40//! assert_eq!(file_stem, Some(OsStr::new("bar")));
41//!
42//! let extension = path.extension();
43//! assert_eq!(extension, Some(OsStr::new("txt")));
44//! ```
45//!
46//! To build or modify paths, use [`PathBuf`]:
47//!
48//! ```
49//! use std::path::PathBuf;
50//!
51//! // This way works...
52//! let mut path = PathBuf::from("c:\\");
53//!
54//! path.push("windows");
55//! path.push("system32");
56//!
57//! path.set_extension("dll");
58//!
59//! // ... but push is best used if you don't know everything up
60//! // front. If you do, this way is better:
61//! let path: PathBuf = ["c:\\", "windows", "system32.dll"].iter().collect();
62//! ```
63//!
64//! [`components`]: Path::components
65//! [`push`]: PathBuf::push
66
67#![stable(feature = "rust1", since = "1.0.0")]
68#![deny(unsafe_op_in_unsafe_fn)]
69
70use core::clone::CloneToUninit;
71
72use crate::borrow::{Borrow, Cow};
73use crate::collections::TryReserveError;
74use crate::error::Error;
75use crate::ffi::{OsStr, OsString, os_str};
76use crate::hash::{Hash, Hasher};
77use crate::iter::FusedIterator;
78use crate::ops::{self, Deref};
79use crate::rc::Rc;
80use crate::str::FromStr;
81use crate::sync::Arc;
82use crate::sys::path::{HAS_PREFIXES, MAIN_SEP_STR, is_sep_byte, is_verbatim_sep, parse_prefix};
83use crate::{cmp, fmt, fs, io, sys};
84
85////////////////////////////////////////////////////////////////////////////////
86// GENERAL NOTES
87////////////////////////////////////////////////////////////////////////////////
88//
89// Parsing in this module is done by directly transmuting OsStr to [u8] slices,
90// taking advantage of the fact that OsStr always encodes ASCII characters
91// as-is. Eventually, this transmutation should be replaced by direct uses of
92// OsStr APIs for parsing, but it will take a while for those to become
93// available.
94
95////////////////////////////////////////////////////////////////////////////////
96// Windows Prefixes
97////////////////////////////////////////////////////////////////////////////////
98
99/// Windows path prefixes, e.g., `C:` or `\\server\share`.
100///
101/// Windows uses a variety of path prefix styles, including references to drive
102/// volumes (like `C:`), network shared folders (like `\\server\share`), and
103/// others. In addition, some path prefixes are "verbatim" (i.e., prefixed with
104/// `\\?\`), in which case `/` is *not* treated as a separator and essentially
105/// no normalization is performed.
106///
107/// # Examples
108///
109/// ```
110/// use std::path::{Component, Path, Prefix};
111/// use std::path::Prefix::*;
112/// use std::ffi::OsStr;
113///
114/// fn get_path_prefix(s: &str) -> Prefix<'_> {
115/// let path = Path::new(s);
116/// match path.components().next().unwrap() {
117/// Component::Prefix(prefix_component) => prefix_component.kind(),
118/// _ => panic!(),
119/// }
120/// }
121///
122/// # if cfg!(windows) {
123/// assert_eq!(Verbatim(OsStr::new("pictures")),
124/// get_path_prefix(r"\\?\pictures\kittens"));
125/// assert_eq!(VerbatimUNC(OsStr::new("server"), OsStr::new("share")),
126/// get_path_prefix(r"\\?\UNC\server\share"));
127/// assert_eq!(VerbatimDisk(b'C'), get_path_prefix(r"\\?\c:\"));
128/// assert_eq!(DeviceNS(OsStr::new("BrainInterface")),
129/// get_path_prefix(r"\\.\BrainInterface"));
130/// assert_eq!(UNC(OsStr::new("server"), OsStr::new("share")),
131/// get_path_prefix(r"\\server\share"));
132/// assert_eq!(Disk(b'C'), get_path_prefix(r"C:\Users\Rust\Pictures\Ferris"));
133/// # }
134/// ```
135#[derive(Copy, Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
136#[stable(feature = "rust1", since = "1.0.0")]
137pub enum Prefix<'a> {
138 /// Verbatim prefix, e.g., `\\?\cat_pics`.
139 ///
140 /// Verbatim prefixes consist of `\\?\` immediately followed by the given
141 /// component.
142 #[stable(feature = "rust1", since = "1.0.0")]
143 Verbatim(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
144
145 /// Verbatim prefix using Windows' _**U**niform **N**aming **C**onvention_,
146 /// e.g., `\\?\UNC\server\share`.
147 ///
148 /// Verbatim UNC prefixes consist of `\\?\UNC\` immediately followed by the
149 /// server's hostname and a share name.
150 #[stable(feature = "rust1", since = "1.0.0")]
151 VerbatimUNC(
152 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
153 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
154 ),
155
156 /// Verbatim disk prefix, e.g., `\\?\C:`.
157 ///
158 /// Verbatim disk prefixes consist of `\\?\` immediately followed by the
159 /// drive letter and `:`.
160 #[stable(feature = "rust1", since = "1.0.0")]
161 VerbatimDisk(#[stable(feature = "rust1", since = "1.0.0")] u8),
162
163 /// Device namespace prefix, e.g., `\\.\COM42`.
164 ///
165 /// Device namespace prefixes consist of `\\.\` (possibly using `/`
166 /// instead of `\`), immediately followed by the device name.
167 #[stable(feature = "rust1", since = "1.0.0")]
168 DeviceNS(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
169
170 /// Prefix using Windows' _**U**niform **N**aming **C**onvention_, e.g.
171 /// `\\server\share`.
172 ///
173 /// UNC prefixes consist of the server's hostname and a share name.
174 #[stable(feature = "rust1", since = "1.0.0")]
175 UNC(
176 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
177 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
178 ),
179
180 /// Prefix `C:` for the given disk drive.
181 #[stable(feature = "rust1", since = "1.0.0")]
182 Disk(#[stable(feature = "rust1", since = "1.0.0")] u8),
183}
184
185impl<'a> Prefix<'a> {
186 #[inline]
187 fn len(&self) -> usize {
188 use self::Prefix::*;
189 fn os_str_len(s: &OsStr) -> usize {
190 s.as_encoded_bytes().len()
191 }
192 match *self {
193 Verbatim(x) => 4 + os_str_len(x),
194 VerbatimUNC(x, y) => {
195 8 + os_str_len(x) + if os_str_len(y) > 0 { 1 + os_str_len(y) } else { 0 }
196 }
197 VerbatimDisk(_) => 6,
198 UNC(x, y) => 2 + os_str_len(x) + if os_str_len(y) > 0 { 1 + os_str_len(y) } else { 0 },
199 DeviceNS(x) => 4 + os_str_len(x),
200 Disk(_) => 2,
201 }
202 }
203
204 /// Determines if the prefix is verbatim, i.e., begins with `\\?\`.
205 ///
206 /// # Examples
207 ///
208 /// ```
209 /// use std::path::Prefix::*;
210 /// use std::ffi::OsStr;
211 ///
212 /// assert!(Verbatim(OsStr::new("pictures")).is_verbatim());
213 /// assert!(VerbatimUNC(OsStr::new("server"), OsStr::new("share")).is_verbatim());
214 /// assert!(VerbatimDisk(b'C').is_verbatim());
215 /// assert!(!DeviceNS(OsStr::new("BrainInterface")).is_verbatim());
216 /// assert!(!UNC(OsStr::new("server"), OsStr::new("share")).is_verbatim());
217 /// assert!(!Disk(b'C').is_verbatim());
218 /// ```
219 #[inline]
220 #[must_use]
221 #[stable(feature = "rust1", since = "1.0.0")]
222 pub fn is_verbatim(&self) -> bool {
223 use self::Prefix::*;
224 matches!(*self, Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..))
225 }
226
227 #[inline]
228 fn is_drive(&self) -> bool {
229 matches!(*self, Prefix::Disk(_))
230 }
231
232 #[inline]
233 fn has_implicit_root(&self) -> bool {
234 !self.is_drive()
235 }
236}
237
238////////////////////////////////////////////////////////////////////////////////
239// Exposed parsing helpers
240////////////////////////////////////////////////////////////////////////////////
241
242/// Determines whether the character is one of the permitted path
243/// separators for the current platform.
244///
245/// # Examples
246///
247/// ```
248/// use std::path;
249///
250/// assert!(path::is_separator('/')); // '/' works for both Unix and Windows
251/// assert!(!path::is_separator('❤'));
252/// ```
253#[must_use]
254#[stable(feature = "rust1", since = "1.0.0")]
255pub fn is_separator(c: char) -> bool {
256 c.is_ascii() && is_sep_byte(c as u8)
257}
258
259/// The primary separator of path components for the current platform.
260///
261/// For example, `/` on Unix and `\` on Windows.
262#[stable(feature = "rust1", since = "1.0.0")]
263#[cfg_attr(not(test), rustc_diagnostic_item = "path_main_separator")]
264pub const MAIN_SEPARATOR: char = crate::sys::path::MAIN_SEP;
265
266/// The primary separator of path components for the current platform.
267///
268/// For example, `/` on Unix and `\` on Windows.
269#[stable(feature = "main_separator_str", since = "1.68.0")]
270pub const MAIN_SEPARATOR_STR: &str = crate::sys::path::MAIN_SEP_STR;
271
272////////////////////////////////////////////////////////////////////////////////
273// Misc helpers
274////////////////////////////////////////////////////////////////////////////////
275
276// Iterate through `iter` while it matches `prefix`; return `None` if `prefix`
277// is not a prefix of `iter`, otherwise return `Some(iter_after_prefix)` giving
278// `iter` after having exhausted `prefix`.
279fn iter_after<'a, 'b, I, J>(mut iter: I, mut prefix: J) -> Option<I>
280where
281 I: Iterator<Item = Component<'a>> + Clone,
282 J: Iterator<Item = Component<'b>>,
283{
284 loop {
285 let mut iter_next = iter.clone();
286 match (iter_next.next(), prefix.next()) {
287 (Some(ref x), Some(ref y)) if x == y => (),
288 (Some(_), Some(_)) => return None,
289 (Some(_), None) => return Some(iter),
290 (None, None) => return Some(iter),
291 (None, Some(_)) => return None,
292 }
293 iter = iter_next;
294 }
295}
296
297////////////////////////////////////////////////////////////////////////////////
298// Cross-platform, iterator-independent parsing
299////////////////////////////////////////////////////////////////////////////////
300
301/// Says whether the first byte after the prefix is a separator.
302fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
303 let path = if let Some(p) = prefix { &s[p.len()..] } else { s };
304 !path.is_empty() && is_sep_byte(path[0])
305}
306
307// basic workhorse for splitting stem and extension
308fn rsplit_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
309 if file.as_encoded_bytes() == b".." {
310 return (Some(file), None);
311 }
312
313 // The unsafety here stems from converting between &OsStr and &[u8]
314 // and back. This is safe to do because (1) we only look at ASCII
315 // contents of the encoding and (2) new &OsStr values are produced
316 // only from ASCII-bounded slices of existing &OsStr values.
317 let mut iter = file.as_encoded_bytes().rsplitn(2, |b| *b == b'.');
318 let after = iter.next();
319 let before = iter.next();
320 if before == Some(b"") {
321 (Some(file), None)
322 } else {
323 unsafe {
324 (
325 before.map(|s| OsStr::from_encoded_bytes_unchecked(s)),
326 after.map(|s| OsStr::from_encoded_bytes_unchecked(s)),
327 )
328 }
329 }
330}
331
332fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
333 let slice = file.as_encoded_bytes();
334 if slice == b".." {
335 return (file, None);
336 }
337
338 // The unsafety here stems from converting between &OsStr and &[u8]
339 // and back. This is safe to do because (1) we only look at ASCII
340 // contents of the encoding and (2) new &OsStr values are produced
341 // only from ASCII-bounded slices of existing &OsStr values.
342 let i = match slice[1..].iter().position(|b| *b == b'.') {
343 Some(i) => i + 1,
344 None => return (file, None),
345 };
346 let before = &slice[..i];
347 let after = &slice[i + 1..];
348 unsafe {
349 (
350 OsStr::from_encoded_bytes_unchecked(before),
351 Some(OsStr::from_encoded_bytes_unchecked(after)),
352 )
353 }
354}
355
356/// Checks whether the string is valid as a file extension, or panics otherwise.
357fn validate_extension(extension: &OsStr) {
358 for &b in extension.as_encoded_bytes() {
359 if is_sep_byte(b) {
360 panic!("extension cannot contain path separators: {extension:?}");
361 }
362 }
363}
364
365////////////////////////////////////////////////////////////////////////////////
366// The core iterators
367////////////////////////////////////////////////////////////////////////////////
368
369/// Component parsing works by a double-ended state machine; the cursors at the
370/// front and back of the path each keep track of what parts of the path have
371/// been consumed so far.
372///
373/// Going front to back, a path is made up of a prefix, a starting
374/// directory component, and a body (of normal components)
375#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
376enum State {
377 Prefix = 0, // c:
378 StartDir = 1, // / or . or nothing
379 Body = 2, // foo/bar/baz
380 Done = 3,
381}
382
383/// A structure wrapping a Windows path prefix as well as its unparsed string
384/// representation.
385///
386/// In addition to the parsed [`Prefix`] information returned by [`kind`],
387/// `PrefixComponent` also holds the raw and unparsed [`OsStr`] slice,
388/// returned by [`as_os_str`].
389///
390/// Instances of this `struct` can be obtained by matching against the
391/// [`Prefix` variant] on [`Component`].
392///
393/// Does not occur on Unix.
394///
395/// # Examples
396///
397/// ```
398/// # if cfg!(windows) {
399/// use std::path::{Component, Path, Prefix};
400/// use std::ffi::OsStr;
401///
402/// let path = Path::new(r"c:\you\later\");
403/// match path.components().next().unwrap() {
404/// Component::Prefix(prefix_component) => {
405/// assert_eq!(Prefix::Disk(b'C'), prefix_component.kind());
406/// assert_eq!(OsStr::new("c:"), prefix_component.as_os_str());
407/// }
408/// _ => unreachable!(),
409/// }
410/// # }
411/// ```
412///
413/// [`as_os_str`]: PrefixComponent::as_os_str
414/// [`kind`]: PrefixComponent::kind
415/// [`Prefix` variant]: Component::Prefix
416#[stable(feature = "rust1", since = "1.0.0")]
417#[derive(Copy, Clone, Eq, Debug)]
418pub struct PrefixComponent<'a> {
419 /// The prefix as an unparsed `OsStr` slice.
420 raw: &'a OsStr,
421
422 /// The parsed prefix data.
423 parsed: Prefix<'a>,
424}
425
426impl<'a> PrefixComponent<'a> {
427 /// Returns the parsed prefix data.
428 ///
429 /// See [`Prefix`]'s documentation for more information on the different
430 /// kinds of prefixes.
431 #[stable(feature = "rust1", since = "1.0.0")]
432 #[must_use]
433 #[inline]
434 pub fn kind(&self) -> Prefix<'a> {
435 self.parsed
436 }
437
438 /// Returns the raw [`OsStr`] slice for this prefix.
439 #[stable(feature = "rust1", since = "1.0.0")]
440 #[must_use]
441 #[inline]
442 pub fn as_os_str(&self) -> &'a OsStr {
443 self.raw
444 }
445}
446
447#[stable(feature = "rust1", since = "1.0.0")]
448impl<'a> PartialEq for PrefixComponent<'a> {
449 #[inline]
450 fn eq(&self, other: &PrefixComponent<'a>) -> bool {
451 self.parsed == other.parsed
452 }
453}
454
455#[stable(feature = "rust1", since = "1.0.0")]
456impl<'a> PartialOrd for PrefixComponent<'a> {
457 #[inline]
458 fn partial_cmp(&self, other: &PrefixComponent<'a>) -> Option<cmp::Ordering> {
459 PartialOrd::partial_cmp(&self.parsed, &other.parsed)
460 }
461}
462
463#[stable(feature = "rust1", since = "1.0.0")]
464impl Ord for PrefixComponent<'_> {
465 #[inline]
466 fn cmp(&self, other: &Self) -> cmp::Ordering {
467 Ord::cmp(&self.parsed, &other.parsed)
468 }
469}
470
471#[stable(feature = "rust1", since = "1.0.0")]
472impl Hash for PrefixComponent<'_> {
473 fn hash<H: Hasher>(&self, h: &mut H) {
474 self.parsed.hash(h);
475 }
476}
477
478/// A single component of a path.
479///
480/// A `Component` roughly corresponds to a substring between path separators
481/// (`/` or `\`).
482///
483/// This `enum` is created by iterating over [`Components`], which in turn is
484/// created by the [`components`](Path::components) method on [`Path`].
485///
486/// # Examples
487///
488/// ```rust
489/// use std::path::{Component, Path};
490///
491/// let path = Path::new("/tmp/foo/bar.txt");
492/// let components = path.components().collect::<Vec<_>>();
493/// assert_eq!(&components, &[
494/// Component::RootDir,
495/// Component::Normal("tmp".as_ref()),
496/// Component::Normal("foo".as_ref()),
497/// Component::Normal("bar.txt".as_ref()),
498/// ]);
499/// ```
500#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
501#[stable(feature = "rust1", since = "1.0.0")]
502pub enum Component<'a> {
503 /// A Windows path prefix, e.g., `C:` or `\\server\share`.
504 ///
505 /// There is a large variety of prefix types, see [`Prefix`]'s documentation
506 /// for more.
507 ///
508 /// Does not occur on Unix.
509 #[stable(feature = "rust1", since = "1.0.0")]
510 Prefix(#[stable(feature = "rust1", since = "1.0.0")] PrefixComponent<'a>),
511
512 /// The root directory component, appears after any prefix and before anything else.
513 ///
514 /// It represents a separator that designates that a path starts from root.
515 #[stable(feature = "rust1", since = "1.0.0")]
516 RootDir,
517
518 /// A reference to the current directory, i.e., `.`.
519 #[stable(feature = "rust1", since = "1.0.0")]
520 CurDir,
521
522 /// A reference to the parent directory, i.e., `..`.
523 #[stable(feature = "rust1", since = "1.0.0")]
524 ParentDir,
525
526 /// A normal component, e.g., `a` and `b` in `a/b`.
527 ///
528 /// This variant is the most common one, it represents references to files
529 /// or directories.
530 #[stable(feature = "rust1", since = "1.0.0")]
531 Normal(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
532}
533
534impl<'a> Component<'a> {
535 /// Extracts the underlying [`OsStr`] slice.
536 ///
537 /// # Examples
538 ///
539 /// ```
540 /// use std::path::Path;
541 ///
542 /// let path = Path::new("./tmp/foo/bar.txt");
543 /// let components: Vec<_> = path.components().map(|comp| comp.as_os_str()).collect();
544 /// assert_eq!(&components, &[".", "tmp", "foo", "bar.txt"]);
545 /// ```
546 #[must_use = "`self` will be dropped if the result is not used"]
547 #[stable(feature = "rust1", since = "1.0.0")]
548 pub fn as_os_str(self) -> &'a OsStr {
549 match self {
550 Component::Prefix(p) => p.as_os_str(),
551 Component::RootDir => OsStr::new(MAIN_SEP_STR),
552 Component::CurDir => OsStr::new("."),
553 Component::ParentDir => OsStr::new(".."),
554 Component::Normal(path) => path,
555 }
556 }
557}
558
559#[stable(feature = "rust1", since = "1.0.0")]
560impl AsRef<OsStr> for Component<'_> {
561 #[inline]
562 fn as_ref(&self) -> &OsStr {
563 self.as_os_str()
564 }
565}
566
567#[stable(feature = "path_component_asref", since = "1.25.0")]
568impl AsRef<Path> for Component<'_> {
569 #[inline]
570 fn as_ref(&self) -> &Path {
571 self.as_os_str().as_ref()
572 }
573}
574
575/// An iterator over the [`Component`]s of a [`Path`].
576///
577/// This `struct` is created by the [`components`] method on [`Path`].
578/// See its documentation for more.
579///
580/// # Examples
581///
582/// ```
583/// use std::path::Path;
584///
585/// let path = Path::new("/tmp/foo/bar.txt");
586///
587/// for component in path.components() {
588/// println!("{component:?}");
589/// }
590/// ```
591///
592/// [`components`]: Path::components
593#[derive(Clone)]
594#[must_use = "iterators are lazy and do nothing unless consumed"]
595#[stable(feature = "rust1", since = "1.0.0")]
596pub struct Components<'a> {
597 // The path left to parse components from
598 path: &'a [u8],
599
600 // The prefix as it was originally parsed, if any
601 prefix: Option<Prefix<'a>>,
602
603 // true if path *physically* has a root separator; for most Windows
604 // prefixes, it may have a "logical" root separator for the purposes of
605 // normalization, e.g., \\server\share == \\server\share\.
606 has_physical_root: bool,
607
608 // The iterator is double-ended, and these two states keep track of what has
609 // been produced from either end
610 front: State,
611 back: State,
612}
613
614/// An iterator over the [`Component`]s of a [`Path`], as [`OsStr`] slices.
615///
616/// This `struct` is created by the [`iter`] method on [`Path`].
617/// See its documentation for more.
618///
619/// [`iter`]: Path::iter
620#[derive(Clone)]
621#[must_use = "iterators are lazy and do nothing unless consumed"]
622#[stable(feature = "rust1", since = "1.0.0")]
623pub struct Iter<'a> {
624 inner: Components<'a>,
625}
626
627#[stable(feature = "path_components_debug", since = "1.13.0")]
628impl fmt::Debug for Components<'_> {
629 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
630 struct DebugHelper<'a>(&'a Path);
631
632 impl fmt::Debug for DebugHelper<'_> {
633 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
634 f.debug_list().entries(self.0.components()).finish()
635 }
636 }
637
638 f.debug_tuple("Components").field(&DebugHelper(self.as_path())).finish()
639 }
640}
641
642impl<'a> Components<'a> {
643 // how long is the prefix, if any?
644 #[inline]
645 fn prefix_len(&self) -> usize {
646 if !HAS_PREFIXES {
647 return 0;
648 }
649 self.prefix.as_ref().map(Prefix::len).unwrap_or(0)
650 }
651
652 #[inline]
653 fn prefix_verbatim(&self) -> bool {
654 if !HAS_PREFIXES {
655 return false;
656 }
657 self.prefix.as_ref().map(Prefix::is_verbatim).unwrap_or(false)
658 }
659
660 /// how much of the prefix is left from the point of view of iteration?
661 #[inline]
662 fn prefix_remaining(&self) -> usize {
663 if !HAS_PREFIXES {
664 return 0;
665 }
666 if self.front == State::Prefix { self.prefix_len() } else { 0 }
667 }
668
669 // Given the iteration so far, how much of the pre-State::Body path is left?
670 #[inline]
671 fn len_before_body(&self) -> usize {
672 let root = if self.front <= State::StartDir && self.has_physical_root { 1 } else { 0 };
673 let cur_dir = if self.front <= State::StartDir && self.include_cur_dir() { 1 } else { 0 };
674 self.prefix_remaining() + root + cur_dir
675 }
676
677 // is the iteration complete?
678 #[inline]
679 fn finished(&self) -> bool {
680 self.front == State::Done || self.back == State::Done || self.front > self.back
681 }
682
683 #[inline]
684 fn is_sep_byte(&self, b: u8) -> bool {
685 if self.prefix_verbatim() { is_verbatim_sep(b) } else { is_sep_byte(b) }
686 }
687
688 /// Extracts a slice corresponding to the portion of the path remaining for iteration.
689 ///
690 /// # Examples
691 ///
692 /// ```
693 /// use std::path::Path;
694 ///
695 /// let mut components = Path::new("/tmp/foo/bar.txt").components();
696 /// components.next();
697 /// components.next();
698 ///
699 /// assert_eq!(Path::new("foo/bar.txt"), components.as_path());
700 /// ```
701 #[must_use]
702 #[stable(feature = "rust1", since = "1.0.0")]
703 pub fn as_path(&self) -> &'a Path {
704 let mut comps = self.clone();
705 if comps.front == State::Body {
706 comps.trim_left();
707 }
708 if comps.back == State::Body {
709 comps.trim_right();
710 }
711 unsafe { Path::from_u8_slice(comps.path) }
712 }
713
714 /// Is the *original* path rooted?
715 fn has_root(&self) -> bool {
716 if self.has_physical_root {
717 return true;
718 }
719 if HAS_PREFIXES && let Some(p) = self.prefix {
720 if p.has_implicit_root() {
721 return true;
722 }
723 }
724 false
725 }
726
727 /// Should the normalized path include a leading . ?
728 fn include_cur_dir(&self) -> bool {
729 if self.has_root() {
730 return false;
731 }
732 let slice = &self.path[self.prefix_remaining()..];
733 match slice {
734 [b'.'] => true,
735 [b'.', b, ..] => self.is_sep_byte(*b),
736 _ => false,
737 }
738 }
739
740 // parse a given byte sequence following the OsStr encoding into the
741 // corresponding path component
742 unsafe fn parse_single_component<'b>(&self, comp: &'b [u8]) -> Option<Component<'b>> {
743 match comp {
744 b"." if HAS_PREFIXES && self.prefix_verbatim() => Some(Component::CurDir),
745 b"." => None, // . components are normalized away, except at
746 // the beginning of a path, which is treated
747 // separately via `include_cur_dir`
748 b".." => Some(Component::ParentDir),
749 b"" => None,
750 _ => Some(Component::Normal(unsafe { OsStr::from_encoded_bytes_unchecked(comp) })),
751 }
752 }
753
754 // parse a component from the left, saying how many bytes to consume to
755 // remove the component
756 fn parse_next_component(&self) -> (usize, Option<Component<'a>>) {
757 debug_assert!(self.front == State::Body);
758 let (extra, comp) = match self.path.iter().position(|b| self.is_sep_byte(*b)) {
759 None => (0, self.path),
760 Some(i) => (1, &self.path[..i]),
761 };
762 // SAFETY: `comp` is a valid substring, since it is split on a separator.
763 (comp.len() + extra, unsafe { self.parse_single_component(comp) })
764 }
765
766 // parse a component from the right, saying how many bytes to consume to
767 // remove the component
768 fn parse_next_component_back(&self) -> (usize, Option<Component<'a>>) {
769 debug_assert!(self.back == State::Body);
770 let start = self.len_before_body();
771 let (extra, comp) = match self.path[start..].iter().rposition(|b| self.is_sep_byte(*b)) {
772 None => (0, &self.path[start..]),
773 Some(i) => (1, &self.path[start + i + 1..]),
774 };
775 // SAFETY: `comp` is a valid substring, since it is split on a separator.
776 (comp.len() + extra, unsafe { self.parse_single_component(comp) })
777 }
778
779 // trim away repeated separators (i.e., empty components) on the left
780 fn trim_left(&mut self) {
781 while !self.path.is_empty() {
782 let (size, comp) = self.parse_next_component();
783 if comp.is_some() {
784 return;
785 } else {
786 self.path = &self.path[size..];
787 }
788 }
789 }
790
791 // trim away repeated separators (i.e., empty components) on the right
792 fn trim_right(&mut self) {
793 while self.path.len() > self.len_before_body() {
794 let (size, comp) = self.parse_next_component_back();
795 if comp.is_some() {
796 return;
797 } else {
798 self.path = &self.path[..self.path.len() - size];
799 }
800 }
801 }
802}
803
804#[stable(feature = "rust1", since = "1.0.0")]
805impl AsRef<Path> for Components<'_> {
806 #[inline]
807 fn as_ref(&self) -> &Path {
808 self.as_path()
809 }
810}
811
812#[stable(feature = "rust1", since = "1.0.0")]
813impl AsRef<OsStr> for Components<'_> {
814 #[inline]
815 fn as_ref(&self) -> &OsStr {
816 self.as_path().as_os_str()
817 }
818}
819
820#[stable(feature = "path_iter_debug", since = "1.13.0")]
821impl fmt::Debug for Iter<'_> {
822 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
823 struct DebugHelper<'a>(&'a Path);
824
825 impl fmt::Debug for DebugHelper<'_> {
826 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
827 f.debug_list().entries(self.0.iter()).finish()
828 }
829 }
830
831 f.debug_tuple("Iter").field(&DebugHelper(self.as_path())).finish()
832 }
833}
834
835impl<'a> Iter<'a> {
836 /// Extracts a slice corresponding to the portion of the path remaining for iteration.
837 ///
838 /// # Examples
839 ///
840 /// ```
841 /// use std::path::Path;
842 ///
843 /// let mut iter = Path::new("/tmp/foo/bar.txt").iter();
844 /// iter.next();
845 /// iter.next();
846 ///
847 /// assert_eq!(Path::new("foo/bar.txt"), iter.as_path());
848 /// ```
849 #[stable(feature = "rust1", since = "1.0.0")]
850 #[must_use]
851 #[inline]
852 pub fn as_path(&self) -> &'a Path {
853 self.inner.as_path()
854 }
855}
856
857#[stable(feature = "rust1", since = "1.0.0")]
858impl AsRef<Path> for Iter<'_> {
859 #[inline]
860 fn as_ref(&self) -> &Path {
861 self.as_path()
862 }
863}
864
865#[stable(feature = "rust1", since = "1.0.0")]
866impl AsRef<OsStr> for Iter<'_> {
867 #[inline]
868 fn as_ref(&self) -> &OsStr {
869 self.as_path().as_os_str()
870 }
871}
872
873#[stable(feature = "rust1", since = "1.0.0")]
874impl<'a> Iterator for Iter<'a> {
875 type Item = &'a OsStr;
876
877 #[inline]
878 fn next(&mut self) -> Option<&'a OsStr> {
879 self.inner.next().map(Component::as_os_str)
880 }
881}
882
883#[stable(feature = "rust1", since = "1.0.0")]
884impl<'a> DoubleEndedIterator for Iter<'a> {
885 #[inline]
886 fn next_back(&mut self) -> Option<&'a OsStr> {
887 self.inner.next_back().map(Component::as_os_str)
888 }
889}
890
891#[stable(feature = "fused", since = "1.26.0")]
892impl FusedIterator for Iter<'_> {}
893
894#[stable(feature = "rust1", since = "1.0.0")]
895impl<'a> Iterator for Components<'a> {
896 type Item = Component<'a>;
897
898 fn next(&mut self) -> Option<Component<'a>> {
899 while !self.finished() {
900 match self.front {
901 // most likely case first
902 State::Body if !self.path.is_empty() => {
903 let (size, comp) = self.parse_next_component();
904 self.path = &self.path[size..];
905 if comp.is_some() {
906 return comp;
907 }
908 }
909 State::Body => {
910 self.front = State::Done;
911 }
912 State::StartDir => {
913 self.front = State::Body;
914 if self.has_physical_root {
915 debug_assert!(!self.path.is_empty());
916 self.path = &self.path[1..];
917 return Some(Component::RootDir);
918 } else if HAS_PREFIXES && let Some(p) = self.prefix {
919 if p.has_implicit_root() && !p.is_verbatim() {
920 return Some(Component::RootDir);
921 }
922 } else if self.include_cur_dir() {
923 debug_assert!(!self.path.is_empty());
924 self.path = &self.path[1..];
925 return Some(Component::CurDir);
926 }
927 }
928 _ if const { !HAS_PREFIXES } => unreachable!(),
929 State::Prefix if self.prefix_len() == 0 => {
930 self.front = State::StartDir;
931 }
932 State::Prefix => {
933 self.front = State::StartDir;
934 debug_assert!(self.prefix_len() <= self.path.len());
935 let raw = &self.path[..self.prefix_len()];
936 self.path = &self.path[self.prefix_len()..];
937 return Some(Component::Prefix(PrefixComponent {
938 raw: unsafe { OsStr::from_encoded_bytes_unchecked(raw) },
939 parsed: self.prefix.unwrap(),
940 }));
941 }
942 State::Done => unreachable!(),
943 }
944 }
945 None
946 }
947}
948
949#[stable(feature = "rust1", since = "1.0.0")]
950impl<'a> DoubleEndedIterator for Components<'a> {
951 fn next_back(&mut self) -> Option<Component<'a>> {
952 while !self.finished() {
953 match self.back {
954 State::Body if self.path.len() > self.len_before_body() => {
955 let (size, comp) = self.parse_next_component_back();
956 self.path = &self.path[..self.path.len() - size];
957 if comp.is_some() {
958 return comp;
959 }
960 }
961 State::Body => {
962 self.back = State::StartDir;
963 }
964 State::StartDir => {
965 self.back = if HAS_PREFIXES { State::Prefix } else { State::Done };
966 if self.has_physical_root {
967 self.path = &self.path[..self.path.len() - 1];
968 return Some(Component::RootDir);
969 } else if HAS_PREFIXES && let Some(p) = self.prefix {
970 if p.has_implicit_root() && !p.is_verbatim() {
971 return Some(Component::RootDir);
972 }
973 } else if self.include_cur_dir() {
974 self.path = &self.path[..self.path.len() - 1];
975 return Some(Component::CurDir);
976 }
977 }
978 _ if !HAS_PREFIXES => unreachable!(),
979 State::Prefix if self.prefix_len() > 0 => {
980 self.back = State::Done;
981 return Some(Component::Prefix(PrefixComponent {
982 raw: unsafe { OsStr::from_encoded_bytes_unchecked(self.path) },
983 parsed: self.prefix.unwrap(),
984 }));
985 }
986 State::Prefix => {
987 self.back = State::Done;
988 return None;
989 }
990 State::Done => unreachable!(),
991 }
992 }
993 None
994 }
995}
996
997#[stable(feature = "fused", since = "1.26.0")]
998impl FusedIterator for Components<'_> {}
999
1000#[stable(feature = "rust1", since = "1.0.0")]
1001impl<'a> PartialEq for Components<'a> {
1002 #[inline]
1003 fn eq(&self, other: &Components<'a>) -> bool {
1004 let Components { path: _, front: _, back: _, has_physical_root: _, prefix: _ } = self;
1005
1006 // Fast path for exact matches, e.g. for hashmap lookups.
1007 // Don't explicitly compare the prefix or has_physical_root fields since they'll
1008 // either be covered by the `path` buffer or are only relevant for `prefix_verbatim()`.
1009 if self.path.len() == other.path.len()
1010 && self.front == other.front
1011 && self.back == State::Body
1012 && other.back == State::Body
1013 && self.prefix_verbatim() == other.prefix_verbatim()
1014 {
1015 // possible future improvement: this could bail out earlier if there were a
1016 // reverse memcmp/bcmp comparing back to front
1017 if self.path == other.path {
1018 return true;
1019 }
1020 }
1021
1022 // compare back to front since absolute paths often share long prefixes
1023 Iterator::eq(self.clone().rev(), other.clone().rev())
1024 }
1025}
1026
1027#[stable(feature = "rust1", since = "1.0.0")]
1028impl Eq for Components<'_> {}
1029
1030#[stable(feature = "rust1", since = "1.0.0")]
1031impl<'a> PartialOrd for Components<'a> {
1032 #[inline]
1033 fn partial_cmp(&self, other: &Components<'a>) -> Option<cmp::Ordering> {
1034 Some(compare_components(self.clone(), other.clone()))
1035 }
1036}
1037
1038#[stable(feature = "rust1", since = "1.0.0")]
1039impl Ord for Components<'_> {
1040 #[inline]
1041 fn cmp(&self, other: &Self) -> cmp::Ordering {
1042 compare_components(self.clone(), other.clone())
1043 }
1044}
1045
1046fn compare_components(mut left: Components<'_>, mut right: Components<'_>) -> cmp::Ordering {
1047 // Fast path for long shared prefixes
1048 //
1049 // - compare raw bytes to find first mismatch
1050 // - backtrack to find separator before mismatch to avoid ambiguous parsings of '.' or '..' characters
1051 // - if found update state to only do a component-wise comparison on the remainder,
1052 // otherwise do it on the full path
1053 //
1054 // The fast path isn't taken for paths with a PrefixComponent to avoid backtracking into
1055 // the middle of one
1056 if left.prefix.is_none() && right.prefix.is_none() && left.front == right.front {
1057 // possible future improvement: a [u8]::first_mismatch simd implementation
1058 let first_difference = match left.path.iter().zip(right.path).position(|(&a, &b)| a != b) {
1059 None if left.path.len() == right.path.len() => return cmp::Ordering::Equal,
1060 None => left.path.len().min(right.path.len()),
1061 Some(diff) => diff,
1062 };
1063
1064 if let Some(previous_sep) =
1065 left.path[..first_difference].iter().rposition(|&b| left.is_sep_byte(b))
1066 {
1067 let mismatched_component_start = previous_sep + 1;
1068 left.path = &left.path[mismatched_component_start..];
1069 left.front = State::Body;
1070 right.path = &right.path[mismatched_component_start..];
1071 right.front = State::Body;
1072 }
1073 }
1074
1075 Iterator::cmp(left, right)
1076}
1077
1078/// An iterator over [`Path`] and its ancestors.
1079///
1080/// This `struct` is created by the [`ancestors`] method on [`Path`].
1081/// See its documentation for more.
1082///
1083/// # Examples
1084///
1085/// ```
1086/// use std::path::Path;
1087///
1088/// let path = Path::new("/foo/bar");
1089///
1090/// for ancestor in path.ancestors() {
1091/// println!("{}", ancestor.display());
1092/// }
1093/// ```
1094///
1095/// [`ancestors`]: Path::ancestors
1096#[derive(Copy, Clone, Debug)]
1097#[must_use = "iterators are lazy and do nothing unless consumed"]
1098#[stable(feature = "path_ancestors", since = "1.28.0")]
1099pub struct Ancestors<'a> {
1100 next: Option<&'a Path>,
1101}
1102
1103#[stable(feature = "path_ancestors", since = "1.28.0")]
1104impl<'a> Iterator for Ancestors<'a> {
1105 type Item = &'a Path;
1106
1107 #[inline]
1108 fn next(&mut self) -> Option<Self::Item> {
1109 let next = self.next;
1110 self.next = next.and_then(Path::parent);
1111 next
1112 }
1113}
1114
1115#[stable(feature = "path_ancestors", since = "1.28.0")]
1116impl FusedIterator for Ancestors<'_> {}
1117
1118////////////////////////////////////////////////////////////////////////////////
1119// Basic types and traits
1120////////////////////////////////////////////////////////////////////////////////
1121
1122/// An owned, mutable path (akin to [`String`]).
1123///
1124/// This type provides methods like [`push`] and [`set_extension`] that mutate
1125/// the path in place. It also implements [`Deref`] to [`Path`], meaning that
1126/// all methods on [`Path`] slices are available on `PathBuf` values as well.
1127///
1128/// [`push`]: PathBuf::push
1129/// [`set_extension`]: PathBuf::set_extension
1130///
1131/// More details about the overall approach can be found in
1132/// the [module documentation](self).
1133///
1134/// # Examples
1135///
1136/// You can use [`push`] to build up a `PathBuf` from
1137/// components:
1138///
1139/// ```
1140/// use std::path::PathBuf;
1141///
1142/// let mut path = PathBuf::new();
1143///
1144/// path.push(r"C:\");
1145/// path.push("windows");
1146/// path.push("system32");
1147///
1148/// path.set_extension("dll");
1149/// ```
1150///
1151/// However, [`push`] is best used for dynamic situations. This is a better way
1152/// to do this when you know all of the components ahead of time:
1153///
1154/// ```
1155/// use std::path::PathBuf;
1156///
1157/// let path: PathBuf = [r"C:\", "windows", "system32.dll"].iter().collect();
1158/// ```
1159///
1160/// We can still do better than this! Since these are all strings, we can use
1161/// `From::from`:
1162///
1163/// ```
1164/// use std::path::PathBuf;
1165///
1166/// let path = PathBuf::from(r"C:\windows\system32.dll");
1167/// ```
1168///
1169/// Which method works best depends on what kind of situation you're in.
1170///
1171/// Note that `PathBuf` does not always sanitize arguments, for example
1172/// [`push`] allows paths built from strings which include separators:
1173///
1174/// ```
1175/// use std::path::PathBuf;
1176///
1177/// let mut path = PathBuf::new();
1178///
1179/// path.push(r"C:\");
1180/// path.push("windows");
1181/// path.push(r"..\otherdir");
1182/// path.push("system32");
1183/// ```
1184///
1185/// The behavior of `PathBuf` may be changed to a panic on such inputs
1186/// in the future. [`Extend::extend`] should be used to add multi-part paths.
1187#[cfg_attr(not(test), rustc_diagnostic_item = "PathBuf")]
1188#[stable(feature = "rust1", since = "1.0.0")]
1189pub struct PathBuf {
1190 inner: OsString,
1191}
1192
1193impl PathBuf {
1194 /// Allocates an empty `PathBuf`.
1195 ///
1196 /// # Examples
1197 ///
1198 /// ```
1199 /// use std::path::PathBuf;
1200 ///
1201 /// let path = PathBuf::new();
1202 /// ```
1203 #[stable(feature = "rust1", since = "1.0.0")]
1204 #[must_use]
1205 #[inline]
1206 #[rustc_const_stable(feature = "const_pathbuf_osstring_new", since = "1.91.0")]
1207 pub const fn new() -> PathBuf {
1208 PathBuf { inner: OsString::new() }
1209 }
1210
1211 /// Creates a new `PathBuf` with a given capacity used to create the
1212 /// internal [`OsString`]. See [`with_capacity`] defined on [`OsString`].
1213 ///
1214 /// # Examples
1215 ///
1216 /// ```
1217 /// use std::path::PathBuf;
1218 ///
1219 /// let mut path = PathBuf::with_capacity(10);
1220 /// let capacity = path.capacity();
1221 ///
1222 /// // This push is done without reallocating
1223 /// path.push(r"C:\");
1224 ///
1225 /// assert_eq!(capacity, path.capacity());
1226 /// ```
1227 ///
1228 /// [`with_capacity`]: OsString::with_capacity
1229 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1230 #[must_use]
1231 #[inline]
1232 pub fn with_capacity(capacity: usize) -> PathBuf {
1233 PathBuf { inner: OsString::with_capacity(capacity) }
1234 }
1235
1236 /// Coerces to a [`Path`] slice.
1237 ///
1238 /// # Examples
1239 ///
1240 /// ```
1241 /// use std::path::{Path, PathBuf};
1242 ///
1243 /// let p = PathBuf::from("/test");
1244 /// assert_eq!(Path::new("/test"), p.as_path());
1245 /// ```
1246 #[cfg_attr(not(test), rustc_diagnostic_item = "pathbuf_as_path")]
1247 #[stable(feature = "rust1", since = "1.0.0")]
1248 #[must_use]
1249 #[inline]
1250 pub fn as_path(&self) -> &Path {
1251 self
1252 }
1253
1254 /// Consumes and leaks the `PathBuf`, returning a mutable reference to the contents,
1255 /// `&'a mut Path`.
1256 ///
1257 /// The caller has free choice over the returned lifetime, including 'static.
1258 /// Indeed, this function is ideally used for data that lives for the remainder of
1259 /// the program's life, as dropping the returned reference will cause a memory leak.
1260 ///
1261 /// It does not reallocate or shrink the `PathBuf`, so the leaked allocation may include
1262 /// unused capacity that is not part of the returned slice. If you want to discard excess
1263 /// capacity, call [`into_boxed_path`], and then [`Box::leak`] instead.
1264 /// However, keep in mind that trimming the capacity may result in a reallocation and copy.
1265 ///
1266 /// [`into_boxed_path`]: Self::into_boxed_path
1267 #[stable(feature = "os_string_pathbuf_leak", since = "1.89.0")]
1268 #[inline]
1269 pub fn leak<'a>(self) -> &'a mut Path {
1270 Path::from_inner_mut(self.inner.leak())
1271 }
1272
1273 /// Extends `self` with `path`.
1274 ///
1275 /// If `path` is absolute, it replaces the current path.
1276 ///
1277 /// On Windows:
1278 ///
1279 /// * if `path` has a root but no prefix (e.g., `\windows`), it
1280 /// replaces everything except for the prefix (if any) of `self`.
1281 /// * if `path` has a prefix but no root, it replaces `self`.
1282 /// * if `self` has a verbatim prefix (e.g. `\\?\C:\windows`)
1283 /// and `path` is not empty, the new path is normalized: all references
1284 /// to `.` and `..` are removed.
1285 ///
1286 /// Consider using [`Path::join`] if you need a new `PathBuf` instead of
1287 /// using this function on a cloned `PathBuf`.
1288 ///
1289 /// # Examples
1290 ///
1291 /// Pushing a relative path extends the existing path:
1292 ///
1293 /// ```
1294 /// use std::path::PathBuf;
1295 ///
1296 /// let mut path = PathBuf::from("/tmp");
1297 /// path.push("file.bk");
1298 /// assert_eq!(path, PathBuf::from("/tmp/file.bk"));
1299 /// ```
1300 ///
1301 /// Pushing an absolute path replaces the existing path:
1302 ///
1303 /// ```
1304 /// use std::path::PathBuf;
1305 ///
1306 /// let mut path = PathBuf::from("/tmp");
1307 /// path.push("/etc");
1308 /// assert_eq!(path, PathBuf::from("/etc"));
1309 /// ```
1310 #[stable(feature = "rust1", since = "1.0.0")]
1311 #[rustc_confusables("append", "put")]
1312 pub fn push<P: AsRef<Path>>(&mut self, path: P) {
1313 self._push(path.as_ref())
1314 }
1315
1316 fn _push(&mut self, path: &Path) {
1317 // in general, a separator is needed if the rightmost byte is not a separator
1318 let buf = self.inner.as_encoded_bytes();
1319 let mut need_sep = buf.last().map(|c| !is_sep_byte(*c)).unwrap_or(false);
1320
1321 // in the special case of `C:` on Windows, do *not* add a separator
1322 let comps = self.components();
1323
1324 if comps.prefix_len() > 0
1325 && comps.prefix_len() == comps.path.len()
1326 && comps.prefix.unwrap().is_drive()
1327 {
1328 need_sep = false
1329 }
1330
1331 let need_clear = if cfg!(target_os = "cygwin") {
1332 // If path is absolute and its prefix is none, it is like `/foo`,
1333 // and will be handled below.
1334 path.prefix().is_some()
1335 } else {
1336 // On Unix: prefix is always None.
1337 path.is_absolute() || path.prefix().is_some()
1338 };
1339
1340 // absolute `path` replaces `self`
1341 if need_clear {
1342 self.inner.truncate(0);
1343
1344 // verbatim paths need . and .. removed
1345 } else if comps.prefix_verbatim() && !path.inner.is_empty() {
1346 let mut buf: Vec<_> = comps.collect();
1347 for c in path.components() {
1348 match c {
1349 Component::RootDir => {
1350 buf.truncate(1);
1351 buf.push(c);
1352 }
1353 Component::CurDir => (),
1354 Component::ParentDir => {
1355 if let Some(Component::Normal(_)) = buf.last() {
1356 buf.pop();
1357 }
1358 }
1359 _ => buf.push(c),
1360 }
1361 }
1362
1363 let mut res = OsString::new();
1364 let mut need_sep = false;
1365
1366 for c in buf {
1367 if need_sep && c != Component::RootDir {
1368 res.push(MAIN_SEP_STR);
1369 }
1370 res.push(c.as_os_str());
1371
1372 need_sep = match c {
1373 Component::RootDir => false,
1374 Component::Prefix(prefix) => {
1375 !prefix.parsed.is_drive() && prefix.parsed.len() > 0
1376 }
1377 _ => true,
1378 }
1379 }
1380
1381 self.inner = res;
1382 return;
1383
1384 // `path` has a root but no prefix, e.g., `\windows` (Windows only)
1385 } else if path.has_root() {
1386 let prefix_len = self.components().prefix_remaining();
1387 self.inner.truncate(prefix_len);
1388
1389 // `path` is a pure relative path
1390 } else if need_sep {
1391 self.inner.push(MAIN_SEP_STR);
1392 }
1393
1394 self.inner.push(path);
1395 }
1396
1397 /// Truncates `self` to [`self.parent`].
1398 ///
1399 /// Returns `false` and does nothing if [`self.parent`] is [`None`].
1400 /// Otherwise, returns `true`.
1401 ///
1402 /// [`self.parent`]: Path::parent
1403 ///
1404 /// # Examples
1405 ///
1406 /// ```
1407 /// use std::path::{Path, PathBuf};
1408 ///
1409 /// let mut p = PathBuf::from("/spirited/away.rs");
1410 ///
1411 /// p.pop();
1412 /// assert_eq!(Path::new("/spirited"), p);
1413 /// p.pop();
1414 /// assert_eq!(Path::new("/"), p);
1415 /// ```
1416 #[stable(feature = "rust1", since = "1.0.0")]
1417 pub fn pop(&mut self) -> bool {
1418 match self.parent().map(|p| p.as_u8_slice().len()) {
1419 Some(len) => {
1420 self.inner.truncate(len);
1421 true
1422 }
1423 None => false,
1424 }
1425 }
1426
1427 /// Sets whether the path has a trailing [separator](MAIN_SEPARATOR).
1428 ///
1429 /// The value returned by [`has_trailing_sep`](Path::has_trailing_sep) will be equivalent to
1430 /// the provided value if possible.
1431 ///
1432 /// # Examples
1433 ///
1434 /// ```
1435 /// #![feature(path_trailing_sep)]
1436 /// use std::path::PathBuf;
1437 ///
1438 /// let mut p = PathBuf::from("dir");
1439 ///
1440 /// assert!(!p.has_trailing_sep());
1441 /// p.set_trailing_sep(false);
1442 /// assert!(!p.has_trailing_sep());
1443 /// p.set_trailing_sep(true);
1444 /// assert!(p.has_trailing_sep());
1445 /// p.set_trailing_sep(false);
1446 /// assert!(!p.has_trailing_sep());
1447 ///
1448 /// p = PathBuf::from("/");
1449 /// assert!(p.has_trailing_sep());
1450 /// p.set_trailing_sep(false);
1451 /// assert!(p.has_trailing_sep());
1452 /// ```
1453 #[unstable(feature = "path_trailing_sep", issue = "142503")]
1454 pub fn set_trailing_sep(&mut self, trailing_sep: bool) {
1455 if trailing_sep { self.push_trailing_sep() } else { self.pop_trailing_sep() }
1456 }
1457
1458 /// Adds a trailing [separator](MAIN_SEPARATOR) to the path.
1459 ///
1460 /// This acts similarly to [`Path::with_trailing_sep`], but mutates the underlying `PathBuf`.
1461 ///
1462 /// # Examples
1463 ///
1464 /// ```
1465 /// #![feature(path_trailing_sep)]
1466 /// use std::ffi::OsStr;
1467 /// use std::path::PathBuf;
1468 ///
1469 /// let mut p = PathBuf::from("dir");
1470 ///
1471 /// assert!(!p.has_trailing_sep());
1472 /// p.push_trailing_sep();
1473 /// assert!(p.has_trailing_sep());
1474 /// p.push_trailing_sep();
1475 /// assert!(p.has_trailing_sep());
1476 ///
1477 /// p = PathBuf::from("dir/");
1478 /// p.push_trailing_sep();
1479 /// assert_eq!(p.as_os_str(), OsStr::new("dir/"));
1480 /// ```
1481 #[unstable(feature = "path_trailing_sep", issue = "142503")]
1482 pub fn push_trailing_sep(&mut self) {
1483 if !self.has_trailing_sep() {
1484 self.push("");
1485 }
1486 }
1487
1488 /// Removes a trailing [separator](MAIN_SEPARATOR) from the path, if possible.
1489 ///
1490 /// This acts similarly to [`Path::trim_trailing_sep`], but mutates the underlying `PathBuf`.
1491 ///
1492 /// # Examples
1493 ///
1494 /// ```
1495 /// #![feature(path_trailing_sep)]
1496 /// use std::ffi::OsStr;
1497 /// use std::path::PathBuf;
1498 ///
1499 /// let mut p = PathBuf::from("dir//");
1500 ///
1501 /// assert!(p.has_trailing_sep());
1502 /// assert_eq!(p.as_os_str(), OsStr::new("dir//"));
1503 /// p.pop_trailing_sep();
1504 /// assert!(!p.has_trailing_sep());
1505 /// assert_eq!(p.as_os_str(), OsStr::new("dir"));
1506 /// p.pop_trailing_sep();
1507 /// assert!(!p.has_trailing_sep());
1508 /// assert_eq!(p.as_os_str(), OsStr::new("dir"));
1509 ///
1510 /// p = PathBuf::from("/");
1511 /// assert!(p.has_trailing_sep());
1512 /// p.pop_trailing_sep();
1513 /// assert!(p.has_trailing_sep());
1514 /// ```
1515 #[unstable(feature = "path_trailing_sep", issue = "142503")]
1516 pub fn pop_trailing_sep(&mut self) {
1517 self.inner.truncate(self.trim_trailing_sep().as_os_str().len());
1518 }
1519
1520 /// Updates [`self.file_name`] to `file_name`.
1521 ///
1522 /// If [`self.file_name`] was [`None`], this is equivalent to pushing
1523 /// `file_name`.
1524 ///
1525 /// Otherwise it is equivalent to calling [`pop`] and then pushing
1526 /// `file_name`. The new path will be a sibling of the original path.
1527 /// (That is, it will have the same parent.)
1528 ///
1529 /// The argument is not sanitized, so can include separators. This
1530 /// behavior may be changed to a panic in the future.
1531 ///
1532 /// [`self.file_name`]: Path::file_name
1533 /// [`pop`]: PathBuf::pop
1534 ///
1535 /// # Examples
1536 ///
1537 /// ```
1538 /// use std::path::PathBuf;
1539 ///
1540 /// let mut buf = PathBuf::from("/");
1541 /// assert!(buf.file_name() == None);
1542 ///
1543 /// buf.set_file_name("foo.txt");
1544 /// assert!(buf == PathBuf::from("/foo.txt"));
1545 /// assert!(buf.file_name().is_some());
1546 ///
1547 /// buf.set_file_name("bar.txt");
1548 /// assert!(buf == PathBuf::from("/bar.txt"));
1549 ///
1550 /// buf.set_file_name("baz");
1551 /// assert!(buf == PathBuf::from("/baz"));
1552 ///
1553 /// buf.set_file_name("../b/c.txt");
1554 /// assert!(buf == PathBuf::from("/../b/c.txt"));
1555 ///
1556 /// buf.set_file_name("baz");
1557 /// assert!(buf == PathBuf::from("/../b/baz"));
1558 /// ```
1559 #[stable(feature = "rust1", since = "1.0.0")]
1560 pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
1561 self._set_file_name(file_name.as_ref())
1562 }
1563
1564 fn _set_file_name(&mut self, file_name: &OsStr) {
1565 if self.file_name().is_some() {
1566 let popped = self.pop();
1567 debug_assert!(popped);
1568 }
1569 self.push(file_name);
1570 }
1571
1572 /// Updates [`self.extension`] to `Some(extension)` or to `None` if
1573 /// `extension` is empty.
1574 ///
1575 /// Returns `false` and does nothing if [`self.file_name`] is [`None`],
1576 /// returns `true` and updates the extension otherwise.
1577 ///
1578 /// If [`self.extension`] is [`None`], the extension is added; otherwise
1579 /// it is replaced.
1580 ///
1581 /// If `extension` is the empty string, [`self.extension`] will be [`None`]
1582 /// afterwards, not `Some("")`.
1583 ///
1584 /// # Panics
1585 ///
1586 /// Panics if the passed extension contains a path separator (see
1587 /// [`is_separator`]).
1588 ///
1589 /// # Caveats
1590 ///
1591 /// The new `extension` may contain dots and will be used in its entirety,
1592 /// but only the part after the final dot will be reflected in
1593 /// [`self.extension`].
1594 ///
1595 /// If the file stem contains internal dots and `extension` is empty, part
1596 /// of the old file stem will be considered the new [`self.extension`].
1597 ///
1598 /// See the examples below.
1599 ///
1600 /// [`self.file_name`]: Path::file_name
1601 /// [`self.extension`]: Path::extension
1602 ///
1603 /// # Examples
1604 ///
1605 /// ```
1606 /// use std::path::{Path, PathBuf};
1607 ///
1608 /// let mut p = PathBuf::from("/feel/the");
1609 ///
1610 /// p.set_extension("force");
1611 /// assert_eq!(Path::new("/feel/the.force"), p.as_path());
1612 ///
1613 /// p.set_extension("dark.side");
1614 /// assert_eq!(Path::new("/feel/the.dark.side"), p.as_path());
1615 ///
1616 /// p.set_extension("cookie");
1617 /// assert_eq!(Path::new("/feel/the.dark.cookie"), p.as_path());
1618 ///
1619 /// p.set_extension("");
1620 /// assert_eq!(Path::new("/feel/the.dark"), p.as_path());
1621 ///
1622 /// p.set_extension("");
1623 /// assert_eq!(Path::new("/feel/the"), p.as_path());
1624 ///
1625 /// p.set_extension("");
1626 /// assert_eq!(Path::new("/feel/the"), p.as_path());
1627 /// ```
1628 #[stable(feature = "rust1", since = "1.0.0")]
1629 pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
1630 self._set_extension(extension.as_ref())
1631 }
1632
1633 fn _set_extension(&mut self, extension: &OsStr) -> bool {
1634 validate_extension(extension);
1635
1636 let file_stem = match self.file_stem() {
1637 None => return false,
1638 Some(f) => f.as_encoded_bytes(),
1639 };
1640
1641 // truncate until right after the file stem
1642 let end_file_stem = file_stem[file_stem.len()..].as_ptr().addr();
1643 let start = self.inner.as_encoded_bytes().as_ptr().addr();
1644 self.inner.truncate(end_file_stem.wrapping_sub(start));
1645
1646 // add the new extension, if any
1647 let new = extension.as_encoded_bytes();
1648 if !new.is_empty() {
1649 self.inner.reserve_exact(new.len() + 1);
1650 self.inner.push(".");
1651 // SAFETY: Since a UTF-8 string was just pushed, it is not possible
1652 // for the buffer to end with a surrogate half.
1653 unsafe { self.inner.extend_from_slice_unchecked(new) };
1654 }
1655
1656 true
1657 }
1658
1659 /// Append [`self.extension`] with `extension`.
1660 ///
1661 /// Returns `false` and does nothing if [`self.file_name`] is [`None`],
1662 /// returns `true` and updates the extension otherwise.
1663 ///
1664 /// # Panics
1665 ///
1666 /// Panics if the passed extension contains a path separator (see
1667 /// [`is_separator`]).
1668 ///
1669 /// # Caveats
1670 ///
1671 /// The appended `extension` may contain dots and will be used in its entirety,
1672 /// but only the part after the final dot will be reflected in
1673 /// [`self.extension`].
1674 ///
1675 /// See the examples below.
1676 ///
1677 /// [`self.file_name`]: Path::file_name
1678 /// [`self.extension`]: Path::extension
1679 ///
1680 /// # Examples
1681 ///
1682 /// ```
1683 /// use std::path::{Path, PathBuf};
1684 ///
1685 /// let mut p = PathBuf::from("/feel/the");
1686 ///
1687 /// p.add_extension("formatted");
1688 /// assert_eq!(Path::new("/feel/the.formatted"), p.as_path());
1689 ///
1690 /// p.add_extension("dark.side");
1691 /// assert_eq!(Path::new("/feel/the.formatted.dark.side"), p.as_path());
1692 ///
1693 /// p.set_extension("cookie");
1694 /// assert_eq!(Path::new("/feel/the.formatted.dark.cookie"), p.as_path());
1695 ///
1696 /// p.set_extension("");
1697 /// assert_eq!(Path::new("/feel/the.formatted.dark"), p.as_path());
1698 ///
1699 /// p.add_extension("");
1700 /// assert_eq!(Path::new("/feel/the.formatted.dark"), p.as_path());
1701 /// ```
1702 #[stable(feature = "path_add_extension", since = "1.91.0")]
1703 pub fn add_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
1704 self._add_extension(extension.as_ref())
1705 }
1706
1707 fn _add_extension(&mut self, extension: &OsStr) -> bool {
1708 validate_extension(extension);
1709
1710 let file_name = match self.file_name() {
1711 None => return false,
1712 Some(f) => f.as_encoded_bytes(),
1713 };
1714
1715 let new = extension.as_encoded_bytes();
1716 if !new.is_empty() {
1717 // truncate until right after the file name
1718 // this is necessary for trimming the trailing separator
1719 let end_file_name = file_name[file_name.len()..].as_ptr().addr();
1720 let start = self.inner.as_encoded_bytes().as_ptr().addr();
1721 self.inner.truncate(end_file_name.wrapping_sub(start));
1722
1723 // append the new extension
1724 self.inner.reserve_exact(new.len() + 1);
1725 self.inner.push(".");
1726 // SAFETY: Since a UTF-8 string was just pushed, it is not possible
1727 // for the buffer to end with a surrogate half.
1728 unsafe { self.inner.extend_from_slice_unchecked(new) };
1729 }
1730
1731 true
1732 }
1733
1734 /// Yields a mutable reference to the underlying [`OsString`] instance.
1735 ///
1736 /// # Examples
1737 ///
1738 /// ```
1739 /// use std::path::{Path, PathBuf};
1740 ///
1741 /// let mut path = PathBuf::from("/foo");
1742 ///
1743 /// path.push("bar");
1744 /// assert_eq!(path, Path::new("/foo/bar"));
1745 ///
1746 /// // OsString's `push` does not add a separator.
1747 /// path.as_mut_os_string().push("baz");
1748 /// assert_eq!(path, Path::new("/foo/barbaz"));
1749 /// ```
1750 #[stable(feature = "path_as_mut_os_str", since = "1.70.0")]
1751 #[must_use]
1752 #[inline]
1753 pub fn as_mut_os_string(&mut self) -> &mut OsString {
1754 &mut self.inner
1755 }
1756
1757 /// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
1758 ///
1759 /// # Examples
1760 ///
1761 /// ```
1762 /// use std::path::PathBuf;
1763 ///
1764 /// let p = PathBuf::from("/the/head");
1765 /// let os_str = p.into_os_string();
1766 /// ```
1767 #[stable(feature = "rust1", since = "1.0.0")]
1768 #[must_use = "`self` will be dropped if the result is not used"]
1769 #[inline]
1770 pub fn into_os_string(self) -> OsString {
1771 self.inner
1772 }
1773
1774 /// Converts this `PathBuf` into a [boxed](Box) [`Path`].
1775 #[stable(feature = "into_boxed_path", since = "1.20.0")]
1776 #[must_use = "`self` will be dropped if the result is not used"]
1777 #[inline]
1778 pub fn into_boxed_path(self) -> Box<Path> {
1779 let rw = Box::into_raw(self.inner.into_boxed_os_str()) as *mut Path;
1780 unsafe { Box::from_raw(rw) }
1781 }
1782
1783 /// Invokes [`capacity`] on the underlying instance of [`OsString`].
1784 ///
1785 /// [`capacity`]: OsString::capacity
1786 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1787 #[must_use]
1788 #[inline]
1789 pub fn capacity(&self) -> usize {
1790 self.inner.capacity()
1791 }
1792
1793 /// Invokes [`clear`] on the underlying instance of [`OsString`].
1794 ///
1795 /// [`clear`]: OsString::clear
1796 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1797 #[inline]
1798 pub fn clear(&mut self) {
1799 self.inner.clear()
1800 }
1801
1802 /// Invokes [`reserve`] on the underlying instance of [`OsString`].
1803 ///
1804 /// [`reserve`]: OsString::reserve
1805 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1806 #[inline]
1807 pub fn reserve(&mut self, additional: usize) {
1808 self.inner.reserve(additional)
1809 }
1810
1811 /// Invokes [`try_reserve`] on the underlying instance of [`OsString`].
1812 ///
1813 /// [`try_reserve`]: OsString::try_reserve
1814 #[stable(feature = "try_reserve_2", since = "1.63.0")]
1815 #[inline]
1816 pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
1817 self.inner.try_reserve(additional)
1818 }
1819
1820 /// Invokes [`reserve_exact`] on the underlying instance of [`OsString`].
1821 ///
1822 /// [`reserve_exact`]: OsString::reserve_exact
1823 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1824 #[inline]
1825 pub fn reserve_exact(&mut self, additional: usize) {
1826 self.inner.reserve_exact(additional)
1827 }
1828
1829 /// Invokes [`try_reserve_exact`] on the underlying instance of [`OsString`].
1830 ///
1831 /// [`try_reserve_exact`]: OsString::try_reserve_exact
1832 #[stable(feature = "try_reserve_2", since = "1.63.0")]
1833 #[inline]
1834 pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
1835 self.inner.try_reserve_exact(additional)
1836 }
1837
1838 /// Invokes [`shrink_to_fit`] on the underlying instance of [`OsString`].
1839 ///
1840 /// [`shrink_to_fit`]: OsString::shrink_to_fit
1841 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1842 #[inline]
1843 pub fn shrink_to_fit(&mut self) {
1844 self.inner.shrink_to_fit()
1845 }
1846
1847 /// Invokes [`shrink_to`] on the underlying instance of [`OsString`].
1848 ///
1849 /// [`shrink_to`]: OsString::shrink_to
1850 #[stable(feature = "shrink_to", since = "1.56.0")]
1851 #[inline]
1852 pub fn shrink_to(&mut self, min_capacity: usize) {
1853 self.inner.shrink_to(min_capacity)
1854 }
1855}
1856
1857#[stable(feature = "rust1", since = "1.0.0")]
1858impl Clone for PathBuf {
1859 #[inline]
1860 fn clone(&self) -> Self {
1861 PathBuf { inner: self.inner.clone() }
1862 }
1863
1864 /// Clones the contents of `source` into `self`.
1865 ///
1866 /// This method is preferred over simply assigning `source.clone()` to `self`,
1867 /// as it avoids reallocation if possible.
1868 #[inline]
1869 fn clone_from(&mut self, source: &Self) {
1870 self.inner.clone_from(&source.inner)
1871 }
1872}
1873
1874#[stable(feature = "box_from_path", since = "1.17.0")]
1875impl From<&Path> for Box<Path> {
1876 /// Creates a boxed [`Path`] from a reference.
1877 ///
1878 /// This will allocate and clone `path` to it.
1879 fn from(path: &Path) -> Box<Path> {
1880 Box::clone_from_ref(path)
1881 }
1882}
1883
1884#[stable(feature = "box_from_mut_slice", since = "1.84.0")]
1885impl From<&mut Path> for Box<Path> {
1886 /// Creates a boxed [`Path`] from a reference.
1887 ///
1888 /// This will allocate and clone `path` to it.
1889 fn from(path: &mut Path) -> Box<Path> {
1890 Self::from(&*path)
1891 }
1892}
1893
1894#[stable(feature = "box_from_cow", since = "1.45.0")]
1895impl From<Cow<'_, Path>> for Box<Path> {
1896 /// Creates a boxed [`Path`] from a clone-on-write pointer.
1897 ///
1898 /// Converting from a `Cow::Owned` does not clone or allocate.
1899 #[inline]
1900 fn from(cow: Cow<'_, Path>) -> Box<Path> {
1901 match cow {
1902 Cow::Borrowed(path) => Box::from(path),
1903 Cow::Owned(path) => Box::from(path),
1904 }
1905 }
1906}
1907
1908#[stable(feature = "path_buf_from_box", since = "1.18.0")]
1909impl From<Box<Path>> for PathBuf {
1910 /// Converts a <code>[Box]<[Path]></code> into a [`PathBuf`].
1911 ///
1912 /// This conversion does not allocate or copy memory.
1913 #[inline]
1914 fn from(boxed: Box<Path>) -> PathBuf {
1915 boxed.into_path_buf()
1916 }
1917}
1918
1919#[stable(feature = "box_from_path_buf", since = "1.20.0")]
1920impl From<PathBuf> for Box<Path> {
1921 /// Converts a [`PathBuf`] into a <code>[Box]<[Path]></code>.
1922 ///
1923 /// This conversion currently should not allocate memory,
1924 /// but this behavior is not guaranteed on all platforms or in all future versions.
1925 #[inline]
1926 fn from(p: PathBuf) -> Box<Path> {
1927 p.into_boxed_path()
1928 }
1929}
1930
1931#[stable(feature = "more_box_slice_clone", since = "1.29.0")]
1932impl Clone for Box<Path> {
1933 #[inline]
1934 fn clone(&self) -> Self {
1935 self.to_path_buf().into_boxed_path()
1936 }
1937}
1938
1939#[stable(feature = "rust1", since = "1.0.0")]
1940impl<T: ?Sized + AsRef<OsStr>> From<&T> for PathBuf {
1941 /// Converts a borrowed [`OsStr`] to a [`PathBuf`].
1942 ///
1943 /// Allocates a [`PathBuf`] and copies the data into it.
1944 #[inline]
1945 fn from(s: &T) -> PathBuf {
1946 PathBuf::from(s.as_ref().to_os_string())
1947 }
1948}
1949
1950#[stable(feature = "rust1", since = "1.0.0")]
1951impl From<OsString> for PathBuf {
1952 /// Converts an [`OsString`] into a [`PathBuf`].
1953 ///
1954 /// This conversion does not allocate or copy memory.
1955 #[inline]
1956 fn from(s: OsString) -> PathBuf {
1957 PathBuf { inner: s }
1958 }
1959}
1960
1961#[stable(feature = "from_path_buf_for_os_string", since = "1.14.0")]
1962impl From<PathBuf> for OsString {
1963 /// Converts a [`PathBuf`] into an [`OsString`]
1964 ///
1965 /// This conversion does not allocate or copy memory.
1966 #[inline]
1967 fn from(path_buf: PathBuf) -> OsString {
1968 path_buf.inner
1969 }
1970}
1971
1972#[stable(feature = "rust1", since = "1.0.0")]
1973impl From<String> for PathBuf {
1974 /// Converts a [`String`] into a [`PathBuf`]
1975 ///
1976 /// This conversion does not allocate or copy memory.
1977 #[inline]
1978 fn from(s: String) -> PathBuf {
1979 PathBuf::from(OsString::from(s))
1980 }
1981}
1982
1983#[stable(feature = "path_from_str", since = "1.32.0")]
1984impl FromStr for PathBuf {
1985 type Err = core::convert::Infallible;
1986
1987 #[inline]
1988 fn from_str(s: &str) -> Result<Self, Self::Err> {
1989 Ok(PathBuf::from(s))
1990 }
1991}
1992
1993#[stable(feature = "rust1", since = "1.0.0")]
1994impl<P: AsRef<Path>> FromIterator<P> for PathBuf {
1995 /// Creates a new `PathBuf` from the [`Path`] elements of an iterator.
1996 ///
1997 /// This uses [`push`](Self::push) to add each element, so can be used to adjoin multiple path
1998 /// [components](Components).
1999 ///
2000 /// # Examples
2001 /// ```
2002 /// # use std::path::PathBuf;
2003 /// let path = PathBuf::from_iter(["/tmp", "foo", "bar"]);
2004 /// assert_eq!(path, PathBuf::from("/tmp/foo/bar"));
2005 /// ```
2006 ///
2007 /// See documentation for [`push`](Self::push) for more details on how the path is constructed.
2008 fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> PathBuf {
2009 let mut buf = PathBuf::new();
2010 buf.extend(iter);
2011 buf
2012 }
2013}
2014
2015#[stable(feature = "rust1", since = "1.0.0")]
2016impl<P: AsRef<Path>> Extend<P> for PathBuf {
2017 /// Extends `self` with [`Path`] elements from `iter`.
2018 ///
2019 /// This uses [`push`](Self::push) to add each element, so can be used to adjoin multiple path
2020 /// [components](Components).
2021 ///
2022 /// # Examples
2023 /// ```
2024 /// # use std::path::PathBuf;
2025 /// let mut path = PathBuf::from("/tmp");
2026 /// path.extend(["foo", "bar", "file.txt"]);
2027 /// assert_eq!(path, PathBuf::from("/tmp/foo/bar/file.txt"));
2028 /// ```
2029 ///
2030 /// See documentation for [`push`](Self::push) for more details on how the path is constructed.
2031 fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) {
2032 iter.into_iter().for_each(move |p| self.push(p.as_ref()));
2033 }
2034
2035 #[inline]
2036 fn extend_one(&mut self, p: P) {
2037 self.push(p.as_ref());
2038 }
2039}
2040
2041#[stable(feature = "rust1", since = "1.0.0")]
2042impl fmt::Debug for PathBuf {
2043 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2044 fmt::Debug::fmt(&**self, formatter)
2045 }
2046}
2047
2048#[stable(feature = "rust1", since = "1.0.0")]
2049impl ops::Deref for PathBuf {
2050 type Target = Path;
2051 #[inline]
2052 fn deref(&self) -> &Path {
2053 Path::new(&self.inner)
2054 }
2055}
2056
2057#[stable(feature = "path_buf_deref_mut", since = "1.68.0")]
2058impl ops::DerefMut for PathBuf {
2059 #[inline]
2060 fn deref_mut(&mut self) -> &mut Path {
2061 Path::from_inner_mut(&mut self.inner)
2062 }
2063}
2064
2065#[stable(feature = "rust1", since = "1.0.0")]
2066impl Borrow<Path> for PathBuf {
2067 #[inline]
2068 fn borrow(&self) -> &Path {
2069 self.deref()
2070 }
2071}
2072
2073#[stable(feature = "default_for_pathbuf", since = "1.17.0")]
2074impl Default for PathBuf {
2075 #[inline]
2076 fn default() -> Self {
2077 PathBuf::new()
2078 }
2079}
2080
2081#[stable(feature = "cow_from_path", since = "1.6.0")]
2082impl<'a> From<&'a Path> for Cow<'a, Path> {
2083 /// Creates a clone-on-write pointer from a reference to
2084 /// [`Path`].
2085 ///
2086 /// This conversion does not clone or allocate.
2087 #[inline]
2088 fn from(s: &'a Path) -> Cow<'a, Path> {
2089 Cow::Borrowed(s)
2090 }
2091}
2092
2093#[stable(feature = "cow_from_path", since = "1.6.0")]
2094impl<'a> From<PathBuf> for Cow<'a, Path> {
2095 /// Creates a clone-on-write pointer from an owned
2096 /// instance of [`PathBuf`].
2097 ///
2098 /// This conversion does not clone or allocate.
2099 #[inline]
2100 fn from(s: PathBuf) -> Cow<'a, Path> {
2101 Cow::Owned(s)
2102 }
2103}
2104
2105#[stable(feature = "cow_from_pathbuf_ref", since = "1.28.0")]
2106impl<'a> From<&'a PathBuf> for Cow<'a, Path> {
2107 /// Creates a clone-on-write pointer from a reference to
2108 /// [`PathBuf`].
2109 ///
2110 /// This conversion does not clone or allocate.
2111 #[inline]
2112 fn from(p: &'a PathBuf) -> Cow<'a, Path> {
2113 Cow::Borrowed(p.as_path())
2114 }
2115}
2116
2117#[stable(feature = "pathbuf_from_cow_path", since = "1.28.0")]
2118impl<'a> From<Cow<'a, Path>> for PathBuf {
2119 /// Converts a clone-on-write pointer to an owned path.
2120 ///
2121 /// Converting from a `Cow::Owned` does not clone or allocate.
2122 #[inline]
2123 fn from(p: Cow<'a, Path>) -> Self {
2124 p.into_owned()
2125 }
2126}
2127
2128#[stable(feature = "shared_from_slice2", since = "1.24.0")]
2129impl From<PathBuf> for Arc<Path> {
2130 /// Converts a [`PathBuf`] into an <code>[Arc]<[Path]></code> by moving the [`PathBuf`] data
2131 /// into a new [`Arc`] buffer.
2132 #[inline]
2133 fn from(s: PathBuf) -> Arc<Path> {
2134 let arc: Arc<OsStr> = Arc::from(s.into_os_string());
2135 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Path) }
2136 }
2137}
2138
2139#[stable(feature = "shared_from_slice2", since = "1.24.0")]
2140impl From<&Path> for Arc<Path> {
2141 /// Converts a [`Path`] into an [`Arc`] by copying the [`Path`] data into a new [`Arc`] buffer.
2142 #[inline]
2143 fn from(s: &Path) -> Arc<Path> {
2144 let arc: Arc<OsStr> = Arc::from(s.as_os_str());
2145 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Path) }
2146 }
2147}
2148
2149#[stable(feature = "shared_from_mut_slice", since = "1.84.0")]
2150impl From<&mut Path> for Arc<Path> {
2151 /// Converts a [`Path`] into an [`Arc`] by copying the [`Path`] data into a new [`Arc`] buffer.
2152 #[inline]
2153 fn from(s: &mut Path) -> Arc<Path> {
2154 Arc::from(&*s)
2155 }
2156}
2157
2158#[stable(feature = "shared_from_slice2", since = "1.24.0")]
2159impl From<PathBuf> for Rc<Path> {
2160 /// Converts a [`PathBuf`] into an <code>[Rc]<[Path]></code> by moving the [`PathBuf`] data into
2161 /// a new [`Rc`] buffer.
2162 #[inline]
2163 fn from(s: PathBuf) -> Rc<Path> {
2164 let rc: Rc<OsStr> = Rc::from(s.into_os_string());
2165 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Path) }
2166 }
2167}
2168
2169#[stable(feature = "shared_from_slice2", since = "1.24.0")]
2170impl From<&Path> for Rc<Path> {
2171 /// Converts a [`Path`] into an [`Rc`] by copying the [`Path`] data into a new [`Rc`] buffer.
2172 #[inline]
2173 fn from(s: &Path) -> Rc<Path> {
2174 let rc: Rc<OsStr> = Rc::from(s.as_os_str());
2175 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Path) }
2176 }
2177}
2178
2179#[stable(feature = "shared_from_mut_slice", since = "1.84.0")]
2180impl From<&mut Path> for Rc<Path> {
2181 /// Converts a [`Path`] into an [`Rc`] by copying the [`Path`] data into a new [`Rc`] buffer.
2182 #[inline]
2183 fn from(s: &mut Path) -> Rc<Path> {
2184 Rc::from(&*s)
2185 }
2186}
2187
2188#[stable(feature = "rust1", since = "1.0.0")]
2189impl ToOwned for Path {
2190 type Owned = PathBuf;
2191 #[inline]
2192 fn to_owned(&self) -> PathBuf {
2193 self.to_path_buf()
2194 }
2195 #[inline]
2196 fn clone_into(&self, target: &mut PathBuf) {
2197 self.inner.clone_into(&mut target.inner);
2198 }
2199}
2200
2201#[stable(feature = "rust1", since = "1.0.0")]
2202impl PartialEq for PathBuf {
2203 #[inline]
2204 fn eq(&self, other: &PathBuf) -> bool {
2205 self.components() == other.components()
2206 }
2207}
2208
2209#[stable(feature = "eq_str_for_path", since = "1.91.0")]
2210impl cmp::PartialEq<str> for PathBuf {
2211 #[inline]
2212 fn eq(&self, other: &str) -> bool {
2213 self.as_path() == other
2214 }
2215}
2216
2217#[stable(feature = "eq_str_for_path", since = "1.91.0")]
2218impl cmp::PartialEq<PathBuf> for str {
2219 #[inline]
2220 fn eq(&self, other: &PathBuf) -> bool {
2221 self == other.as_path()
2222 }
2223}
2224
2225#[stable(feature = "eq_str_for_path", since = "1.91.0")]
2226impl cmp::PartialEq<String> for PathBuf {
2227 #[inline]
2228 fn eq(&self, other: &String) -> bool {
2229 self.as_path() == other.as_str()
2230 }
2231}
2232
2233#[stable(feature = "eq_str_for_path", since = "1.91.0")]
2234impl cmp::PartialEq<PathBuf> for String {
2235 #[inline]
2236 fn eq(&self, other: &PathBuf) -> bool {
2237 self.as_str() == other.as_path()
2238 }
2239}
2240
2241#[stable(feature = "rust1", since = "1.0.0")]
2242impl Hash for PathBuf {
2243 fn hash<H: Hasher>(&self, h: &mut H) {
2244 self.as_path().hash(h)
2245 }
2246}
2247
2248#[stable(feature = "rust1", since = "1.0.0")]
2249impl Eq for PathBuf {}
2250
2251#[stable(feature = "rust1", since = "1.0.0")]
2252impl PartialOrd for PathBuf {
2253 #[inline]
2254 fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
2255 Some(compare_components(self.components(), other.components()))
2256 }
2257}
2258
2259#[stable(feature = "rust1", since = "1.0.0")]
2260impl Ord for PathBuf {
2261 #[inline]
2262 fn cmp(&self, other: &PathBuf) -> cmp::Ordering {
2263 compare_components(self.components(), other.components())
2264 }
2265}
2266
2267#[stable(feature = "rust1", since = "1.0.0")]
2268impl AsRef<OsStr> for PathBuf {
2269 #[inline]
2270 fn as_ref(&self) -> &OsStr {
2271 &self.inner[..]
2272 }
2273}
2274
2275/// A slice of a path (akin to [`str`]).
2276///
2277/// This type supports a number of operations for inspecting a path, including
2278/// breaking the path into its components (separated by `/` on Unix and by either
2279/// `/` or `\` on Windows), extracting the file name, determining whether the path
2280/// is absolute, and so on.
2281///
2282/// This is an *unsized* type, meaning that it must always be used behind a
2283/// pointer like `&` or [`Box`]. For an owned version of this type,
2284/// see [`PathBuf`].
2285///
2286/// More details about the overall approach can be found in
2287/// the [module documentation](self).
2288///
2289/// # Examples
2290///
2291/// ```
2292/// use std::path::Path;
2293/// use std::ffi::OsStr;
2294///
2295/// // Note: this example does work on Windows
2296/// let path = Path::new("./foo/bar.txt");
2297///
2298/// let parent = path.parent();
2299/// assert_eq!(parent, Some(Path::new("./foo")));
2300///
2301/// let file_stem = path.file_stem();
2302/// assert_eq!(file_stem, Some(OsStr::new("bar")));
2303///
2304/// let extension = path.extension();
2305/// assert_eq!(extension, Some(OsStr::new("txt")));
2306/// ```
2307#[cfg_attr(not(test), rustc_diagnostic_item = "Path")]
2308#[stable(feature = "rust1", since = "1.0.0")]
2309// `Path::new` and `impl CloneToUninit for Path` current implementation relies
2310// on `Path` being layout-compatible with `OsStr`.
2311// However, `Path` layout is considered an implementation detail and must not be relied upon.
2312#[repr(transparent)]
2313pub struct Path {
2314 inner: OsStr,
2315}
2316
2317/// An error returned from [`Path::strip_prefix`] if the prefix was not found.
2318///
2319/// This `struct` is created by the [`strip_prefix`] method on [`Path`].
2320/// See its documentation for more.
2321///
2322/// [`strip_prefix`]: Path::strip_prefix
2323#[derive(Debug, Clone, PartialEq, Eq)]
2324#[stable(since = "1.7.0", feature = "strip_prefix")]
2325pub struct StripPrefixError(());
2326
2327/// An error returned from [`Path::normalize_lexically`] if a `..` parent reference
2328/// would escape the path.
2329#[unstable(feature = "normalize_lexically", issue = "134694")]
2330#[derive(Debug, PartialEq)]
2331#[non_exhaustive]
2332pub struct NormalizeError;
2333
2334impl Path {
2335 // The following (private!) function allows construction of a path from a u8
2336 // slice, which is only safe when it is known to follow the OsStr encoding.
2337 unsafe fn from_u8_slice(s: &[u8]) -> &Path {
2338 unsafe { Path::new(OsStr::from_encoded_bytes_unchecked(s)) }
2339 }
2340 // The following (private!) function reveals the byte encoding used for OsStr.
2341 pub(crate) fn as_u8_slice(&self) -> &[u8] {
2342 self.inner.as_encoded_bytes()
2343 }
2344
2345 /// Directly wraps a string slice as a `Path` slice.
2346 ///
2347 /// This is a cost-free conversion.
2348 ///
2349 /// # Examples
2350 ///
2351 /// ```
2352 /// use std::path::Path;
2353 ///
2354 /// Path::new("foo.txt");
2355 /// ```
2356 ///
2357 /// You can create `Path`s from `String`s, or even other `Path`s:
2358 ///
2359 /// ```
2360 /// use std::path::Path;
2361 ///
2362 /// let string = String::from("foo.txt");
2363 /// let from_string = Path::new(&string);
2364 /// let from_path = Path::new(&from_string);
2365 /// assert_eq!(from_string, from_path);
2366 /// ```
2367 #[stable(feature = "rust1", since = "1.0.0")]
2368 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2369 pub const fn new<S: [const] AsRef<OsStr> + ?Sized>(s: &S) -> &Path {
2370 unsafe { &*(s.as_ref() as *const OsStr as *const Path) }
2371 }
2372
2373 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2374 const fn from_inner_mut(inner: &mut OsStr) -> &mut Path {
2375 // SAFETY: Path is just a wrapper around OsStr,
2376 // therefore converting &mut OsStr to &mut Path is safe.
2377 unsafe { &mut *(inner as *mut OsStr as *mut Path) }
2378 }
2379
2380 /// Yields the underlying [`OsStr`] slice.
2381 ///
2382 /// # Examples
2383 ///
2384 /// ```
2385 /// use std::path::Path;
2386 ///
2387 /// let os_str = Path::new("foo.txt").as_os_str();
2388 /// assert_eq!(os_str, std::ffi::OsStr::new("foo.txt"));
2389 /// ```
2390 #[stable(feature = "rust1", since = "1.0.0")]
2391 #[must_use]
2392 #[inline]
2393 pub fn as_os_str(&self) -> &OsStr {
2394 &self.inner
2395 }
2396
2397 /// Yields a mutable reference to the underlying [`OsStr`] slice.
2398 ///
2399 /// # Examples
2400 ///
2401 /// ```
2402 /// use std::path::{Path, PathBuf};
2403 ///
2404 /// let mut path = PathBuf::from("Foo.TXT");
2405 ///
2406 /// assert_ne!(path, Path::new("foo.txt"));
2407 ///
2408 /// path.as_mut_os_str().make_ascii_lowercase();
2409 /// assert_eq!(path, Path::new("foo.txt"));
2410 /// ```
2411 #[stable(feature = "path_as_mut_os_str", since = "1.70.0")]
2412 #[must_use]
2413 #[inline]
2414 pub fn as_mut_os_str(&mut self) -> &mut OsStr {
2415 &mut self.inner
2416 }
2417
2418 /// Yields a [`&str`] slice if the `Path` is valid unicode.
2419 ///
2420 /// This conversion may entail doing a check for UTF-8 validity.
2421 /// Note that validation is performed because non-UTF-8 strings are
2422 /// perfectly valid for some OS.
2423 ///
2424 /// [`&str`]: str
2425 ///
2426 /// # Examples
2427 ///
2428 /// ```
2429 /// use std::path::Path;
2430 ///
2431 /// let path = Path::new("foo.txt");
2432 /// assert_eq!(path.to_str(), Some("foo.txt"));
2433 /// ```
2434 #[stable(feature = "rust1", since = "1.0.0")]
2435 #[must_use = "this returns the result of the operation, \
2436 without modifying the original"]
2437 #[inline]
2438 pub fn to_str(&self) -> Option<&str> {
2439 self.inner.to_str()
2440 }
2441
2442 /// Converts a `Path` to a [`Cow<str>`].
2443 ///
2444 /// Any non-UTF-8 sequences are replaced with
2445 /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD].
2446 ///
2447 /// [U+FFFD]: super::char::REPLACEMENT_CHARACTER
2448 ///
2449 /// # Examples
2450 ///
2451 /// Calling `to_string_lossy` on a `Path` with valid unicode:
2452 ///
2453 /// ```
2454 /// use std::path::Path;
2455 ///
2456 /// let path = Path::new("foo.txt");
2457 /// assert_eq!(path.to_string_lossy(), "foo.txt");
2458 /// ```
2459 ///
2460 /// Had `path` contained invalid unicode, the `to_string_lossy` call might
2461 /// have returned `"fo�.txt"`.
2462 #[stable(feature = "rust1", since = "1.0.0")]
2463 #[must_use = "this returns the result of the operation, \
2464 without modifying the original"]
2465 #[inline]
2466 pub fn to_string_lossy(&self) -> Cow<'_, str> {
2467 self.inner.to_string_lossy()
2468 }
2469
2470 /// Converts a `Path` to an owned [`PathBuf`].
2471 ///
2472 /// # Examples
2473 ///
2474 /// ```
2475 /// use std::path::{Path, PathBuf};
2476 ///
2477 /// let path_buf = Path::new("foo.txt").to_path_buf();
2478 /// assert_eq!(path_buf, PathBuf::from("foo.txt"));
2479 /// ```
2480 #[rustc_conversion_suggestion]
2481 #[must_use = "this returns the result of the operation, \
2482 without modifying the original"]
2483 #[stable(feature = "rust1", since = "1.0.0")]
2484 #[cfg_attr(not(test), rustc_diagnostic_item = "path_to_pathbuf")]
2485 pub fn to_path_buf(&self) -> PathBuf {
2486 PathBuf::from(self.inner.to_os_string())
2487 }
2488
2489 /// Returns `true` if the `Path` is absolute, i.e., if it is independent of
2490 /// the current directory.
2491 ///
2492 /// * On Unix, a path is absolute if it starts with the root, so
2493 /// `is_absolute` and [`has_root`] are equivalent.
2494 ///
2495 /// * On Windows, a path is absolute if it has a prefix and starts with the
2496 /// root: `c:\windows` is absolute, while `c:temp` and `\temp` are not.
2497 ///
2498 /// # Examples
2499 ///
2500 /// ```
2501 /// use std::path::Path;
2502 ///
2503 /// assert!(!Path::new("foo.txt").is_absolute());
2504 /// ```
2505 ///
2506 /// [`has_root`]: Path::has_root
2507 #[stable(feature = "rust1", since = "1.0.0")]
2508 #[must_use]
2509 #[allow(deprecated)]
2510 pub fn is_absolute(&self) -> bool {
2511 sys::path::is_absolute(self)
2512 }
2513
2514 /// Returns `true` if the `Path` is relative, i.e., not absolute.
2515 ///
2516 /// See [`is_absolute`]'s documentation for more details.
2517 ///
2518 /// # Examples
2519 ///
2520 /// ```
2521 /// use std::path::Path;
2522 ///
2523 /// assert!(Path::new("foo.txt").is_relative());
2524 /// ```
2525 ///
2526 /// [`is_absolute`]: Path::is_absolute
2527 #[stable(feature = "rust1", since = "1.0.0")]
2528 #[must_use]
2529 #[inline]
2530 pub fn is_relative(&self) -> bool {
2531 !self.is_absolute()
2532 }
2533
2534 pub(crate) fn prefix(&self) -> Option<Prefix<'_>> {
2535 self.components().prefix
2536 }
2537
2538 /// Returns `true` if the `Path` has a root.
2539 ///
2540 /// * On Unix, a path has a root if it begins with `/`.
2541 ///
2542 /// * On Windows, a path has a root if it:
2543 /// * has no prefix and begins with a separator, e.g., `\windows`
2544 /// * has a prefix followed by a separator, e.g., `c:\windows` but not `c:windows`
2545 /// * has any non-disk prefix, e.g., `\\server\share`
2546 ///
2547 /// # Examples
2548 ///
2549 /// ```
2550 /// use std::path::Path;
2551 ///
2552 /// assert!(Path::new("/etc/passwd").has_root());
2553 /// ```
2554 #[stable(feature = "rust1", since = "1.0.0")]
2555 #[must_use]
2556 #[inline]
2557 pub fn has_root(&self) -> bool {
2558 self.components().has_root()
2559 }
2560
2561 /// Returns the `Path` without its final component, if there is one.
2562 ///
2563 /// This means it returns `Some("")` for relative paths with one component.
2564 ///
2565 /// Returns [`None`] if the path terminates in a root or prefix, or if it's
2566 /// the empty string.
2567 ///
2568 /// # Examples
2569 ///
2570 /// ```
2571 /// use std::path::Path;
2572 ///
2573 /// let path = Path::new("/foo/bar");
2574 /// let parent = path.parent().unwrap();
2575 /// assert_eq!(parent, Path::new("/foo"));
2576 ///
2577 /// let grand_parent = parent.parent().unwrap();
2578 /// assert_eq!(grand_parent, Path::new("/"));
2579 /// assert_eq!(grand_parent.parent(), None);
2580 ///
2581 /// let relative_path = Path::new("foo/bar");
2582 /// let parent = relative_path.parent();
2583 /// assert_eq!(parent, Some(Path::new("foo")));
2584 /// let grand_parent = parent.and_then(Path::parent);
2585 /// assert_eq!(grand_parent, Some(Path::new("")));
2586 /// let great_grand_parent = grand_parent.and_then(Path::parent);
2587 /// assert_eq!(great_grand_parent, None);
2588 /// ```
2589 #[stable(feature = "rust1", since = "1.0.0")]
2590 #[doc(alias = "dirname")]
2591 #[must_use]
2592 pub fn parent(&self) -> Option<&Path> {
2593 let mut comps = self.components();
2594 let comp = comps.next_back();
2595 comp.and_then(|p| match p {
2596 Component::Normal(_) | Component::CurDir | Component::ParentDir => {
2597 Some(comps.as_path())
2598 }
2599 _ => None,
2600 })
2601 }
2602
2603 /// Produces an iterator over `Path` and its ancestors.
2604 ///
2605 /// The iterator will yield the `Path` that is returned if the [`parent`] method is used zero
2606 /// or more times. If the [`parent`] method returns [`None`], the iterator will do likewise.
2607 /// The iterator will always yield at least one value, namely `Some(&self)`. Next it will yield
2608 /// `&self.parent()`, `&self.parent().and_then(Path::parent)` and so on.
2609 ///
2610 /// # Examples
2611 ///
2612 /// ```
2613 /// use std::path::Path;
2614 ///
2615 /// let mut ancestors = Path::new("/foo/bar").ancestors();
2616 /// assert_eq!(ancestors.next(), Some(Path::new("/foo/bar")));
2617 /// assert_eq!(ancestors.next(), Some(Path::new("/foo")));
2618 /// assert_eq!(ancestors.next(), Some(Path::new("/")));
2619 /// assert_eq!(ancestors.next(), None);
2620 ///
2621 /// let mut ancestors = Path::new("../foo/bar").ancestors();
2622 /// assert_eq!(ancestors.next(), Some(Path::new("../foo/bar")));
2623 /// assert_eq!(ancestors.next(), Some(Path::new("../foo")));
2624 /// assert_eq!(ancestors.next(), Some(Path::new("..")));
2625 /// assert_eq!(ancestors.next(), Some(Path::new("")));
2626 /// assert_eq!(ancestors.next(), None);
2627 /// ```
2628 ///
2629 /// [`parent`]: Path::parent
2630 #[stable(feature = "path_ancestors", since = "1.28.0")]
2631 #[inline]
2632 pub fn ancestors(&self) -> Ancestors<'_> {
2633 Ancestors { next: Some(&self) }
2634 }
2635
2636 /// Returns the final component of the `Path`, if there is one.
2637 ///
2638 /// If the path is a normal file, this is the file name. If it's the path of a directory, this
2639 /// is the directory name.
2640 ///
2641 /// Returns [`None`] if the path terminates in `..`.
2642 ///
2643 /// # Examples
2644 ///
2645 /// ```
2646 /// use std::path::Path;
2647 /// use std::ffi::OsStr;
2648 ///
2649 /// assert_eq!(Some(OsStr::new("bin")), Path::new("/usr/bin/").file_name());
2650 /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("tmp/foo.txt").file_name());
2651 /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.").file_name());
2652 /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.//").file_name());
2653 /// assert_eq!(None, Path::new("foo.txt/..").file_name());
2654 /// assert_eq!(None, Path::new("/").file_name());
2655 /// ```
2656 #[stable(feature = "rust1", since = "1.0.0")]
2657 #[doc(alias = "basename")]
2658 #[must_use]
2659 pub fn file_name(&self) -> Option<&OsStr> {
2660 self.components().next_back().and_then(|p| match p {
2661 Component::Normal(p) => Some(p),
2662 _ => None,
2663 })
2664 }
2665
2666 /// Returns a path that, when joined onto `base`, yields `self`.
2667 ///
2668 /// # Errors
2669 ///
2670 /// If `base` is not a prefix of `self` (i.e., [`starts_with`]
2671 /// returns `false`), returns [`Err`].
2672 ///
2673 /// [`starts_with`]: Path::starts_with
2674 ///
2675 /// # Examples
2676 ///
2677 /// ```
2678 /// use std::path::{Path, PathBuf};
2679 ///
2680 /// let path = Path::new("/test/haha/foo.txt");
2681 ///
2682 /// assert_eq!(path.strip_prefix("/"), Ok(Path::new("test/haha/foo.txt")));
2683 /// assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
2684 /// assert_eq!(path.strip_prefix("/test/"), Ok(Path::new("haha/foo.txt")));
2685 /// assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(Path::new("")));
2686 /// assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Path::new("")));
2687 ///
2688 /// assert!(path.strip_prefix("test").is_err());
2689 /// assert!(path.strip_prefix("/te").is_err());
2690 /// assert!(path.strip_prefix("/haha").is_err());
2691 ///
2692 /// let prefix = PathBuf::from("/test/");
2693 /// assert_eq!(path.strip_prefix(prefix), Ok(Path::new("haha/foo.txt")));
2694 /// ```
2695 #[stable(since = "1.7.0", feature = "path_strip_prefix")]
2696 pub fn strip_prefix<P>(&self, base: P) -> Result<&Path, StripPrefixError>
2697 where
2698 P: AsRef<Path>,
2699 {
2700 self._strip_prefix(base.as_ref())
2701 }
2702
2703 fn _strip_prefix(&self, base: &Path) -> Result<&Path, StripPrefixError> {
2704 iter_after(self.components(), base.components())
2705 .map(|c| c.as_path())
2706 .ok_or(StripPrefixError(()))
2707 }
2708
2709 /// Determines whether `base` is a prefix of `self`.
2710 ///
2711 /// Only considers whole path components to match.
2712 ///
2713 /// # Examples
2714 ///
2715 /// ```
2716 /// use std::path::Path;
2717 ///
2718 /// let path = Path::new("/etc/passwd");
2719 ///
2720 /// assert!(path.starts_with("/etc"));
2721 /// assert!(path.starts_with("/etc/"));
2722 /// assert!(path.starts_with("/etc/passwd"));
2723 /// assert!(path.starts_with("/etc/passwd/")); // extra slash is okay
2724 /// assert!(path.starts_with("/etc/passwd///")); // multiple extra slashes are okay
2725 ///
2726 /// assert!(!path.starts_with("/e"));
2727 /// assert!(!path.starts_with("/etc/passwd.txt"));
2728 ///
2729 /// assert!(!Path::new("/etc/foo.rs").starts_with("/etc/foo"));
2730 /// ```
2731 #[stable(feature = "rust1", since = "1.0.0")]
2732 #[must_use]
2733 pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool {
2734 self._starts_with(base.as_ref())
2735 }
2736
2737 fn _starts_with(&self, base: &Path) -> bool {
2738 iter_after(self.components(), base.components()).is_some()
2739 }
2740
2741 /// Determines whether `child` is a suffix of `self`.
2742 ///
2743 /// Only considers whole path components to match.
2744 ///
2745 /// # Examples
2746 ///
2747 /// ```
2748 /// use std::path::Path;
2749 ///
2750 /// let path = Path::new("/etc/resolv.conf");
2751 ///
2752 /// assert!(path.ends_with("resolv.conf"));
2753 /// assert!(path.ends_with("etc/resolv.conf"));
2754 /// assert!(path.ends_with("/etc/resolv.conf"));
2755 ///
2756 /// assert!(!path.ends_with("/resolv.conf"));
2757 /// assert!(!path.ends_with("conf")); // use .extension() instead
2758 /// ```
2759 #[stable(feature = "rust1", since = "1.0.0")]
2760 #[must_use]
2761 pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {
2762 self._ends_with(child.as_ref())
2763 }
2764
2765 fn _ends_with(&self, child: &Path) -> bool {
2766 iter_after(self.components().rev(), child.components().rev()).is_some()
2767 }
2768
2769 /// Checks whether the `Path` is empty.
2770 ///
2771 /// # Examples
2772 ///
2773 /// ```
2774 /// #![feature(path_is_empty)]
2775 /// use std::path::Path;
2776 ///
2777 /// let path = Path::new("");
2778 /// assert!(path.is_empty());
2779 ///
2780 /// let path = Path::new("foo");
2781 /// assert!(!path.is_empty());
2782 ///
2783 /// let path = Path::new(".");
2784 /// assert!(!path.is_empty());
2785 /// ```
2786 #[unstable(feature = "path_is_empty", issue = "148494")]
2787 pub fn is_empty(&self) -> bool {
2788 self.as_os_str().is_empty()
2789 }
2790
2791 /// Extracts the stem (non-extension) portion of [`self.file_name`].
2792 ///
2793 /// [`self.file_name`]: Path::file_name
2794 ///
2795 /// The stem is:
2796 ///
2797 /// * [`None`], if there is no file name;
2798 /// * The entire file name if there is no embedded `.`;
2799 /// * The entire file name if the file name begins with `.` and has no other `.`s within;
2800 /// * Otherwise, the portion of the file name before the final `.`
2801 ///
2802 /// # Examples
2803 ///
2804 /// ```
2805 /// use std::path::Path;
2806 ///
2807 /// assert_eq!("foo", Path::new("foo.rs").file_stem().unwrap());
2808 /// assert_eq!("foo.tar", Path::new("foo.tar.gz").file_stem().unwrap());
2809 /// ```
2810 ///
2811 /// # See Also
2812 /// This method is similar to [`Path::file_prefix`], which extracts the portion of the file name
2813 /// before the *first* `.`
2814 ///
2815 /// [`Path::file_prefix`]: Path::file_prefix
2816 ///
2817 #[stable(feature = "rust1", since = "1.0.0")]
2818 #[must_use]
2819 pub fn file_stem(&self) -> Option<&OsStr> {
2820 self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.or(after))
2821 }
2822
2823 /// Extracts the prefix of [`self.file_name`].
2824 ///
2825 /// The prefix is:
2826 ///
2827 /// * [`None`], if there is no file name;
2828 /// * The entire file name if there is no embedded `.`;
2829 /// * The portion of the file name before the first non-beginning `.`;
2830 /// * The entire file name if the file name begins with `.` and has no other `.`s within;
2831 /// * The portion of the file name before the second `.` if the file name begins with `.`
2832 ///
2833 /// [`self.file_name`]: Path::file_name
2834 ///
2835 /// # Examples
2836 ///
2837 /// ```
2838 /// use std::path::Path;
2839 ///
2840 /// assert_eq!("foo", Path::new("foo.rs").file_prefix().unwrap());
2841 /// assert_eq!("foo", Path::new("foo.tar.gz").file_prefix().unwrap());
2842 /// assert_eq!(".config", Path::new(".config").file_prefix().unwrap());
2843 /// assert_eq!(".config", Path::new(".config.toml").file_prefix().unwrap());
2844 /// ```
2845 ///
2846 /// # See Also
2847 /// This method is similar to [`Path::file_stem`], which extracts the portion of the file name
2848 /// before the *last* `.`
2849 ///
2850 /// [`Path::file_stem`]: Path::file_stem
2851 ///
2852 #[stable(feature = "path_file_prefix", since = "1.91.0")]
2853 #[must_use]
2854 pub fn file_prefix(&self) -> Option<&OsStr> {
2855 self.file_name().map(split_file_at_dot).and_then(|(before, _after)| Some(before))
2856 }
2857
2858 /// Extracts the extension (without the leading dot) of [`self.file_name`], if possible.
2859 ///
2860 /// The extension is:
2861 ///
2862 /// * [`None`], if there is no file name;
2863 /// * [`None`], if there is no embedded `.`;
2864 /// * [`None`], if the file name begins with `.` and has no other `.`s within;
2865 /// * Otherwise, the portion of the file name after the final `.`
2866 ///
2867 /// [`self.file_name`]: Path::file_name
2868 ///
2869 /// # Examples
2870 ///
2871 /// ```
2872 /// use std::path::Path;
2873 ///
2874 /// assert_eq!("rs", Path::new("foo.rs").extension().unwrap());
2875 /// assert_eq!("gz", Path::new("foo.tar.gz").extension().unwrap());
2876 /// ```
2877 #[stable(feature = "rust1", since = "1.0.0")]
2878 #[must_use]
2879 pub fn extension(&self) -> Option<&OsStr> {
2880 self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.and(after))
2881 }
2882
2883 /// Checks whether the path ends in a trailing [separator](MAIN_SEPARATOR).
2884 ///
2885 /// This is generally done to ensure that a path is treated as a directory, not a file,
2886 /// although it does not actually guarantee that such a path is a directory on the underlying
2887 /// file system.
2888 ///
2889 /// Despite this behavior, two paths are still considered the same in Rust whether they have a
2890 /// trailing separator or not.
2891 ///
2892 /// # Examples
2893 ///
2894 /// ```
2895 /// #![feature(path_trailing_sep)]
2896 /// use std::path::Path;
2897 ///
2898 /// assert!(Path::new("dir/").has_trailing_sep());
2899 /// assert!(!Path::new("file.rs").has_trailing_sep());
2900 /// ```
2901 #[unstable(feature = "path_trailing_sep", issue = "142503")]
2902 #[must_use]
2903 #[inline]
2904 pub fn has_trailing_sep(&self) -> bool {
2905 self.as_os_str().as_encoded_bytes().last().copied().is_some_and(is_sep_byte)
2906 }
2907
2908 /// Ensures that a path has a trailing [separator](MAIN_SEPARATOR),
2909 /// allocating a [`PathBuf`] if necessary.
2910 ///
2911 /// The resulting path will return true for [`has_trailing_sep`](Self::has_trailing_sep).
2912 ///
2913 /// # Examples
2914 ///
2915 /// ```
2916 /// #![feature(path_trailing_sep)]
2917 /// use std::ffi::OsStr;
2918 /// use std::path::Path;
2919 ///
2920 /// assert_eq!(Path::new("dir//").with_trailing_sep().as_os_str(), OsStr::new("dir//"));
2921 /// assert_eq!(Path::new("dir/").with_trailing_sep().as_os_str(), OsStr::new("dir/"));
2922 /// assert!(!Path::new("dir").has_trailing_sep());
2923 /// assert!(Path::new("dir").with_trailing_sep().has_trailing_sep());
2924 /// ```
2925 #[unstable(feature = "path_trailing_sep", issue = "142503")]
2926 #[must_use]
2927 #[inline]
2928 pub fn with_trailing_sep(&self) -> Cow<'_, Path> {
2929 if self.has_trailing_sep() { Cow::Borrowed(self) } else { Cow::Owned(self.join("")) }
2930 }
2931
2932 /// Trims a trailing [separator](MAIN_SEPARATOR) from a path, if possible.
2933 ///
2934 /// The resulting path will return false for [`has_trailing_sep`](Self::has_trailing_sep) for
2935 /// most paths.
2936 ///
2937 /// Some paths, like `/`, cannot be trimmed in this way.
2938 ///
2939 /// # Examples
2940 ///
2941 /// ```
2942 /// #![feature(path_trailing_sep)]
2943 /// use std::ffi::OsStr;
2944 /// use std::path::Path;
2945 ///
2946 /// assert_eq!(Path::new("dir//").trim_trailing_sep().as_os_str(), OsStr::new("dir"));
2947 /// assert_eq!(Path::new("dir/").trim_trailing_sep().as_os_str(), OsStr::new("dir"));
2948 /// assert_eq!(Path::new("dir").trim_trailing_sep().as_os_str(), OsStr::new("dir"));
2949 /// assert_eq!(Path::new("/").trim_trailing_sep().as_os_str(), OsStr::new("/"));
2950 /// assert_eq!(Path::new("//").trim_trailing_sep().as_os_str(), OsStr::new("//"));
2951 /// ```
2952 #[unstable(feature = "path_trailing_sep", issue = "142503")]
2953 #[must_use]
2954 #[inline]
2955 pub fn trim_trailing_sep(&self) -> &Path {
2956 if self.has_trailing_sep() && (!self.has_root() || self.parent().is_some()) {
2957 let mut bytes = self.inner.as_encoded_bytes();
2958 while let Some((last, init)) = bytes.split_last()
2959 && is_sep_byte(*last)
2960 {
2961 bytes = init;
2962 }
2963
2964 // SAFETY: Trimming trailing ASCII bytes will retain the validity of the string.
2965 Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(bytes) })
2966 } else {
2967 self
2968 }
2969 }
2970
2971 /// Creates an owned [`PathBuf`] with `path` adjoined to `self`.
2972 ///
2973 /// If `path` is absolute, it replaces the current path.
2974 ///
2975 /// See [`PathBuf::push`] for more details on what it means to adjoin a path.
2976 ///
2977 /// # Examples
2978 ///
2979 /// ```
2980 /// use std::path::{Path, PathBuf};
2981 ///
2982 /// assert_eq!(Path::new("/etc").join("passwd"), PathBuf::from("/etc/passwd"));
2983 /// assert_eq!(Path::new("/etc").join("/bin/sh"), PathBuf::from("/bin/sh"));
2984 /// ```
2985 #[stable(feature = "rust1", since = "1.0.0")]
2986 #[must_use]
2987 pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
2988 self._join(path.as_ref())
2989 }
2990
2991 fn _join(&self, path: &Path) -> PathBuf {
2992 let mut buf = self.to_path_buf();
2993 buf.push(path);
2994 buf
2995 }
2996
2997 /// Creates an owned [`PathBuf`] like `self` but with the given file name.
2998 ///
2999 /// See [`PathBuf::set_file_name`] for more details.
3000 ///
3001 /// # Examples
3002 ///
3003 /// ```
3004 /// use std::path::{Path, PathBuf};
3005 ///
3006 /// let path = Path::new("/tmp/foo.png");
3007 /// assert_eq!(path.with_file_name("bar"), PathBuf::from("/tmp/bar"));
3008 /// assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt"));
3009 ///
3010 /// let path = Path::new("/tmp");
3011 /// assert_eq!(path.with_file_name("var"), PathBuf::from("/var"));
3012 /// ```
3013 #[stable(feature = "rust1", since = "1.0.0")]
3014 #[must_use]
3015 pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf {
3016 self._with_file_name(file_name.as_ref())
3017 }
3018
3019 fn _with_file_name(&self, file_name: &OsStr) -> PathBuf {
3020 let mut buf = self.to_path_buf();
3021 buf.set_file_name(file_name);
3022 buf
3023 }
3024
3025 /// Creates an owned [`PathBuf`] like `self` but with the given extension.
3026 ///
3027 /// See [`PathBuf::set_extension`] for more details.
3028 ///
3029 /// # Examples
3030 ///
3031 /// ```
3032 /// use std::path::Path;
3033 ///
3034 /// let path = Path::new("foo.rs");
3035 /// assert_eq!(path.with_extension("txt"), Path::new("foo.txt"));
3036 /// assert_eq!(path.with_extension(""), Path::new("foo"));
3037 /// ```
3038 ///
3039 /// Handling multiple extensions:
3040 ///
3041 /// ```
3042 /// use std::path::Path;
3043 ///
3044 /// let path = Path::new("foo.tar.gz");
3045 /// assert_eq!(path.with_extension("xz"), Path::new("foo.tar.xz"));
3046 /// assert_eq!(path.with_extension("").with_extension("txt"), Path::new("foo.txt"));
3047 /// ```
3048 ///
3049 /// Adding an extension where one did not exist:
3050 ///
3051 /// ```
3052 /// use std::path::Path;
3053 ///
3054 /// let path = Path::new("foo");
3055 /// assert_eq!(path.with_extension("rs"), Path::new("foo.rs"));
3056 /// ```
3057 #[stable(feature = "rust1", since = "1.0.0")]
3058 pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
3059 self._with_extension(extension.as_ref())
3060 }
3061
3062 fn _with_extension(&self, extension: &OsStr) -> PathBuf {
3063 let self_len = self.as_os_str().len();
3064 let self_bytes = self.as_os_str().as_encoded_bytes();
3065
3066 let (new_capacity, slice_to_copy) = match self.extension() {
3067 None => {
3068 // Enough capacity for the extension and the dot
3069 let capacity = self_len + extension.len() + 1;
3070 let whole_path = self_bytes;
3071 (capacity, whole_path)
3072 }
3073 Some(previous_extension) => {
3074 let capacity = self_len + extension.len() - previous_extension.len();
3075 let path_till_dot = &self_bytes[..self_len - previous_extension.len()];
3076 (capacity, path_till_dot)
3077 }
3078 };
3079
3080 let mut new_path = PathBuf::with_capacity(new_capacity);
3081 // SAFETY: The path is empty, so cannot have surrogate halves.
3082 unsafe { new_path.inner.extend_from_slice_unchecked(slice_to_copy) };
3083 new_path.set_extension(extension);
3084 new_path
3085 }
3086
3087 /// Creates an owned [`PathBuf`] like `self` but with the extension added.
3088 ///
3089 /// See [`PathBuf::add_extension`] for more details.
3090 ///
3091 /// # Examples
3092 ///
3093 /// ```
3094 /// use std::path::{Path, PathBuf};
3095 ///
3096 /// let path = Path::new("foo.rs");
3097 /// assert_eq!(path.with_added_extension("txt"), PathBuf::from("foo.rs.txt"));
3098 ///
3099 /// let path = Path::new("foo.tar.gz");
3100 /// assert_eq!(path.with_added_extension(""), PathBuf::from("foo.tar.gz"));
3101 /// assert_eq!(path.with_added_extension("xz"), PathBuf::from("foo.tar.gz.xz"));
3102 /// assert_eq!(path.with_added_extension("").with_added_extension("txt"), PathBuf::from("foo.tar.gz.txt"));
3103 /// ```
3104 #[stable(feature = "path_add_extension", since = "1.91.0")]
3105 pub fn with_added_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
3106 let mut new_path = self.to_path_buf();
3107 new_path.add_extension(extension);
3108 new_path
3109 }
3110
3111 /// Produces an iterator over the [`Component`]s of the path.
3112 ///
3113 /// When parsing the path, there is a small amount of normalization:
3114 ///
3115 /// * Repeated separators are ignored, so `a/b` and `a//b` both have
3116 /// `a` and `b` as components.
3117 ///
3118 /// * Occurrences of `.` are normalized away, except if they are at the
3119 /// beginning of the path. For example, `a/./b`, `a/b/`, `a/b/.` and
3120 /// `a/b` all have `a` and `b` as components, but `./a/b` starts with
3121 /// an additional [`CurDir`] component.
3122 ///
3123 /// * Trailing separators are normalized away, so `/a/b` and `/a/b/` are equivalent.
3124 ///
3125 /// Note that no other normalization takes place; in particular, `a/c`
3126 /// and `a/b/../c` are distinct, to account for the possibility that `b`
3127 /// is a symbolic link (so its parent isn't `a`).
3128 ///
3129 /// # Examples
3130 ///
3131 /// ```
3132 /// use std::path::{Path, Component};
3133 /// use std::ffi::OsStr;
3134 ///
3135 /// let mut components = Path::new("/tmp/foo.txt").components();
3136 ///
3137 /// assert_eq!(components.next(), Some(Component::RootDir));
3138 /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("tmp"))));
3139 /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("foo.txt"))));
3140 /// assert_eq!(components.next(), None)
3141 /// ```
3142 ///
3143 /// [`CurDir`]: Component::CurDir
3144 #[stable(feature = "rust1", since = "1.0.0")]
3145 pub fn components(&self) -> Components<'_> {
3146 let prefix = parse_prefix(self.as_os_str());
3147 Components {
3148 path: self.as_u8_slice(),
3149 prefix,
3150 has_physical_root: has_physical_root(self.as_u8_slice(), prefix),
3151 // use a platform-specific initial state to avoid one turn of
3152 // the state-machine when the platform doesn't have a Prefix.
3153 front: const { if HAS_PREFIXES { State::Prefix } else { State::StartDir } },
3154 back: State::Body,
3155 }
3156 }
3157
3158 /// Produces an iterator over the path's components viewed as [`OsStr`]
3159 /// slices.
3160 ///
3161 /// For more information about the particulars of how the path is separated
3162 /// into components, see [`components`].
3163 ///
3164 /// [`components`]: Path::components
3165 ///
3166 /// # Examples
3167 ///
3168 /// ```
3169 /// use std::path::{self, Path};
3170 /// use std::ffi::OsStr;
3171 ///
3172 /// let mut it = Path::new("/tmp/foo.txt").iter();
3173 /// assert_eq!(it.next(), Some(OsStr::new(&path::MAIN_SEPARATOR.to_string())));
3174 /// assert_eq!(it.next(), Some(OsStr::new("tmp")));
3175 /// assert_eq!(it.next(), Some(OsStr::new("foo.txt")));
3176 /// assert_eq!(it.next(), None)
3177 /// ```
3178 #[stable(feature = "rust1", since = "1.0.0")]
3179 #[inline]
3180 pub fn iter(&self) -> Iter<'_> {
3181 Iter { inner: self.components() }
3182 }
3183
3184 /// Returns an object that implements [`Display`] for safely printing paths
3185 /// that may contain non-Unicode data. This may perform lossy conversion,
3186 /// depending on the platform. If you would like an implementation which
3187 /// escapes the path please use [`Debug`] instead.
3188 ///
3189 /// [`Display`]: fmt::Display
3190 /// [`Debug`]: fmt::Debug
3191 ///
3192 /// # Examples
3193 ///
3194 /// ```
3195 /// use std::path::Path;
3196 ///
3197 /// let path = Path::new("/tmp/foo.rs");
3198 ///
3199 /// println!("{}", path.display());
3200 /// ```
3201 #[stable(feature = "rust1", since = "1.0.0")]
3202 #[must_use = "this does not display the path, \
3203 it returns an object that can be displayed"]
3204 #[inline]
3205 pub fn display(&self) -> Display<'_> {
3206 Display { inner: self.inner.display() }
3207 }
3208
3209 /// Queries the file system to get information about a file, directory, etc.
3210 ///
3211 /// This function will traverse symbolic links to query information about the
3212 /// destination file.
3213 ///
3214 /// This is an alias to [`fs::metadata`].
3215 ///
3216 /// # Examples
3217 ///
3218 /// ```no_run
3219 /// use std::path::Path;
3220 ///
3221 /// let path = Path::new("/Minas/tirith");
3222 /// let metadata = path.metadata().expect("metadata call failed");
3223 /// println!("{:?}", metadata.file_type());
3224 /// ```
3225 #[stable(feature = "path_ext", since = "1.5.0")]
3226 #[inline]
3227 pub fn metadata(&self) -> io::Result<fs::Metadata> {
3228 fs::metadata(self)
3229 }
3230
3231 /// Queries the metadata about a file without following symlinks.
3232 ///
3233 /// This is an alias to [`fs::symlink_metadata`].
3234 ///
3235 /// # Examples
3236 ///
3237 /// ```no_run
3238 /// use std::path::Path;
3239 ///
3240 /// let path = Path::new("/Minas/tirith");
3241 /// let metadata = path.symlink_metadata().expect("symlink_metadata call failed");
3242 /// println!("{:?}", metadata.file_type());
3243 /// ```
3244 #[stable(feature = "path_ext", since = "1.5.0")]
3245 #[inline]
3246 pub fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
3247 fs::symlink_metadata(self)
3248 }
3249
3250 /// Returns the canonical, absolute form of the path with all intermediate
3251 /// components normalized and symbolic links resolved.
3252 ///
3253 /// This is an alias to [`fs::canonicalize`].
3254 ///
3255 /// # Errors
3256 ///
3257 /// This method will return an error in the following situations, but is not
3258 /// limited to just these cases:
3259 ///
3260 /// * `path` does not exist.
3261 /// * A non-final component in path is not a directory.
3262 ///
3263 /// # Examples
3264 ///
3265 /// ```no_run
3266 /// use std::path::{Path, PathBuf};
3267 ///
3268 /// let path = Path::new("/foo/test/../test/bar.rs");
3269 /// assert_eq!(path.canonicalize().unwrap(), PathBuf::from("/foo/test/bar.rs"));
3270 /// ```
3271 #[stable(feature = "path_ext", since = "1.5.0")]
3272 #[inline]
3273 pub fn canonicalize(&self) -> io::Result<PathBuf> {
3274 fs::canonicalize(self)
3275 }
3276
3277 /// Normalize a path, including `..` without traversing the filesystem.
3278 ///
3279 /// Returns an error if normalization would leave leading `..` components.
3280 ///
3281 /// <div class="warning">
3282 ///
3283 /// This function always resolves `..` to the "lexical" parent.
3284 /// That is "a/b/../c" will always resolve to `a/c` which can change the meaning of the path.
3285 /// In particular, `a/c` and `a/b/../c` are distinct on many systems because `b` may be a symbolic link, so its parent isn't `a`.
3286 ///
3287 /// </div>
3288 ///
3289 /// [`path::absolute`](absolute) is an alternative that preserves `..`.
3290 /// Or [`Path::canonicalize`] can be used to resolve any `..` by querying the filesystem.
3291 #[unstable(feature = "normalize_lexically", issue = "134694")]
3292 pub fn normalize_lexically(&self) -> Result<PathBuf, NormalizeError> {
3293 let mut lexical = PathBuf::new();
3294 let mut iter = self.components().peekable();
3295
3296 // Find the root, if any, and add it to the lexical path.
3297 // Here we treat the Windows path "C:\" as a single "root" even though
3298 // `components` splits it into two: (Prefix, RootDir).
3299 let root = match iter.peek() {
3300 Some(Component::ParentDir) => return Err(NormalizeError),
3301 Some(p @ Component::RootDir) | Some(p @ Component::CurDir) => {
3302 lexical.push(p);
3303 iter.next();
3304 lexical.as_os_str().len()
3305 }
3306 Some(Component::Prefix(prefix)) => {
3307 lexical.push(prefix.as_os_str());
3308 iter.next();
3309 if let Some(p @ Component::RootDir) = iter.peek() {
3310 lexical.push(p);
3311 iter.next();
3312 }
3313 lexical.as_os_str().len()
3314 }
3315 None => return Ok(PathBuf::new()),
3316 Some(Component::Normal(_)) => 0,
3317 };
3318
3319 for component in iter {
3320 match component {
3321 Component::RootDir => unreachable!(),
3322 Component::Prefix(_) => return Err(NormalizeError),
3323 Component::CurDir => continue,
3324 Component::ParentDir => {
3325 // It's an error if ParentDir causes us to go above the "root".
3326 if lexical.as_os_str().len() == root {
3327 return Err(NormalizeError);
3328 } else {
3329 lexical.pop();
3330 }
3331 }
3332 Component::Normal(path) => lexical.push(path),
3333 }
3334 }
3335 Ok(lexical)
3336 }
3337
3338 /// Reads a symbolic link, returning the file that the link points to.
3339 ///
3340 /// This is an alias to [`fs::read_link`].
3341 ///
3342 /// # Examples
3343 ///
3344 /// ```no_run
3345 /// use std::path::Path;
3346 ///
3347 /// let path = Path::new("/laputa/sky_castle.rs");
3348 /// let path_link = path.read_link().expect("read_link call failed");
3349 /// ```
3350 #[stable(feature = "path_ext", since = "1.5.0")]
3351 #[inline]
3352 pub fn read_link(&self) -> io::Result<PathBuf> {
3353 fs::read_link(self)
3354 }
3355
3356 /// Returns an iterator over the entries within a directory.
3357 ///
3358 /// The iterator will yield instances of <code>[io::Result]<[fs::DirEntry]></code>. New
3359 /// errors may be encountered after an iterator is initially constructed.
3360 ///
3361 /// This is an alias to [`fs::read_dir`].
3362 ///
3363 /// # Examples
3364 ///
3365 /// ```no_run
3366 /// use std::path::Path;
3367 ///
3368 /// let path = Path::new("/laputa");
3369 /// for entry in path.read_dir().expect("read_dir call failed") {
3370 /// if let Ok(entry) = entry {
3371 /// println!("{:?}", entry.path());
3372 /// }
3373 /// }
3374 /// ```
3375 #[stable(feature = "path_ext", since = "1.5.0")]
3376 #[inline]
3377 pub fn read_dir(&self) -> io::Result<fs::ReadDir> {
3378 fs::read_dir(self)
3379 }
3380
3381 /// Returns `true` if the path points at an existing entity.
3382 ///
3383 /// Warning: this method may be error-prone, consider using [`try_exists()`] instead!
3384 /// It also has a risk of introducing time-of-check to time-of-use ([TOCTOU]) bugs.
3385 ///
3386 /// This function will traverse symbolic links to query information about the
3387 /// destination file.
3388 ///
3389 /// If you cannot access the metadata of the file, e.g. because of a
3390 /// permission error or broken symbolic links, this will return `false`.
3391 ///
3392 /// # Examples
3393 ///
3394 /// ```no_run
3395 /// use std::path::Path;
3396 /// assert!(!Path::new("does_not_exist.txt").exists());
3397 /// ```
3398 ///
3399 /// # See Also
3400 ///
3401 /// This is a convenience function that coerces errors to false. If you want to
3402 /// check errors, call [`Path::try_exists`].
3403 ///
3404 /// [`try_exists()`]: Self::try_exists
3405 /// [TOCTOU]: fs#time-of-check-to-time-of-use-toctou
3406 #[stable(feature = "path_ext", since = "1.5.0")]
3407 #[must_use]
3408 #[inline]
3409 pub fn exists(&self) -> bool {
3410 fs::metadata(self).is_ok()
3411 }
3412
3413 /// Returns `Ok(true)` if the path points at an existing entity.
3414 ///
3415 /// This function will traverse symbolic links to query information about the
3416 /// destination file. In case of broken symbolic links this will return `Ok(false)`.
3417 ///
3418 /// [`Path::exists()`] only checks whether or not a path was both found and readable. By
3419 /// contrast, `try_exists` will return `Ok(true)` or `Ok(false)`, respectively, if the path
3420 /// was _verified_ to exist or not exist. If its existence can neither be confirmed nor
3421 /// denied, it will propagate an `Err(_)` instead. This can be the case if e.g. listing
3422 /// permission is denied on one of the parent directories.
3423 ///
3424 /// Note that while this avoids some pitfalls of the `exists()` method, it still can not
3425 /// prevent time-of-check to time-of-use ([TOCTOU]) bugs. You should only use it in scenarios
3426 /// where those bugs are not an issue.
3427 ///
3428 /// This is an alias for [`std::fs::exists`](crate::fs::exists).
3429 ///
3430 /// # Examples
3431 ///
3432 /// ```no_run
3433 /// use std::path::Path;
3434 /// assert!(!Path::new("does_not_exist.txt").try_exists().expect("Can't check existence of file does_not_exist.txt"));
3435 /// assert!(Path::new("/root/secret_file.txt").try_exists().is_err());
3436 /// ```
3437 ///
3438 /// [TOCTOU]: fs#time-of-check-to-time-of-use-toctou
3439 /// [`exists()`]: Self::exists
3440 #[stable(feature = "path_try_exists", since = "1.63.0")]
3441 #[inline]
3442 pub fn try_exists(&self) -> io::Result<bool> {
3443 fs::exists(self)
3444 }
3445
3446 /// Returns `true` if the path exists on disk and is pointing at a regular file.
3447 ///
3448 /// This function will traverse symbolic links to query information about the
3449 /// destination file.
3450 ///
3451 /// If you cannot access the metadata of the file, e.g. because of a
3452 /// permission error or broken symbolic links, this will return `false`.
3453 ///
3454 /// # Examples
3455 ///
3456 /// ```no_run
3457 /// use std::path::Path;
3458 /// assert_eq!(Path::new("./is_a_directory/").is_file(), false);
3459 /// assert_eq!(Path::new("a_file.txt").is_file(), true);
3460 /// ```
3461 ///
3462 /// # See Also
3463 ///
3464 /// This is a convenience function that coerces errors to false. If you want to
3465 /// check errors, call [`fs::metadata`] and handle its [`Result`]. Then call
3466 /// [`fs::Metadata::is_file`] if it was [`Ok`].
3467 ///
3468 /// When the goal is simply to read from (or write to) the source, the most
3469 /// reliable way to test the source can be read (or written to) is to open
3470 /// it. Only using `is_file` can break workflows like `diff <( prog_a )` on
3471 /// a Unix-like system for example. See [`fs::File::open`] or
3472 /// [`fs::OpenOptions::open`] for more information.
3473 #[stable(feature = "path_ext", since = "1.5.0")]
3474 #[must_use]
3475 pub fn is_file(&self) -> bool {
3476 fs::metadata(self).map(|m| m.is_file()).unwrap_or(false)
3477 }
3478
3479 /// Returns `true` if the path exists on disk and is pointing at a directory.
3480 ///
3481 /// This function will traverse symbolic links to query information about the
3482 /// destination file.
3483 ///
3484 /// If you cannot access the metadata of the file, e.g. because of a
3485 /// permission error or broken symbolic links, this will return `false`.
3486 ///
3487 /// # Examples
3488 ///
3489 /// ```no_run
3490 /// use std::path::Path;
3491 /// assert_eq!(Path::new("./is_a_directory/").is_dir(), true);
3492 /// assert_eq!(Path::new("a_file.txt").is_dir(), false);
3493 /// ```
3494 ///
3495 /// # See Also
3496 ///
3497 /// This is a convenience function that coerces errors to false. If you want to
3498 /// check errors, call [`fs::metadata`] and handle its [`Result`]. Then call
3499 /// [`fs::Metadata::is_dir`] if it was [`Ok`].
3500 #[stable(feature = "path_ext", since = "1.5.0")]
3501 #[must_use]
3502 pub fn is_dir(&self) -> bool {
3503 fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
3504 }
3505
3506 /// Returns `true` if the path exists on disk and is pointing at a symbolic link.
3507 ///
3508 /// This function will not traverse symbolic links.
3509 /// In case of a broken symbolic link this will also return true.
3510 ///
3511 /// If you cannot access the directory containing the file, e.g., because of a
3512 /// permission error, this will return false.
3513 ///
3514 /// # Examples
3515 ///
3516 /// ```rust,no_run
3517 /// # #[cfg(unix)] {
3518 /// use std::path::Path;
3519 /// use std::os::unix::fs::symlink;
3520 ///
3521 /// let link_path = Path::new("link");
3522 /// symlink("/origin_does_not_exist/", link_path).unwrap();
3523 /// assert_eq!(link_path.is_symlink(), true);
3524 /// assert_eq!(link_path.exists(), false);
3525 /// # }
3526 /// ```
3527 ///
3528 /// # See Also
3529 ///
3530 /// This is a convenience function that coerces errors to false. If you want to
3531 /// check errors, call [`fs::symlink_metadata`] and handle its [`Result`]. Then call
3532 /// [`fs::Metadata::is_symlink`] if it was [`Ok`].
3533 #[must_use]
3534 #[stable(feature = "is_symlink", since = "1.58.0")]
3535 pub fn is_symlink(&self) -> bool {
3536 fs::symlink_metadata(self).map(|m| m.is_symlink()).unwrap_or(false)
3537 }
3538
3539 /// Converts a [`Box<Path>`](Box) into a [`PathBuf`] without copying or
3540 /// allocating.
3541 #[stable(feature = "into_boxed_path", since = "1.20.0")]
3542 #[must_use = "`self` will be dropped if the result is not used"]
3543 pub fn into_path_buf(self: Box<Self>) -> PathBuf {
3544 let rw = Box::into_raw(self) as *mut OsStr;
3545 let inner = unsafe { Box::from_raw(rw) };
3546 PathBuf { inner: OsString::from(inner) }
3547 }
3548}
3549
3550#[unstable(feature = "clone_to_uninit", issue = "126799")]
3551unsafe impl CloneToUninit for Path {
3552 #[inline]
3553 #[cfg_attr(debug_assertions, track_caller)]
3554 unsafe fn clone_to_uninit(&self, dst: *mut u8) {
3555 // SAFETY: Path is just a transparent wrapper around OsStr
3556 unsafe { self.inner.clone_to_uninit(dst) }
3557 }
3558}
3559
3560#[stable(feature = "rust1", since = "1.0.0")]
3561#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
3562impl const AsRef<OsStr> for Path {
3563 #[inline]
3564 fn as_ref(&self) -> &OsStr {
3565 &self.inner
3566 }
3567}
3568
3569#[stable(feature = "rust1", since = "1.0.0")]
3570impl fmt::Debug for Path {
3571 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
3572 fmt::Debug::fmt(&self.inner, formatter)
3573 }
3574}
3575
3576/// Helper struct for safely printing paths with [`format!`] and `{}`.
3577///
3578/// A [`Path`] might contain non-Unicode data. This `struct` implements the
3579/// [`Display`] trait in a way that mitigates that. It is created by the
3580/// [`display`](Path::display) method on [`Path`]. This may perform lossy
3581/// conversion, depending on the platform. If you would like an implementation
3582/// which escapes the path please use [`Debug`] instead.
3583///
3584/// # Examples
3585///
3586/// ```
3587/// use std::path::Path;
3588///
3589/// let path = Path::new("/tmp/foo.rs");
3590///
3591/// println!("{}", path.display());
3592/// ```
3593///
3594/// [`Display`]: fmt::Display
3595/// [`format!`]: crate::format
3596#[stable(feature = "rust1", since = "1.0.0")]
3597pub struct Display<'a> {
3598 inner: os_str::Display<'a>,
3599}
3600
3601#[stable(feature = "rust1", since = "1.0.0")]
3602impl fmt::Debug for Display<'_> {
3603 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3604 fmt::Debug::fmt(&self.inner, f)
3605 }
3606}
3607
3608#[stable(feature = "rust1", since = "1.0.0")]
3609impl fmt::Display for Display<'_> {
3610 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3611 fmt::Display::fmt(&self.inner, f)
3612 }
3613}
3614
3615#[stable(feature = "rust1", since = "1.0.0")]
3616impl PartialEq for Path {
3617 #[inline]
3618 fn eq(&self, other: &Path) -> bool {
3619 self.components() == other.components()
3620 }
3621}
3622
3623#[stable(feature = "eq_str_for_path", since = "1.91.0")]
3624impl cmp::PartialEq<str> for Path {
3625 #[inline]
3626 fn eq(&self, other: &str) -> bool {
3627 let other: &OsStr = other.as_ref();
3628 self == other
3629 }
3630}
3631
3632#[stable(feature = "eq_str_for_path", since = "1.91.0")]
3633impl cmp::PartialEq<Path> for str {
3634 #[inline]
3635 fn eq(&self, other: &Path) -> bool {
3636 other == self
3637 }
3638}
3639
3640#[stable(feature = "eq_str_for_path", since = "1.91.0")]
3641impl cmp::PartialEq<String> for Path {
3642 #[inline]
3643 fn eq(&self, other: &String) -> bool {
3644 self == other.as_str()
3645 }
3646}
3647
3648#[stable(feature = "eq_str_for_path", since = "1.91.0")]
3649impl cmp::PartialEq<Path> for String {
3650 #[inline]
3651 fn eq(&self, other: &Path) -> bool {
3652 self.as_str() == other
3653 }
3654}
3655
3656#[stable(feature = "rust1", since = "1.0.0")]
3657impl Hash for Path {
3658 fn hash<H: Hasher>(&self, h: &mut H) {
3659 let bytes = self.as_u8_slice();
3660 let (prefix_len, verbatim) = match parse_prefix(&self.inner) {
3661 Some(prefix) => {
3662 prefix.hash(h);
3663 (prefix.len(), prefix.is_verbatim())
3664 }
3665 None => (0, false),
3666 };
3667 let bytes = &bytes[prefix_len..];
3668
3669 let mut component_start = 0;
3670 // track some extra state to avoid prefix collisions.
3671 // ["foo", "bar"] and ["foobar"], will have the same payload bytes
3672 // but result in different chunk_bits
3673 let mut chunk_bits: usize = 0;
3674
3675 for i in 0..bytes.len() {
3676 let is_sep = if verbatim { is_verbatim_sep(bytes[i]) } else { is_sep_byte(bytes[i]) };
3677 if is_sep {
3678 if i > component_start {
3679 let to_hash = &bytes[component_start..i];
3680 chunk_bits = chunk_bits.wrapping_add(to_hash.len());
3681 chunk_bits = chunk_bits.rotate_right(2);
3682 h.write(to_hash);
3683 }
3684
3685 // skip over separator and optionally a following CurDir item
3686 // since components() would normalize these away.
3687 component_start = i + 1;
3688
3689 let tail = &bytes[component_start..];
3690
3691 if !verbatim {
3692 component_start += match tail {
3693 [b'.'] => 1,
3694 [b'.', sep, ..] if is_sep_byte(*sep) => 1,
3695 _ => 0,
3696 };
3697 }
3698 }
3699 }
3700
3701 if component_start < bytes.len() {
3702 let to_hash = &bytes[component_start..];
3703 chunk_bits = chunk_bits.wrapping_add(to_hash.len());
3704 chunk_bits = chunk_bits.rotate_right(2);
3705 h.write(to_hash);
3706 }
3707
3708 h.write_usize(chunk_bits);
3709 }
3710}
3711
3712#[stable(feature = "rust1", since = "1.0.0")]
3713impl Eq for Path {}
3714
3715#[stable(feature = "rust1", since = "1.0.0")]
3716impl PartialOrd for Path {
3717 #[inline]
3718 fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
3719 Some(compare_components(self.components(), other.components()))
3720 }
3721}
3722
3723#[stable(feature = "rust1", since = "1.0.0")]
3724impl Ord for Path {
3725 #[inline]
3726 fn cmp(&self, other: &Path) -> cmp::Ordering {
3727 compare_components(self.components(), other.components())
3728 }
3729}
3730
3731#[stable(feature = "rust1", since = "1.0.0")]
3732#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
3733impl const AsRef<Path> for Path {
3734 #[inline]
3735 fn as_ref(&self) -> &Path {
3736 self
3737 }
3738}
3739
3740#[stable(feature = "rust1", since = "1.0.0")]
3741#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
3742impl const AsRef<Path> for OsStr {
3743 #[inline]
3744 fn as_ref(&self) -> &Path {
3745 Path::new(self)
3746 }
3747}
3748
3749#[stable(feature = "cow_os_str_as_ref_path", since = "1.8.0")]
3750impl AsRef<Path> for Cow<'_, OsStr> {
3751 #[inline]
3752 fn as_ref(&self) -> &Path {
3753 Path::new(self)
3754 }
3755}
3756
3757#[stable(feature = "rust1", since = "1.0.0")]
3758impl AsRef<Path> for OsString {
3759 #[inline]
3760 fn as_ref(&self) -> &Path {
3761 Path::new(self)
3762 }
3763}
3764
3765#[stable(feature = "rust1", since = "1.0.0")]
3766impl AsRef<Path> for str {
3767 #[inline]
3768 fn as_ref(&self) -> &Path {
3769 Path::new(self)
3770 }
3771}
3772
3773#[stable(feature = "rust1", since = "1.0.0")]
3774impl AsRef<Path> for String {
3775 #[inline]
3776 fn as_ref(&self) -> &Path {
3777 Path::new(self)
3778 }
3779}
3780
3781#[stable(feature = "rust1", since = "1.0.0")]
3782impl AsRef<Path> for PathBuf {
3783 #[inline]
3784 fn as_ref(&self) -> &Path {
3785 self
3786 }
3787}
3788
3789#[stable(feature = "path_into_iter", since = "1.6.0")]
3790impl<'a> IntoIterator for &'a PathBuf {
3791 type Item = &'a OsStr;
3792 type IntoIter = Iter<'a>;
3793 #[inline]
3794 fn into_iter(self) -> Iter<'a> {
3795 self.iter()
3796 }
3797}
3798
3799#[stable(feature = "path_into_iter", since = "1.6.0")]
3800impl<'a> IntoIterator for &'a Path {
3801 type Item = &'a OsStr;
3802 type IntoIter = Iter<'a>;
3803 #[inline]
3804 fn into_iter(self) -> Iter<'a> {
3805 self.iter()
3806 }
3807}
3808
3809macro_rules! impl_cmp {
3810 (<$($life:lifetime),*> $lhs:ty, $rhs: ty) => {
3811 #[stable(feature = "partialeq_path", since = "1.6.0")]
3812 impl<$($life),*> PartialEq<$rhs> for $lhs {
3813 #[inline]
3814 fn eq(&self, other: &$rhs) -> bool {
3815 <Path as PartialEq>::eq(self, other)
3816 }
3817 }
3818
3819 #[stable(feature = "partialeq_path", since = "1.6.0")]
3820 impl<$($life),*> PartialEq<$lhs> for $rhs {
3821 #[inline]
3822 fn eq(&self, other: &$lhs) -> bool {
3823 <Path as PartialEq>::eq(self, other)
3824 }
3825 }
3826
3827 #[stable(feature = "cmp_path", since = "1.8.0")]
3828 impl<$($life),*> PartialOrd<$rhs> for $lhs {
3829 #[inline]
3830 fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
3831 <Path as PartialOrd>::partial_cmp(self, other)
3832 }
3833 }
3834
3835 #[stable(feature = "cmp_path", since = "1.8.0")]
3836 impl<$($life),*> PartialOrd<$lhs> for $rhs {
3837 #[inline]
3838 fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
3839 <Path as PartialOrd>::partial_cmp(self, other)
3840 }
3841 }
3842 };
3843}
3844
3845impl_cmp!(<> PathBuf, Path);
3846impl_cmp!(<'a> PathBuf, &'a Path);
3847impl_cmp!(<'a> Cow<'a, Path>, Path);
3848impl_cmp!(<'a, 'b> Cow<'a, Path>, &'b Path);
3849impl_cmp!(<'a> Cow<'a, Path>, PathBuf);
3850
3851macro_rules! impl_cmp_os_str {
3852 (<$($life:lifetime),*> $lhs:ty, $rhs: ty) => {
3853 #[stable(feature = "cmp_path", since = "1.8.0")]
3854 impl<$($life),*> PartialEq<$rhs> for $lhs {
3855 #[inline]
3856 fn eq(&self, other: &$rhs) -> bool {
3857 <Path as PartialEq>::eq(self, other.as_ref())
3858 }
3859 }
3860
3861 #[stable(feature = "cmp_path", since = "1.8.0")]
3862 impl<$($life),*> PartialEq<$lhs> for $rhs {
3863 #[inline]
3864 fn eq(&self, other: &$lhs) -> bool {
3865 <Path as PartialEq>::eq(self.as_ref(), other)
3866 }
3867 }
3868
3869 #[stable(feature = "cmp_path", since = "1.8.0")]
3870 impl<$($life),*> PartialOrd<$rhs> for $lhs {
3871 #[inline]
3872 fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
3873 <Path as PartialOrd>::partial_cmp(self, other.as_ref())
3874 }
3875 }
3876
3877 #[stable(feature = "cmp_path", since = "1.8.0")]
3878 impl<$($life),*> PartialOrd<$lhs> for $rhs {
3879 #[inline]
3880 fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
3881 <Path as PartialOrd>::partial_cmp(self.as_ref(), other)
3882 }
3883 }
3884 };
3885}
3886
3887impl_cmp_os_str!(<> PathBuf, OsStr);
3888impl_cmp_os_str!(<'a> PathBuf, &'a OsStr);
3889impl_cmp_os_str!(<'a> PathBuf, Cow<'a, OsStr>);
3890impl_cmp_os_str!(<> PathBuf, OsString);
3891impl_cmp_os_str!(<> Path, OsStr);
3892impl_cmp_os_str!(<'a> Path, &'a OsStr);
3893impl_cmp_os_str!(<'a> Path, Cow<'a, OsStr>);
3894impl_cmp_os_str!(<> Path, OsString);
3895impl_cmp_os_str!(<'a> &'a Path, OsStr);
3896impl_cmp_os_str!(<'a, 'b> &'a Path, Cow<'b, OsStr>);
3897impl_cmp_os_str!(<'a> &'a Path, OsString);
3898impl_cmp_os_str!(<'a> Cow<'a, Path>, OsStr);
3899impl_cmp_os_str!(<'a, 'b> Cow<'a, Path>, &'b OsStr);
3900impl_cmp_os_str!(<'a> Cow<'a, Path>, OsString);
3901
3902#[stable(since = "1.7.0", feature = "strip_prefix")]
3903impl fmt::Display for StripPrefixError {
3904 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3905 "prefix not found".fmt(f)
3906 }
3907}
3908
3909#[stable(since = "1.7.0", feature = "strip_prefix")]
3910impl Error for StripPrefixError {}
3911
3912#[unstable(feature = "normalize_lexically", issue = "134694")]
3913impl fmt::Display for NormalizeError {
3914 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3915 f.write_str("parent reference `..` points outside of base directory")
3916 }
3917}
3918#[unstable(feature = "normalize_lexically", issue = "134694")]
3919impl Error for NormalizeError {}
3920
3921/// Makes the path absolute without accessing the filesystem.
3922///
3923/// If the path is relative, the current directory is used as the base directory.
3924/// All intermediate components will be resolved according to platform-specific
3925/// rules, but unlike [`canonicalize`][crate::fs::canonicalize], this does not
3926/// resolve symlinks and may succeed even if the path does not exist.
3927///
3928/// If the `path` is empty or getting the
3929/// [current directory][crate::env::current_dir] fails, then an error will be
3930/// returned.
3931///
3932/// # Platform-specific behavior
3933///
3934/// On POSIX platforms, the path is resolved using [POSIX semantics][posix-semantics],
3935/// except that it stops short of resolving symlinks. This means it will keep `..`
3936/// components and trailing separators.
3937///
3938/// On Windows, for verbatim paths, this will simply return the path as given. For other
3939/// paths, this is currently equivalent to calling
3940/// [`GetFullPathNameW`][windows-path].
3941///
3942/// On Cygwin, this is currently equivalent to calling [`cygwin_conv_path`][cygwin-path]
3943/// with mode `CCP_WIN_A_TO_POSIX`, and then being processed like other POSIX platforms.
3944/// If a Windows path is given, it will be converted to an absolute POSIX path without
3945/// keeping `..`.
3946///
3947/// Note that these [may change in the future][changes].
3948///
3949/// # Errors
3950///
3951/// This function may return an error in the following situations:
3952///
3953/// * If `path` is syntactically invalid; in particular, if it is empty.
3954/// * If getting the [current directory][crate::env::current_dir] fails.
3955///
3956/// # Examples
3957///
3958/// ## POSIX paths
3959///
3960/// ```
3961/// # #[cfg(unix)]
3962/// fn main() -> std::io::Result<()> {
3963/// use std::path::{self, Path};
3964///
3965/// // Relative to absolute
3966/// let absolute = path::absolute("foo/./bar")?;
3967/// assert!(absolute.ends_with("foo/bar"));
3968///
3969/// // Absolute to absolute
3970/// let absolute = path::absolute("/foo//test/.././bar.rs")?;
3971/// assert_eq!(absolute, Path::new("/foo/test/../bar.rs"));
3972/// Ok(())
3973/// }
3974/// # #[cfg(not(unix))]
3975/// # fn main() {}
3976/// ```
3977///
3978/// ## Windows paths
3979///
3980/// ```
3981/// # #[cfg(windows)]
3982/// fn main() -> std::io::Result<()> {
3983/// use std::path::{self, Path};
3984///
3985/// // Relative to absolute
3986/// let absolute = path::absolute("foo/./bar")?;
3987/// assert!(absolute.ends_with(r"foo\bar"));
3988///
3989/// // Absolute to absolute
3990/// let absolute = path::absolute(r"C:\foo//test\..\./bar.rs")?;
3991///
3992/// assert_eq!(absolute, Path::new(r"C:\foo\bar.rs"));
3993/// Ok(())
3994/// }
3995/// # #[cfg(not(windows))]
3996/// # fn main() {}
3997/// ```
3998///
3999/// Note that this [may change in the future][changes].
4000///
4001/// [changes]: io#platform-specific-behavior
4002/// [posix-semantics]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
4003/// [windows-path]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfullpathnamew
4004/// [cygwin-path]: https://cygwin.com/cygwin-api/func-cygwin-conv-path.html
4005#[stable(feature = "absolute_path", since = "1.79.0")]
4006pub fn absolute<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
4007 let path = path.as_ref();
4008 if path.as_os_str().is_empty() {
4009 Err(io::const_error!(io::ErrorKind::InvalidInput, "cannot make an empty path absolute"))
4010 } else {
4011 sys::path::absolute(path)
4012 }
4013}