1#![stable(feature = "rust1", since = "1.0.0")]
82#![deny(unsafe_op_in_unsafe_fn)]
83
84use core::clone::CloneToUninit;
85
86use crate::alloc::Allocator;
87use crate::borrow::{Borrow, Cow};
88use crate::collections::TryReserveError;
89use crate::error::Error;
90use crate::ffi::{OsStr, OsString, os_str};
91use crate::hash::{Hash, Hasher};
92use crate::iter::FusedIterator;
93use crate::ops::{self, Deref};
94use crate::rc::Rc;
95use crate::str::FromStr;
96use crate::sync::Arc;
97use crate::sys::path::{HAS_PREFIXES, is_sep_byte, is_verbatim_sep, parse_prefix};
98use crate::{cmp, fmt, fs, io, sys};
99
100#[derive(#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::marker::Copy for Prefix<'a> { }Copy, #[automatically_derived]#[doc(hidden)]#[stable(feature = "rust1", since =
"1.0.0")]
unsafe impl<'a> ::core::clone::TrivialClone for Prefix<'a> { }
#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::clone::Clone for Prefix<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<&'a OsStr>;
let _: ::core::clone::AssertParamIsClone<&'a OsStr>;
let _: ::core::clone::AssertParamIsClone<&'a OsStr>;
let _: ::core::clone::AssertParamIsClone<u8>;
let _: ::core::clone::AssertParamIsClone<&'a OsStr>;
let _: ::core::clone::AssertParamIsClone<&'a OsStr>;
let _: ::core::clone::AssertParamIsClone<&'a OsStr>;
*self
}
}Clone, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::fmt::Debug for Prefix<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::Verbatim(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"Verbatim", &__self_0),
Self::VerbatimUNC(__self_0, __self_1) =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"VerbatimUNC", __self_0, &__self_1),
Self::VerbatimDisk(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"VerbatimDisk", &__self_0),
Self::DeviceNS(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"DeviceNS", &__self_0),
Self::UNC(__self_0, __self_1) =>
::core::fmt::Formatter::debug_struct_field2_finish(f, "UNC",
__self_0, &__self_1),
Self::Disk(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f, "Disk",
&__self_0),
}
}
}Debug, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::hash::Hash for Prefix<'a> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
state);
match self {
Self::Verbatim(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
Self::VerbatimUNC(__self_0, __self_1) => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
Self::VerbatimDisk(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
Self::DeviceNS(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
Self::UNC(__self_0, __self_1) => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
Self::Disk(__self_0) => ::core::hash::Hash::hash(__self_0, state),
}
}
}Hash, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::cmp::PartialOrd for Prefix<'a> {
#[inline]
fn partial_cmp(&self, other: &Self)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}PartialOrd, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::cmp::Ord for Prefix<'a> {
#[inline]
fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
match (self, other) {
(Self::Verbatim(__self_0), Self::Verbatim(__arg1_0)) =>
::core::cmp::Ord::cmp(__self_0, __arg1_0),
(Self::VerbatimUNC(__self_0, __self_1),
Self::VerbatimUNC(__arg1_0, __arg1_1)) =>
match ::core::cmp::Ord::cmp(__self_0, __arg1_0) {
::core::cmp::Ordering::Equal =>
::core::cmp::Ord::cmp(__self_1, __arg1_1),
cmp => cmp,
},
(Self::VerbatimDisk(__self_0), Self::VerbatimDisk(__arg1_0)) =>
::core::cmp::Ord::cmp(__self_0, __arg1_0),
(Self::DeviceNS(__self_0), Self::DeviceNS(__arg1_0)) =>
::core::cmp::Ord::cmp(__self_0, __arg1_0),
(Self::UNC(__self_0, __self_1), Self::UNC(__arg1_0, __arg1_1)) =>
match ::core::cmp::Ord::cmp(__self_0, __arg1_0) {
::core::cmp::Ordering::Equal =>
::core::cmp::Ord::cmp(__self_1, __arg1_1),
cmp => cmp,
},
(Self::Disk(__self_0), Self::Disk(__arg1_0)) =>
::core::cmp::Ord::cmp(__self_0, __arg1_0),
_ =>
::core::cmp::Ord::cmp(&::core::intrinsics::discriminant_value(self),
&::core::intrinsics::discriminant_value(other)),
}
}
}Ord, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::marker::StructuralPartialEq for Prefix<'a> { }
#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::cmp::PartialEq for Prefix<'a> {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other) &&
match (self, other) {
(Self::Verbatim(__self_0), Self::Verbatim(__arg1_0)) =>
__self_0 == __arg1_0,
(Self::VerbatimUNC(__self_0, __self_1),
Self::VerbatimUNC(__arg1_0, __arg1_1)) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(Self::VerbatimDisk(__self_0), Self::VerbatimDisk(__arg1_0))
=> __self_0 == __arg1_0,
(Self::DeviceNS(__self_0), Self::DeviceNS(__arg1_0)) =>
__self_0 == __arg1_0,
(Self::UNC(__self_0, __self_1), Self::UNC(__arg1_0, __arg1_1))
=> __self_0 == __arg1_0 && __self_1 == __arg1_1,
(Self::Disk(__self_0), Self::Disk(__arg1_0)) =>
__self_0 == __arg1_0,
_ => unsafe { ::core::intrinsics::unreachable() }
}
}
}PartialEq, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::cmp::Eq for Prefix<'a> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<&'a OsStr>;
let _: ::core::cmp::AssertParamIsEq<&'a OsStr>;
let _: ::core::cmp::AssertParamIsEq<&'a OsStr>;
let _: ::core::cmp::AssertParamIsEq<u8>;
let _: ::core::cmp::AssertParamIsEq<&'a OsStr>;
let _: ::core::cmp::AssertParamIsEq<&'a OsStr>;
let _: ::core::cmp::AssertParamIsEq<&'a OsStr>;
}
}Eq)]
151#[stable(feature = "rust1", since = "1.0.0")]
152pub enum Prefix<'a> {
153 #[stable(feature = "rust1", since = "1.0.0")]
158 Verbatim(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
159
160 #[stable(feature = "rust1", since = "1.0.0")]
166 VerbatimUNC(
167 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
168 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
169 ),
170
171 #[stable(feature = "rust1", since = "1.0.0")]
176 VerbatimDisk(#[stable(feature = "rust1", since = "1.0.0")] u8),
177
178 #[stable(feature = "rust1", since = "1.0.0")]
183 DeviceNS(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
184
185 #[stable(feature = "rust1", since = "1.0.0")]
190 UNC(
191 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
192 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
193 ),
194
195 #[stable(feature = "rust1", since = "1.0.0")]
197 Disk(#[stable(feature = "rust1", since = "1.0.0")] u8),
198}
199
200impl<'a> Prefix<'a> {
201 #[inline]
202 fn len(&self) -> usize {
203 use self::Prefix::*;
204 fn os_str_len(s: &OsStr) -> usize {
205 s.as_encoded_bytes().len()
206 }
207 match *self {
208 Verbatim(x) => 4 + os_str_len(x),
209 VerbatimUNC(x, y) => {
210 8 + os_str_len(x) + if os_str_len(y) > 0 { 1 + os_str_len(y) } else { 0 }
211 }
212 VerbatimDisk(_) => 6,
213 UNC(x, y) => 2 + os_str_len(x) + if os_str_len(y) > 0 { 1 + os_str_len(y) } else { 0 },
214 DeviceNS(x) => 4 + os_str_len(x),
215 Disk(_) => 2,
216 }
217 }
218
219 #[inline]
235 #[must_use]
236 #[stable(feature = "rust1", since = "1.0.0")]
237 pub fn is_verbatim(&self) -> bool {
238 use self::Prefix::*;
239 #[allow(non_exhaustive_omitted_patterns)] match *self {
Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..) => true,
_ => false,
}matches!(*self, Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..))
240 }
241
242 #[inline]
243 fn is_drive(&self) -> bool {
244 #[allow(non_exhaustive_omitted_patterns)] match *self {
Prefix::Disk(_) => true,
_ => false,
}matches!(*self, Prefix::Disk(_))
245 }
246
247 #[inline]
248 fn has_implicit_root(&self) -> bool {
249 !self.is_drive()
250 }
251}
252
253#[must_use]
269#[stable(feature = "rust1", since = "1.0.0")]
270#[rustc_const_unstable(feature = "const_path_separators", issue = "153106")]
271pub const fn is_separator(c: char) -> bool {
272 c.is_ascii() && is_sep_byte(c as u8)
273}
274
275#[unstable(feature = "const_path_separators", issue = "153106")]
279pub const SEPARATORS: &[char] = crate::sys::path::SEPARATORS;
280
281#[unstable(feature = "const_path_separators", issue = "153106")]
285pub const SEPARATORS_STR: &[&str] = crate::sys::path::SEPARATORS_STR;
286
287#[stable(feature = "rust1", since = "1.0.0")]
290#[cfg_attr(not(test), rustc_diagnostic_item = "path_main_separator")]
291pub const MAIN_SEPARATOR: char = SEPARATORS[0];
292
293#[stable(feature = "main_separator_str", since = "1.68.0")]
296pub const MAIN_SEPARATOR_STR: &str = SEPARATORS_STR[0];
297
298fn iter_after<'a, 'b, I, J>(mut iter: I, mut prefix: J) -> Option<I>
306where
307 I: Iterator<Item = Component<'a>> + Clone,
308 J: Iterator<Item = Component<'b>>,
309{
310 loop {
311 let mut iter_next = iter.clone();
312 match (iter_next.next(), prefix.next()) {
313 (Some(ref x), Some(ref y)) if x == y => (),
314 (Some(_), Some(_)) => return None,
315 (Some(_), None) => return Some(iter),
316 (None, None) => return Some(iter),
317 (None, Some(_)) => return None,
318 }
319 iter = iter_next;
320 }
321}
322
323fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
329 let path = if let Some(p) = prefix { &s[p.len()..] } else { s };
330 !path.is_empty() && is_sep_byte(path[0])
331}
332
333fn rsplit_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
335 if file.as_encoded_bytes() == b".." {
336 return (Some(file), None);
337 }
338
339 let mut iter = file.as_encoded_bytes().rsplitn(2, |b| *b == b'.');
344 let after = iter.next();
345 let before = iter.next();
346 if before == Some(b"") {
347 (Some(file), None)
348 } else {
349 unsafe {
350 (
351 before.map(|s| OsStr::from_encoded_bytes_unchecked(s)),
352 after.map(|s| OsStr::from_encoded_bytes_unchecked(s)),
353 )
354 }
355 }
356}
357
358fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
359 let slice = file.as_encoded_bytes();
360 if slice == b".." {
361 return (file, None);
362 }
363
364 let i = match slice[1..].iter().position(|b| *b == b'.') {
369 Some(i) => i + 1,
370 None => return (file, None),
371 };
372 let before = &slice[..i];
373 let after = &slice[i + 1..];
374 unsafe {
375 (
376 OsStr::from_encoded_bytes_unchecked(before),
377 Some(OsStr::from_encoded_bytes_unchecked(after)),
378 )
379 }
380}
381
382fn validate_extension(extension: &OsStr) {
384 for &b in extension.as_encoded_bytes() {
385 if is_sep_byte(b) {
386 {
::core::panicking::panic_fmt(format_args!("extension cannot contain path separators: {0:?}",
extension));
};panic!("extension cannot contain path separators: {extension:?}");
387 }
388 }
389}
390
391#[derive(#[automatically_derived]impl ::core::marker::Copy for State { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for State {
}
#[automatically_derived]impl ::core::clone::Clone for State {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::marker::StructuralPartialEq for State { }
#[automatically_derived]impl ::core::cmp::PartialEq for State {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other)
}
}PartialEq, #[automatically_derived]impl ::core::cmp::PartialOrd for State {
#[inline]
fn partial_cmp(&self, other: &Self)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&::core::intrinsics::discriminant_value(self),
&::core::intrinsics::discriminant_value(other))
}
}PartialOrd, #[automatically_derived]impl ::core::fmt::Debug for State {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
State::Prefix => "Prefix",
State::StartDir => "StartDir",
State::Body => "Body",
State::Done => "Done",
})
}
}Debug)]
402enum State {
403 Prefix = 0, StartDir = 1, Body = 2, Done = 3,
407}
408
409#[stable(feature = "rust1", since = "1.0.0")]
443#[derive(#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::marker::Copy for PrefixComponent<'a> { }Copy, #[automatically_derived]#[doc(hidden)]#[stable(feature = "rust1", since =
"1.0.0")]
unsafe impl<'a> ::core::clone::TrivialClone for PrefixComponent<'a> { }
#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::clone::Clone for PrefixComponent<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<&'a OsStr>;
let _: ::core::clone::AssertParamIsClone<Prefix<'a>>;
*self
}
}Clone, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::cmp::Eq for PrefixComponent<'a> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<&'a OsStr>;
let _: ::core::cmp::AssertParamIsEq<Prefix<'a>>;
}
}Eq, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::fmt::Debug for PrefixComponent<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"PrefixComponent raw parsed", &self.raw, &&self.parsed)
}
}Debug)]
444pub struct PrefixComponent<'a> {
445 raw: &'a OsStr,
447
448 parsed: Prefix<'a>,
450}
451
452impl<'a> PrefixComponent<'a> {
453 #[stable(feature = "rust1", since = "1.0.0")]
458 #[must_use]
459 #[inline]
460 pub fn kind(&self) -> Prefix<'a> {
461 self.parsed
462 }
463
464 #[stable(feature = "rust1", since = "1.0.0")]
466 #[must_use]
467 #[inline]
468 pub fn as_os_str(&self) -> &'a OsStr {
469 self.raw
470 }
471}
472
473#[stable(feature = "rust1", since = "1.0.0")]
474impl<'a> PartialEq for PrefixComponent<'a> {
475 #[inline]
476 fn eq(&self, other: &PrefixComponent<'a>) -> bool {
477 self.parsed == other.parsed
478 }
479}
480
481#[stable(feature = "rust1", since = "1.0.0")]
482impl<'a> PartialOrd for PrefixComponent<'a> {
483 #[inline]
484 fn partial_cmp(&self, other: &PrefixComponent<'a>) -> Option<cmp::Ordering> {
485 PartialOrd::partial_cmp(&self.parsed, &other.parsed)
486 }
487}
488
489#[stable(feature = "rust1", since = "1.0.0")]
490impl Ord for PrefixComponent<'_> {
491 #[inline]
492 fn cmp(&self, other: &Self) -> cmp::Ordering {
493 Ord::cmp(&self.parsed, &other.parsed)
494 }
495}
496
497#[stable(feature = "rust1", since = "1.0.0")]
498impl Hash for PrefixComponent<'_> {
499 fn hash<H: Hasher>(&self, h: &mut H) {
500 self.parsed.hash(h);
501 }
502}
503
504#[derive(#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::marker::Copy for Component<'a> { }Copy, #[automatically_derived]#[doc(hidden)]#[stable(feature = "rust1", since =
"1.0.0")]
unsafe impl<'a> ::core::clone::TrivialClone for Component<'a> { }
#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::clone::Clone for Component<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<PrefixComponent<'a>>;
let _: ::core::clone::AssertParamIsClone<&'a OsStr>;
*self
}
}Clone, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::marker::StructuralPartialEq for Component<'a> { }
#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::cmp::PartialEq for Component<'a> {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other) &&
match (self, other) {
(Self::Prefix(__self_0), Self::Prefix(__arg1_0)) =>
__self_0 == __arg1_0,
(Self::Normal(__self_0), Self::Normal(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::cmp::Eq for Component<'a> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<PrefixComponent<'a>>;
let _: ::core::cmp::AssertParamIsEq<&'a OsStr>;
}
}Eq, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::cmp::PartialOrd for Component<'a> {
#[inline]
fn partial_cmp(&self, other: &Self)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}PartialOrd, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::cmp::Ord for Component<'a> {
#[inline]
fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
match (self, other) {
(Self::Prefix(__self_0), Self::Prefix(__arg1_0)) =>
::core::cmp::Ord::cmp(__self_0, __arg1_0),
(Self::Normal(__self_0), Self::Normal(__arg1_0)) =>
::core::cmp::Ord::cmp(__self_0, __arg1_0),
_ =>
::core::cmp::Ord::cmp(&::core::intrinsics::discriminant_value(self),
&::core::intrinsics::discriminant_value(other)),
}
}
}Ord, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::hash::Hash for Component<'a> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
state);
match self {
Self::Prefix(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
Self::Normal(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
_ => {}
}
}
}Hash, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::fmt::Debug for Component<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::Prefix(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"Prefix", &__self_0),
Self::RootDir => ::core::fmt::Formatter::write_str(f, "RootDir"),
Self::CurDir => ::core::fmt::Formatter::write_str(f, "CurDir"),
Self::ParentDir =>
::core::fmt::Formatter::write_str(f, "ParentDir"),
Self::Normal(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"Normal", &__self_0),
}
}
}Debug)]
527#[stable(feature = "rust1", since = "1.0.0")]
528pub enum Component<'a> {
529 #[stable(feature = "rust1", since = "1.0.0")]
536 Prefix(#[stable(feature = "rust1", since = "1.0.0")] PrefixComponent<'a>),
537
538 #[stable(feature = "rust1", since = "1.0.0")]
542 RootDir,
543
544 #[stable(feature = "rust1", since = "1.0.0")]
546 CurDir,
547
548 #[stable(feature = "rust1", since = "1.0.0")]
550 ParentDir,
551
552 #[stable(feature = "rust1", since = "1.0.0")]
557 Normal(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
558}
559
560impl<'a> Component<'a> {
561 #[must_use = "`self` will be dropped if the result is not used"]
573 #[stable(feature = "rust1", since = "1.0.0")]
574 pub fn as_os_str(self) -> &'a OsStr {
575 match self {
576 Component::Prefix(p) => p.as_os_str(),
577 Component::RootDir => OsStr::new(MAIN_SEPARATOR_STR),
578 Component::CurDir => OsStr::new("."),
579 Component::ParentDir => OsStr::new(".."),
580 Component::Normal(path) => path,
581 }
582 }
583}
584
585#[stable(feature = "rust1", since = "1.0.0")]
586impl AsRef<OsStr> for Component<'_> {
587 #[inline]
588 fn as_ref(&self) -> &OsStr {
589 self.as_os_str()
590 }
591}
592
593#[stable(feature = "path_component_asref", since = "1.25.0")]
594impl AsRef<Path> for Component<'_> {
595 #[inline]
596 fn as_ref(&self) -> &Path {
597 self.as_os_str().as_ref()
598 }
599}
600
601#[derive(#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::clone::Clone for Components<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
Components {
path: ::core::clone::Clone::clone(&self.path),
prefix: ::core::clone::Clone::clone(&self.prefix),
has_physical_root: ::core::clone::Clone::clone(&self.has_physical_root),
front: ::core::clone::Clone::clone(&self.front),
back: ::core::clone::Clone::clone(&self.back),
}
}
}Clone)]
620#[must_use = "iterators are lazy and do nothing unless consumed"]
621#[stable(feature = "rust1", since = "1.0.0")]
622pub struct Components<'a> {
623 path: &'a [u8],
625
626 prefix: Option<Prefix<'a>>,
628
629 has_physical_root: bool,
633
634 front: State,
637 back: State,
638}
639
640#[derive(#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::clone::Clone for Iter<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
Iter { inner: ::core::clone::Clone::clone(&self.inner) }
}
}Clone)]
647#[must_use = "iterators are lazy and do nothing unless consumed"]
648#[stable(feature = "rust1", since = "1.0.0")]
649pub struct Iter<'a> {
650 inner: Components<'a>,
651}
652
653#[stable(feature = "path_components_debug", since = "1.13.0")]
654impl fmt::Debug for Components<'_> {
655 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
656 struct DebugHelper<'a>(&'a Path);
657
658 impl fmt::Debug for DebugHelper<'_> {
659 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
660 f.debug_list().entries(self.0.components()).finish()
661 }
662 }
663
664 f.debug_tuple("Components").field(&DebugHelper(self.as_path())).finish()
665 }
666}
667
668impl<'a> Components<'a> {
669 #[inline]
671 fn prefix_len(&self) -> usize {
672 if !HAS_PREFIXES {
673 return 0;
674 }
675 self.prefix.as_ref().map(Prefix::len).unwrap_or(0)
676 }
677
678 #[inline]
679 fn prefix_verbatim(&self) -> bool {
680 if !HAS_PREFIXES {
681 return false;
682 }
683 self.prefix.as_ref().map(Prefix::is_verbatim).unwrap_or(false)
684 }
685
686 #[inline]
688 fn prefix_remaining(&self) -> usize {
689 if !HAS_PREFIXES {
690 return 0;
691 }
692 if self.front == State::Prefix { self.prefix_len() } else { 0 }
693 }
694
695 #[inline]
697 fn len_before_body(&self) -> usize {
698 let root = if self.front <= State::StartDir && self.has_physical_root { 1 } else { 0 };
699 let cur_dir = if self.front <= State::StartDir && self.include_cur_dir() { 1 } else { 0 };
700 self.prefix_remaining() + root + cur_dir
701 }
702
703 #[inline]
705 fn finished(&self) -> bool {
706 self.front == State::Done || self.back == State::Done || self.front > self.back
707 }
708
709 #[inline]
710 fn is_sep_byte(&self, b: u8) -> bool {
711 if self.prefix_verbatim() { is_verbatim_sep(b) } else { is_sep_byte(b) }
712 }
713
714 #[must_use]
728 #[stable(feature = "rust1", since = "1.0.0")]
729 pub fn as_path(&self) -> &'a Path {
730 let mut comps = self.clone();
731 if comps.front == State::Body {
732 comps.trim_left();
733 }
734 if comps.back == State::Body {
735 comps.trim_right();
736 }
737 unsafe { Path::from_u8_slice(comps.path) }
738 }
739
740 fn has_root(&self) -> bool {
742 if self.has_physical_root {
743 return true;
744 }
745 if HAS_PREFIXES && let Some(p) = self.prefix {
746 if p.has_implicit_root() {
747 return true;
748 }
749 }
750 false
751 }
752
753 fn include_cur_dir(&self) -> bool {
755 if self.has_root() {
756 return false;
757 }
758 let slice = &self.path[self.prefix_remaining()..];
759 match slice {
760 [b'.'] => true,
761 [b'.', b, ..] => self.is_sep_byte(*b),
762 _ => false,
763 }
764 }
765
766 unsafe fn parse_single_component<'b>(&self, comp: &'b [u8]) -> Option<Component<'b>> {
769 match comp {
770 b"." if HAS_PREFIXES && self.prefix_verbatim() => Some(Component::CurDir),
771 b"." => None, b".." => Some(Component::ParentDir),
775 b"" => None,
776 _ => Some(Component::Normal(unsafe { OsStr::from_encoded_bytes_unchecked(comp) })),
777 }
778 }
779
780 fn parse_next_component(&self) -> (usize, Option<Component<'a>>) {
783 if true {
if !(self.front == State::Body) {
::core::panicking::panic("assertion failed: self.front == State::Body")
};
};debug_assert!(self.front == State::Body);
784 let (extra, comp) = match self.path.iter().position(|b| self.is_sep_byte(*b)) {
785 None => (0, self.path),
786 Some(i) => (1, &self.path[..i]),
787 };
788 (comp.len() + extra, unsafe { self.parse_single_component(comp) })
790 }
791
792 fn parse_next_component_back(&self) -> (usize, Option<Component<'a>>) {
795 if true {
if !(self.back == State::Body) {
::core::panicking::panic("assertion failed: self.back == State::Body")
};
};debug_assert!(self.back == State::Body);
796 let start = self.len_before_body();
797 let (extra, comp) = match self.path[start..].iter().rposition(|b| self.is_sep_byte(*b)) {
798 None => (0, &self.path[start..]),
799 Some(i) => (1, &self.path[start + i + 1..]),
800 };
801 (comp.len() + extra, unsafe { self.parse_single_component(comp) })
803 }
804
805 fn trim_left(&mut self) {
807 while !self.path.is_empty() {
808 let (size, comp) = self.parse_next_component();
809 if comp.is_some() {
810 return;
811 } else {
812 self.path = &self.path[size..];
813 }
814 }
815 }
816
817 fn trim_right(&mut self) {
819 while self.path.len() > self.len_before_body() {
820 let (size, comp) = self.parse_next_component_back();
821 if comp.is_some() {
822 return;
823 } else {
824 self.path = &self.path[..self.path.len() - size];
825 }
826 }
827 }
828}
829
830#[stable(feature = "rust1", since = "1.0.0")]
831impl AsRef<Path> for Components<'_> {
832 #[inline]
833 fn as_ref(&self) -> &Path {
834 self.as_path()
835 }
836}
837
838#[stable(feature = "rust1", since = "1.0.0")]
839impl AsRef<OsStr> for Components<'_> {
840 #[inline]
841 fn as_ref(&self) -> &OsStr {
842 self.as_path().as_os_str()
843 }
844}
845
846#[stable(feature = "path_iter_debug", since = "1.13.0")]
847impl fmt::Debug for Iter<'_> {
848 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
849 struct DebugHelper<'a>(&'a Path);
850
851 impl fmt::Debug for DebugHelper<'_> {
852 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
853 f.debug_list().entries(self.0.iter()).finish()
854 }
855 }
856
857 f.debug_tuple("Iter").field(&DebugHelper(self.as_path())).finish()
858 }
859}
860
861impl<'a> Iter<'a> {
862 #[stable(feature = "rust1", since = "1.0.0")]
876 #[must_use]
877 #[inline]
878 pub fn as_path(&self) -> &'a Path {
879 self.inner.as_path()
880 }
881}
882
883#[stable(feature = "rust1", since = "1.0.0")]
884impl AsRef<Path> for Iter<'_> {
885 #[inline]
886 fn as_ref(&self) -> &Path {
887 self.as_path()
888 }
889}
890
891#[stable(feature = "rust1", since = "1.0.0")]
892impl AsRef<OsStr> for Iter<'_> {
893 #[inline]
894 fn as_ref(&self) -> &OsStr {
895 self.as_path().as_os_str()
896 }
897}
898
899#[stable(feature = "rust1", since = "1.0.0")]
900impl<'a> Iterator for Iter<'a> {
901 type Item = &'a OsStr;
902
903 #[inline]
904 fn next(&mut self) -> Option<&'a OsStr> {
905 self.inner.next().map(Component::as_os_str)
906 }
907}
908
909#[stable(feature = "rust1", since = "1.0.0")]
910impl<'a> DoubleEndedIterator for Iter<'a> {
911 #[inline]
912 fn next_back(&mut self) -> Option<&'a OsStr> {
913 self.inner.next_back().map(Component::as_os_str)
914 }
915}
916
917#[stable(feature = "fused", since = "1.26.0")]
918impl FusedIterator for Iter<'_> {}
919
920#[stable(feature = "rust1", since = "1.0.0")]
921impl<'a> Iterator for Components<'a> {
922 type Item = Component<'a>;
923
924 fn next(&mut self) -> Option<Component<'a>> {
925 while !self.finished() {
926 match self.front {
927 State::Body if !self.path.is_empty() => {
929 let (size, comp) = self.parse_next_component();
930 self.path = &self.path[size..];
931 if comp.is_some() {
932 return comp;
933 }
934 }
935 State::Body => {
936 self.front = State::Done;
937 }
938 State::StartDir => {
939 self.front = State::Body;
940 if self.has_physical_root {
941 if true {
if !!self.path.is_empty() {
::core::panicking::panic("assertion failed: !self.path.is_empty()")
};
};debug_assert!(!self.path.is_empty());
942 self.path = &self.path[1..];
943 return Some(Component::RootDir);
944 } else if HAS_PREFIXES && let Some(p) = self.prefix {
945 if p.has_implicit_root() && !p.is_verbatim() {
946 return Some(Component::RootDir);
947 }
948 } else if self.include_cur_dir() {
949 if true {
if !!self.path.is_empty() {
::core::panicking::panic("assertion failed: !self.path.is_empty()")
};
};debug_assert!(!self.path.is_empty());
950 self.path = &self.path[1..];
951 return Some(Component::CurDir);
952 }
953 }
954 _ if const { !HAS_PREFIXES } => ::core::panicking::unreachable_default_msg()unreachable!(),
955 State::Prefix if self.prefix_len() == 0 => {
956 self.front = State::StartDir;
957 }
958 State::Prefix => {
959 self.front = State::StartDir;
960 if true {
if !(self.prefix_len() <= self.path.len()) {
::core::panicking::panic("assertion failed: self.prefix_len() <= self.path.len()")
};
};debug_assert!(self.prefix_len() <= self.path.len());
961 let raw = &self.path[..self.prefix_len()];
962 self.path = &self.path[self.prefix_len()..];
963 return Some(Component::Prefix(PrefixComponent {
964 raw: unsafe { OsStr::from_encoded_bytes_unchecked(raw) },
965 parsed: self.prefix.unwrap(),
966 }));
967 }
968 State::Done => ::core::panicking::unreachable_default_msg()unreachable!(),
969 }
970 }
971 None
972 }
973}
974
975#[stable(feature = "rust1", since = "1.0.0")]
976impl<'a> DoubleEndedIterator for Components<'a> {
977 fn next_back(&mut self) -> Option<Component<'a>> {
978 while !self.finished() {
979 match self.back {
980 State::Body if self.path.len() > self.len_before_body() => {
981 let (size, comp) = self.parse_next_component_back();
982 self.path = &self.path[..self.path.len() - size];
983 if comp.is_some() {
984 return comp;
985 }
986 }
987 State::Body => {
988 self.back = State::StartDir;
989 }
990 State::StartDir => {
991 self.back = if HAS_PREFIXES { State::Prefix } else { State::Done };
992 if self.has_physical_root {
993 self.path = &self.path[..self.path.len() - 1];
994 return Some(Component::RootDir);
995 } else if HAS_PREFIXES && let Some(p) = self.prefix {
996 if p.has_implicit_root() && !p.is_verbatim() {
997 return Some(Component::RootDir);
998 }
999 } else if self.include_cur_dir() {
1000 self.path = &self.path[..self.path.len() - 1];
1001 return Some(Component::CurDir);
1002 }
1003 }
1004 _ if !HAS_PREFIXES => ::core::panicking::unreachable_default_msg()unreachable!(),
1005 State::Prefix if self.prefix_len() > 0 => {
1006 self.back = State::Done;
1007 return Some(Component::Prefix(PrefixComponent {
1008 raw: unsafe { OsStr::from_encoded_bytes_unchecked(self.path) },
1009 parsed: self.prefix.unwrap(),
1010 }));
1011 }
1012 State::Prefix => {
1013 self.back = State::Done;
1014 return None;
1015 }
1016 State::Done => ::core::panicking::unreachable_default_msg()unreachable!(),
1017 }
1018 }
1019 None
1020 }
1021}
1022
1023#[stable(feature = "fused", since = "1.26.0")]
1024impl FusedIterator for Components<'_> {}
1025
1026#[stable(feature = "rust1", since = "1.0.0")]
1027impl<'a> PartialEq for Components<'a> {
1028 #[inline]
1029 fn eq(&self, other: &Components<'a>) -> bool {
1030 let Components { path: _, front: _, back: _, has_physical_root: _, prefix: _ } = self;
1031
1032 if self.path.len() == other.path.len()
1036 && self.front == other.front
1037 && self.back == State::Body
1038 && other.back == State::Body
1039 && self.prefix_verbatim() == other.prefix_verbatim()
1040 {
1041 if self.path == other.path {
1044 return true;
1045 }
1046 }
1047
1048 Iterator::eq(self.clone().rev(), other.clone().rev())
1050 }
1051}
1052
1053#[stable(feature = "rust1", since = "1.0.0")]
1054impl Eq for Components<'_> {}
1055
1056#[stable(feature = "rust1", since = "1.0.0")]
1057impl<'a> PartialOrd for Components<'a> {
1058 #[inline]
1059 fn partial_cmp(&self, other: &Components<'a>) -> Option<cmp::Ordering> {
1060 Some(compare_components(self.clone(), other.clone()))
1061 }
1062}
1063
1064#[stable(feature = "rust1", since = "1.0.0")]
1065impl Ord for Components<'_> {
1066 #[inline]
1067 fn cmp(&self, other: &Self) -> cmp::Ordering {
1068 compare_components(self.clone(), other.clone())
1069 }
1070}
1071
1072fn compare_components(mut left: Components<'_>, mut right: Components<'_>) -> cmp::Ordering {
1073 if left.prefix.is_none() && right.prefix.is_none() && left.front == right.front {
1083 let first_difference = match left.path.iter().zip(right.path).position(|(&a, &b)| a != b) {
1085 None if left.path.len() == right.path.len() => return cmp::Ordering::Equal,
1086 None => left.path.len().min(right.path.len()),
1087 Some(diff) => diff,
1088 };
1089
1090 if let Some(previous_sep) =
1091 left.path[..first_difference].iter().rposition(|&b| left.is_sep_byte(b))
1092 {
1093 let mismatched_component_start = previous_sep + 1;
1094 left.path = &left.path[mismatched_component_start..];
1095 left.front = State::Body;
1096 right.path = &right.path[mismatched_component_start..];
1097 right.front = State::Body;
1098 }
1099 }
1100
1101 Iterator::cmp(left, right)
1102}
1103
1104#[derive(#[automatically_derived]#[stable(feature = "path_ancestors", since =
"1.28.0")]
impl<'a> ::core::marker::Copy for Ancestors<'a> { }Copy, #[automatically_derived]#[doc(hidden)]#[stable(feature = "path_ancestors",
since = "1.28.0")]
unsafe impl<'a> ::core::clone::TrivialClone for Ancestors<'a> { }
#[automatically_derived]#[stable(feature = "path_ancestors", since =
"1.28.0")]
impl<'a> ::core::clone::Clone for Ancestors<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<Option<&'a Path>>;
*self
}
}Clone, #[automatically_derived]#[stable(feature = "path_ancestors", since =
"1.28.0")]
impl<'a> ::core::fmt::Debug for Ancestors<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f,
"Ancestors next", &&self.next)
}
}Debug)]
1123#[must_use = "iterators are lazy and do nothing unless consumed"]
1124#[stable(feature = "path_ancestors", since = "1.28.0")]
1125pub struct Ancestors<'a> {
1126 next: Option<&'a Path>,
1127}
1128
1129#[stable(feature = "path_ancestors", since = "1.28.0")]
1130impl<'a> Iterator for Ancestors<'a> {
1131 type Item = &'a Path;
1132
1133 #[inline]
1134 fn next(&mut self) -> Option<Self::Item> {
1135 let next = self.next;
1136 self.next = next.and_then(Path::parent);
1137 next
1138 }
1139}
1140
1141#[stable(feature = "path_ancestors", since = "1.28.0")]
1142impl FusedIterator for Ancestors<'_> {}
1143
1144#[cfg_attr(not(test), rustc_diagnostic_item = "PathBuf")]
1214#[stable(feature = "rust1", since = "1.0.0")]
1215pub struct PathBuf {
1216 inner: OsString,
1217}
1218
1219impl PathBuf {
1220 #[stable(feature = "rust1", since = "1.0.0")]
1230 #[must_use]
1231 #[inline]
1232 #[rustc_const_stable(feature = "const_pathbuf_osstring_new", since = "1.91.0")]
1233 pub const fn new() -> PathBuf {
1234 PathBuf { inner: OsString::new() }
1235 }
1236
1237 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1256 #[must_use]
1257 #[inline]
1258 pub fn with_capacity(capacity: usize) -> PathBuf {
1259 PathBuf { inner: OsString::with_capacity(capacity) }
1260 }
1261
1262 #[cfg_attr(not(test), rustc_diagnostic_item = "pathbuf_as_path")]
1273 #[stable(feature = "rust1", since = "1.0.0")]
1274 #[must_use]
1275 #[inline]
1276 pub fn as_path(&self) -> &Path {
1277 self
1278 }
1279
1280 #[stable(feature = "os_string_pathbuf_leak", since = "1.89.0")]
1294 #[inline]
1295 pub fn leak<'a>(self) -> &'a mut Path {
1296 Path::from_inner_mut(self.inner.leak())
1297 }
1298
1299 #[stable(feature = "rust1", since = "1.0.0")]
1337 #[rustc_confusables("append", "put")]
1338 pub fn push<P: AsRef<Path>>(&mut self, path: P) {
1339 self._push(path.as_ref())
1340 }
1341
1342 fn _push(&mut self, path: &Path) {
1343 let buf = self.inner.as_encoded_bytes();
1345 let mut need_sep = buf.last().map(|c| !is_sep_byte(*c)).unwrap_or(false);
1346
1347 let self_comps = self.components();
1348 let self_prefix_len = self_comps.prefix_len();
1349
1350 if self_prefix_len > 0
1352 && self_prefix_len == self_comps.path.len()
1353 && self_comps.prefix.unwrap().is_drive()
1354 {
1355 need_sep = false
1356 }
1357
1358 let path_comps = path.components();
1359
1360 let need_clear = if falsecfg!(any(target_os = "windows", target_os = "cygwin")) {
1361 path_comps.prefix.is_some()
1364 } else {
1365 path.is_absolute() || path_comps.prefix.is_some()
1367 };
1368
1369 if need_clear {
1371 self.inner.clear();
1372
1373 } else if self_comps.prefix_verbatim() && !path.inner.is_empty() {
1375 let mut buf: Vec<_> = self_comps.collect();
1376 for c in path_comps {
1377 match c {
1378 Component::RootDir => {
1379 buf.truncate(1);
1380 buf.push(c);
1381 }
1382 Component::CurDir => (),
1383 Component::ParentDir => {
1384 if let Some(Component::Normal(_)) = buf.last() {
1385 buf.pop();
1386 }
1387 }
1388 _ => buf.push(c),
1389 }
1390 }
1391
1392 let mut res = OsString::new();
1393 let mut need_sep = false;
1394
1395 for c in buf {
1396 if need_sep && c != Component::RootDir {
1397 res.push(MAIN_SEPARATOR_STR);
1398 }
1399 res.push(c.as_os_str());
1400 need_sep = c != Component::RootDir;
1401 }
1402
1403 self.inner = res;
1404 return;
1405
1406 } else if path_comps.has_root() {
1408 self.inner.truncate(self_prefix_len);
1409
1410 } else if need_sep {
1412 self.inner.push(MAIN_SEPARATOR_STR);
1413 }
1414
1415 self.inner.push(path);
1416 }
1417
1418 #[stable(feature = "rust1", since = "1.0.0")]
1438 pub fn pop(&mut self) -> bool {
1439 match self.parent().map(|p| p.as_u8_slice().len()) {
1440 Some(len) => {
1441 self.inner.truncate(len);
1442 true
1443 }
1444 None => false,
1445 }
1446 }
1447
1448 #[unstable(feature = "path_trailing_sep", issue = "142503")]
1475 pub fn set_trailing_sep(&mut self, trailing_sep: bool) {
1476 if trailing_sep { self.push_trailing_sep() } else { self.pop_trailing_sep() }
1477 }
1478
1479 #[unstable(feature = "path_trailing_sep", issue = "142503")]
1503 pub fn push_trailing_sep(&mut self) {
1504 if !self.has_trailing_sep() {
1505 self.push("");
1506 }
1507 }
1508
1509 #[unstable(feature = "path_trailing_sep", issue = "142503")]
1537 pub fn pop_trailing_sep(&mut self) {
1538 self.inner.truncate(self.trim_trailing_sep().as_os_str().len());
1539 }
1540
1541 #[stable(feature = "rust1", since = "1.0.0")]
1581 pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
1582 self._set_file_name(file_name.as_ref())
1583 }
1584
1585 fn _set_file_name(&mut self, file_name: &OsStr) {
1586 if self.file_name().is_some() {
1587 let popped = self.pop();
1588 if true {
if !popped { ::core::panicking::panic("assertion failed: popped") };
};debug_assert!(popped);
1589 }
1590 self.push(file_name);
1591 }
1592
1593 #[stable(feature = "rust1", since = "1.0.0")]
1650 pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
1651 self._set_extension(extension.as_ref())
1652 }
1653
1654 fn _set_extension(&mut self, extension: &OsStr) -> bool {
1655 validate_extension(extension);
1656
1657 let file_stem = match self.file_stem() {
1658 None => return false,
1659 Some(f) => f.as_encoded_bytes(),
1660 };
1661
1662 let end_file_stem = file_stem[file_stem.len()..].as_ptr().addr();
1664 let start = self.inner.as_encoded_bytes().as_ptr().addr();
1665 self.inner.truncate(end_file_stem.wrapping_sub(start));
1666
1667 let new = extension.as_encoded_bytes();
1669 if !new.is_empty() {
1670 self.inner.reserve_exact(new.len() + 1);
1671 self.inner.push(".");
1672 unsafe { self.inner.extend_from_slice_unchecked(new) };
1675 }
1676
1677 true
1678 }
1679
1680 #[stable(feature = "path_add_extension", since = "1.91.0")]
1724 pub fn add_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
1725 self._add_extension(extension.as_ref())
1726 }
1727
1728 fn _add_extension(&mut self, extension: &OsStr) -> bool {
1729 validate_extension(extension);
1730
1731 let file_name = match self.file_name() {
1732 None => return false,
1733 Some(f) => f.as_encoded_bytes(),
1734 };
1735
1736 let new = extension.as_encoded_bytes();
1737 if !new.is_empty() {
1738 let end_file_name = file_name[file_name.len()..].as_ptr().addr();
1741 let start = self.inner.as_encoded_bytes().as_ptr().addr();
1742 self.inner.truncate(end_file_name.wrapping_sub(start));
1743
1744 self.inner.reserve_exact(new.len() + 1);
1746 self.inner.push(".");
1747 unsafe { self.inner.extend_from_slice_unchecked(new) };
1750 }
1751
1752 true
1753 }
1754
1755 #[stable(feature = "path_as_mut_os_str", since = "1.70.0")]
1772 #[must_use]
1773 #[inline]
1774 pub fn as_mut_os_string(&mut self) -> &mut OsString {
1775 &mut self.inner
1776 }
1777
1778 #[stable(feature = "rust1", since = "1.0.0")]
1789 #[must_use = "`self` will be dropped if the result is not used"]
1790 #[inline]
1791 pub fn into_os_string(self) -> OsString {
1792 self.inner
1793 }
1794
1795 #[stable(feature = "pathbuf_into_string", since = "1.98.0")]
1809 pub fn into_string(self) -> Result<String, PathBuf> {
1810 self.into_os_string().into_string().map_err(PathBuf::from)
1811 }
1812
1813 #[stable(feature = "into_boxed_path", since = "1.20.0")]
1815 #[must_use = "`self` will be dropped if the result is not used"]
1816 #[inline]
1817 pub fn into_boxed_path(self) -> Box<Path> {
1818 let rw = Box::into_raw(self.inner.into_boxed_os_str()) as *mut Path;
1819 unsafe { Box::from_raw(rw) }
1820 }
1821
1822 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1826 #[must_use]
1827 #[inline]
1828 pub fn capacity(&self) -> usize {
1829 self.inner.capacity()
1830 }
1831
1832 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1836 #[inline]
1837 pub fn clear(&mut self) {
1838 self.inner.clear()
1839 }
1840
1841 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1845 #[inline]
1846 pub fn reserve(&mut self, additional: usize) {
1847 self.inner.reserve(additional)
1848 }
1849
1850 #[stable(feature = "try_reserve_2", since = "1.63.0")]
1854 #[inline]
1855 pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
1856 self.inner.try_reserve(additional)
1857 }
1858
1859 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1863 #[inline]
1864 pub fn reserve_exact(&mut self, additional: usize) {
1865 self.inner.reserve_exact(additional)
1866 }
1867
1868 #[stable(feature = "try_reserve_2", since = "1.63.0")]
1872 #[inline]
1873 pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
1874 self.inner.try_reserve_exact(additional)
1875 }
1876
1877 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1881 #[inline]
1882 pub fn shrink_to_fit(&mut self) {
1883 self.inner.shrink_to_fit()
1884 }
1885
1886 #[stable(feature = "shrink_to", since = "1.56.0")]
1890 #[inline]
1891 pub fn shrink_to(&mut self, min_capacity: usize) {
1892 self.inner.shrink_to(min_capacity)
1893 }
1894}
1895
1896#[stable(feature = "rust1", since = "1.0.0")]
1897impl Clone for PathBuf {
1898 #[inline]
1899 fn clone(&self) -> Self {
1900 PathBuf { inner: self.inner.clone() }
1901 }
1902
1903 #[inline]
1908 fn clone_from(&mut self, source: &Self) {
1909 self.inner.clone_from(&source.inner)
1910 }
1911}
1912
1913#[stable(feature = "box_from_path", since = "1.17.0")]
1914impl From<&Path> for Box<Path> {
1915 fn from(path: &Path) -> Box<Path> {
1919 Box::clone_from_ref(path)
1920 }
1921}
1922
1923#[stable(feature = "box_from_mut_slice", since = "1.84.0")]
1924impl From<&mut Path> for Box<Path> {
1925 fn from(path: &mut Path) -> Box<Path> {
1929 Self::from(&*path)
1930 }
1931}
1932
1933#[stable(feature = "box_from_cow", since = "1.45.0")]
1934impl From<Cow<'_, Path>> for Box<Path> {
1935 #[inline]
1939 fn from(cow: Cow<'_, Path>) -> Box<Path> {
1940 match cow {
1941 Cow::Borrowed(path) => Box::from(path),
1942 Cow::Owned(path) => Box::from(path),
1943 }
1944 }
1945}
1946
1947#[stable(feature = "path_buf_from_box", since = "1.18.0")]
1948impl From<Box<Path>> for PathBuf {
1949 #[inline]
1953 fn from(boxed: Box<Path>) -> PathBuf {
1954 boxed.into_path_buf()
1955 }
1956}
1957
1958#[stable(feature = "box_from_path_buf", since = "1.20.0")]
1959impl From<PathBuf> for Box<Path> {
1960 #[inline]
1965 fn from(p: PathBuf) -> Box<Path> {
1966 p.into_boxed_path()
1967 }
1968}
1969
1970#[stable(feature = "more_box_slice_clone", since = "1.29.0")]
1971impl<A: Allocator + Clone> Clone for Box<Path, A> {
1972 #[inline]
1973 fn clone(&self) -> Self {
1974 Box::clone_from_ref_in(&**self, Self::allocator(self).clone())
1975 }
1976}
1977
1978#[stable(feature = "rust1", since = "1.0.0")]
1979impl<T: ?Sized + AsRef<OsStr>> From<&T> for PathBuf {
1980 #[inline]
1984 fn from(s: &T) -> PathBuf {
1985 PathBuf::from(s.as_ref().to_os_string())
1986 }
1987}
1988
1989#[stable(feature = "rust1", since = "1.0.0")]
1990impl From<OsString> for PathBuf {
1991 #[inline]
1995 fn from(s: OsString) -> PathBuf {
1996 PathBuf { inner: s }
1997 }
1998}
1999
2000#[stable(feature = "from_path_buf_for_os_string", since = "1.14.0")]
2001impl From<PathBuf> for OsString {
2002 #[inline]
2006 fn from(path_buf: PathBuf) -> OsString {
2007 path_buf.inner
2008 }
2009}
2010
2011#[stable(feature = "rust1", since = "1.0.0")]
2012impl From<String> for PathBuf {
2013 #[inline]
2017 fn from(s: String) -> PathBuf {
2018 PathBuf::from(OsString::from(s))
2019 }
2020}
2021
2022#[stable(feature = "path_from_str", since = "1.32.0")]
2023impl FromStr for PathBuf {
2024 type Err = !;
2025
2026 #[inline]
2027 fn from_str(s: &str) -> Result<Self, !> {
2028 Ok(PathBuf::from(s))
2029 }
2030}
2031
2032#[stable(feature = "rust1", since = "1.0.0")]
2033impl<P: AsRef<Path>> FromIterator<P> for PathBuf {
2034 fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> PathBuf {
2048 let mut buf = PathBuf::new();
2049 buf.extend(iter);
2050 buf
2051 }
2052}
2053
2054#[stable(feature = "rust1", since = "1.0.0")]
2055impl<P: AsRef<Path>> Extend<P> for PathBuf {
2056 fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) {
2071 iter.into_iter().for_each(move |p| self.push(p.as_ref()));
2072 }
2073
2074 #[inline]
2075 fn extend_one(&mut self, p: P) {
2076 self.push(p.as_ref());
2077 }
2078}
2079
2080#[stable(feature = "rust1", since = "1.0.0")]
2081impl fmt::Debug for PathBuf {
2082 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2083 fmt::Debug::fmt(&**self, formatter)
2084 }
2085}
2086
2087#[stable(feature = "rust1", since = "1.0.0")]
2088impl ops::Deref for PathBuf {
2089 type Target = Path;
2090 #[inline]
2091 fn deref(&self) -> &Path {
2092 Path::new(&self.inner)
2093 }
2094}
2095
2096#[stable(feature = "path_buf_deref_mut", since = "1.68.0")]
2097impl ops::DerefMut for PathBuf {
2098 #[inline]
2099 fn deref_mut(&mut self) -> &mut Path {
2100 Path::from_inner_mut(&mut self.inner)
2101 }
2102}
2103
2104#[stable(feature = "rust1", since = "1.0.0")]
2105impl Borrow<Path> for PathBuf {
2106 #[inline]
2107 fn borrow(&self) -> &Path {
2108 self.deref()
2109 }
2110}
2111
2112#[stable(feature = "default_for_pathbuf", since = "1.17.0")]
2113impl Default for PathBuf {
2114 #[inline]
2115 fn default() -> Self {
2116 PathBuf::new()
2117 }
2118}
2119
2120#[stable(feature = "cow_from_path", since = "1.6.0")]
2121impl<'a> From<&'a Path> for Cow<'a, Path> {
2122 #[inline]
2127 fn from(s: &'a Path) -> Cow<'a, Path> {
2128 Cow::Borrowed(s)
2129 }
2130}
2131
2132#[stable(feature = "cow_from_path", since = "1.6.0")]
2133impl<'a> From<PathBuf> for Cow<'a, Path> {
2134 #[inline]
2139 fn from(s: PathBuf) -> Cow<'a, Path> {
2140 Cow::Owned(s)
2141 }
2142}
2143
2144#[stable(feature = "cow_from_pathbuf_ref", since = "1.28.0")]
2145impl<'a> From<&'a PathBuf> for Cow<'a, Path> {
2146 #[inline]
2151 fn from(p: &'a PathBuf) -> Cow<'a, Path> {
2152 Cow::Borrowed(p.as_path())
2153 }
2154}
2155
2156#[stable(feature = "pathbuf_from_cow_path", since = "1.28.0")]
2157impl<'a> From<Cow<'a, Path>> for PathBuf {
2158 #[inline]
2162 fn from(p: Cow<'a, Path>) -> Self {
2163 p.into_owned()
2164 }
2165}
2166
2167#[stable(feature = "shared_from_slice2", since = "1.24.0")]
2168impl From<PathBuf> for Arc<Path> {
2169 #[inline]
2172 fn from(s: PathBuf) -> Arc<Path> {
2173 let arc: Arc<OsStr> = Arc::from(s.into_os_string());
2174 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Path) }
2175 }
2176}
2177
2178#[stable(feature = "shared_from_slice2", since = "1.24.0")]
2179impl From<&Path> for Arc<Path> {
2180 #[inline]
2182 fn from(s: &Path) -> Arc<Path> {
2183 let arc: Arc<OsStr> = Arc::from(s.as_os_str());
2184 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Path) }
2185 }
2186}
2187
2188#[stable(feature = "shared_from_mut_slice", since = "1.84.0")]
2189impl From<&mut Path> for Arc<Path> {
2190 #[inline]
2192 fn from(s: &mut Path) -> Arc<Path> {
2193 Arc::from(&*s)
2194 }
2195}
2196
2197#[stable(feature = "shared_from_slice2", since = "1.24.0")]
2198impl From<PathBuf> for Rc<Path> {
2199 #[inline]
2202 fn from(s: PathBuf) -> Rc<Path> {
2203 let rc: Rc<OsStr> = Rc::from(s.into_os_string());
2204 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Path) }
2205 }
2206}
2207
2208#[stable(feature = "shared_from_slice2", since = "1.24.0")]
2209impl From<&Path> for Rc<Path> {
2210 #[inline]
2212 fn from(s: &Path) -> Rc<Path> {
2213 let rc: Rc<OsStr> = Rc::from(s.as_os_str());
2214 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Path) }
2215 }
2216}
2217
2218#[stable(feature = "shared_from_mut_slice", since = "1.84.0")]
2219impl From<&mut Path> for Rc<Path> {
2220 #[inline]
2222 fn from(s: &mut Path) -> Rc<Path> {
2223 Rc::from(&*s)
2224 }
2225}
2226
2227#[stable(feature = "rust1", since = "1.0.0")]
2228impl ToOwned for Path {
2229 type Owned = PathBuf;
2230 #[inline]
2231 fn to_owned(&self) -> PathBuf {
2232 self.to_path_buf()
2233 }
2234 #[inline]
2235 fn clone_into(&self, target: &mut PathBuf) {
2236 self.inner.clone_into(&mut target.inner);
2237 }
2238}
2239
2240#[stable(feature = "rust1", since = "1.0.0")]
2241impl PartialEq for PathBuf {
2242 #[inline]
2243 fn eq(&self, other: &PathBuf) -> bool {
2244 self.components() == other.components()
2245 }
2246}
2247
2248#[stable(feature = "eq_str_for_path", since = "1.91.0")]
2249impl cmp::PartialEq<str> for PathBuf {
2250 #[inline]
2251 fn eq(&self, other: &str) -> bool {
2252 self.as_path() == other
2253 }
2254}
2255
2256#[stable(feature = "eq_str_for_path", since = "1.91.0")]
2257impl cmp::PartialEq<PathBuf> for str {
2258 #[inline]
2259 fn eq(&self, other: &PathBuf) -> bool {
2260 self == other.as_path()
2261 }
2262}
2263
2264#[stable(feature = "eq_str_for_path", since = "1.91.0")]
2265impl cmp::PartialEq<String> for PathBuf {
2266 #[inline]
2267 fn eq(&self, other: &String) -> bool {
2268 self.as_path() == other.as_str()
2269 }
2270}
2271
2272#[stable(feature = "eq_str_for_path", since = "1.91.0")]
2273impl cmp::PartialEq<PathBuf> for String {
2274 #[inline]
2275 fn eq(&self, other: &PathBuf) -> bool {
2276 self.as_str() == other.as_path()
2277 }
2278}
2279
2280#[stable(feature = "rust1", since = "1.0.0")]
2281impl Hash for PathBuf {
2282 fn hash<H: Hasher>(&self, h: &mut H) {
2283 self.as_path().hash(h)
2284 }
2285}
2286
2287#[stable(feature = "rust1", since = "1.0.0")]
2288impl Eq for PathBuf {}
2289
2290#[stable(feature = "rust1", since = "1.0.0")]
2291impl PartialOrd for PathBuf {
2292 #[inline]
2293 fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
2294 Some(compare_components(self.components(), other.components()))
2295 }
2296}
2297
2298#[stable(feature = "rust1", since = "1.0.0")]
2299impl Ord for PathBuf {
2300 #[inline]
2301 fn cmp(&self, other: &PathBuf) -> cmp::Ordering {
2302 compare_components(self.components(), other.components())
2303 }
2304}
2305
2306#[stable(feature = "rust1", since = "1.0.0")]
2307impl AsRef<OsStr> for PathBuf {
2308 #[inline]
2309 fn as_ref(&self) -> &OsStr {
2310 &self.inner[..]
2311 }
2312}
2313
2314#[cfg_attr(not(test), rustc_diagnostic_item = "Path")]
2347#[stable(feature = "rust1", since = "1.0.0")]
2348#[repr(transparent)]
2352pub struct Path {
2353 inner: OsStr,
2354}
2355
2356#[derive(#[automatically_derived]#[stable(since = "1.7.0", feature = "strip_prefix")]
impl ::core::fmt::Debug for StripPrefixError {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f,
"StripPrefixError", &&self.0)
}
}Debug, #[automatically_derived]#[stable(since = "1.7.0", feature = "strip_prefix")]
impl ::core::clone::Clone for StripPrefixError {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
StripPrefixError(::core::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]#[stable(since = "1.7.0", feature = "strip_prefix")]
impl ::core::marker::StructuralPartialEq for StripPrefixError { }
#[automatically_derived]#[stable(since = "1.7.0", feature = "strip_prefix")]
impl ::core::cmp::PartialEq for StripPrefixError {
#[inline]
fn eq(&self, other: &Self) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]#[stable(since = "1.7.0", feature = "strip_prefix")]
impl ::core::cmp::Eq for StripPrefixError {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<()>;
}
}Eq)]
2363#[stable(since = "1.7.0", feature = "strip_prefix")]
2364pub struct StripPrefixError(());
2365
2366#[unstable(feature = "normalize_lexically", issue = "134694")]
2369#[derive(#[automatically_derived]#[unstable(feature = "normalize_lexically", issue =
"134694")]
impl ::core::fmt::Debug for NormalizeError {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "NormalizeError")
}
}Debug, #[automatically_derived]#[unstable(feature = "normalize_lexically", issue =
"134694")]
impl ::core::marker::StructuralPartialEq for NormalizeError { }
#[automatically_derived]#[unstable(feature = "normalize_lexically", issue =
"134694")]
impl ::core::cmp::PartialEq for NormalizeError {
#[inline]
fn eq(&self, other: &Self) -> bool { true }
}PartialEq)]
2370#[non_exhaustive]
2371pub struct NormalizeError;
2372
2373impl Path {
2374 pub(crate) unsafe fn from_u8_slice(s: &[u8]) -> &Path {
2377 unsafe { Path::new(OsStr::from_encoded_bytes_unchecked(s)) }
2378 }
2379 pub(crate) fn as_u8_slice(&self) -> &[u8] {
2381 self.inner.as_encoded_bytes()
2382 }
2383
2384 #[stable(feature = "rust1", since = "1.0.0")]
2407 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2408 pub const fn new<S: [const] AsRef<OsStr> + ?Sized>(s: &S) -> &Path {
2409 unsafe { &*(s.as_ref() as *const OsStr as *const Path) }
2410 }
2411
2412 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2413 const fn from_inner_mut(inner: &mut OsStr) -> &mut Path {
2414 unsafe { &mut *(inner as *mut OsStr as *mut Path) }
2417 }
2418
2419 #[stable(feature = "rust1", since = "1.0.0")]
2430 #[must_use]
2431 #[inline]
2432 pub fn as_os_str(&self) -> &OsStr {
2433 &self.inner
2434 }
2435
2436 #[stable(feature = "path_as_mut_os_str", since = "1.70.0")]
2451 #[must_use]
2452 #[inline]
2453 pub fn as_mut_os_str(&mut self) -> &mut OsStr {
2454 &mut self.inner
2455 }
2456
2457 #[stable(feature = "rust1", since = "1.0.0")]
2474 #[must_use = "this returns the result of the operation, \
2475 without modifying the original"]
2476 #[inline]
2477 pub fn to_str(&self) -> Option<&str> {
2478 self.inner.to_str()
2479 }
2480
2481 #[stable(feature = "rust1", since = "1.0.0")]
2502 #[must_use = "this returns the result of the operation, \
2503 without modifying the original"]
2504 #[inline]
2505 pub fn to_string_lossy(&self) -> Cow<'_, str> {
2506 self.inner.to_string_lossy()
2507 }
2508
2509 #[rustc_conversion_suggestion]
2520 #[must_use = "this returns the result of the operation, \
2521 without modifying the original"]
2522 #[stable(feature = "rust1", since = "1.0.0")]
2523 #[cfg_attr(not(test), rustc_diagnostic_item = "path_to_pathbuf")]
2524 pub fn to_path_buf(&self) -> PathBuf {
2525 PathBuf::from(self.inner.to_os_string())
2526 }
2527
2528 #[stable(feature = "rust1", since = "1.0.0")]
2547 #[must_use]
2548 #[allow(deprecated)]
2549 pub fn is_absolute(&self) -> bool {
2550 sys::path::is_absolute(self)
2551 }
2552
2553 #[stable(feature = "rust1", since = "1.0.0")]
2567 #[must_use]
2568 #[inline]
2569 pub fn is_relative(&self) -> bool {
2570 !self.is_absolute()
2571 }
2572
2573 #[cfg(not(target_os = "uefi"))]
2574 pub(crate) fn prefix(&self) -> Option<Prefix<'_>> {
2575 self.components().prefix
2576 }
2577
2578 #[stable(feature = "rust1", since = "1.0.0")]
2595 #[must_use]
2596 #[inline]
2597 pub fn has_root(&self) -> bool {
2598 self.components().has_root()
2599 }
2600
2601 #[stable(feature = "rust1", since = "1.0.0")]
2630 #[doc(alias = "dirname")]
2631 #[must_use]
2632 pub fn parent(&self) -> Option<&Path> {
2633 let mut comps = self.components();
2634 let comp = comps.next_back();
2635 comp.and_then(|p| match p {
2636 Component::Normal(_) | Component::CurDir | Component::ParentDir => {
2637 Some(comps.as_path())
2638 }
2639 _ => None,
2640 })
2641 }
2642
2643 #[stable(feature = "path_ancestors", since = "1.28.0")]
2671 #[inline]
2672 pub fn ancestors(&self) -> Ancestors<'_> {
2673 Ancestors { next: Some(self) }
2674 }
2675
2676 #[stable(feature = "rust1", since = "1.0.0")]
2697 #[doc(alias = "basename")]
2698 #[must_use]
2699 pub fn file_name(&self) -> Option<&OsStr> {
2700 self.components().next_back().and_then(|p| match p {
2701 Component::Normal(p) => Some(p),
2702 _ => None,
2703 })
2704 }
2705
2706 #[stable(since = "1.7.0", feature = "path_strip_prefix")]
2736 pub fn strip_prefix<P>(&self, base: P) -> Result<&Path, StripPrefixError>
2737 where
2738 P: AsRef<Path>,
2739 {
2740 self._strip_prefix(base.as_ref())
2741 }
2742
2743 #[must_use = "this returns the remaining path as a new path, without modifying the original"]
2769 #[stable(feature = "trim_prefix_suffix", since = "1.100.0")]
2770 pub fn trim_prefix<P>(&self, base: P) -> &Path
2771 where
2772 P: AsRef<Path>,
2773 {
2774 self._strip_prefix(base.as_ref()).unwrap_or(self)
2775 }
2776
2777 fn _strip_prefix(&self, base: &Path) -> Result<&Path, StripPrefixError> {
2778 iter_after(self.components(), base.components())
2779 .map(|c| c.as_path())
2780 .ok_or(StripPrefixError(()))
2781 }
2782
2783 #[stable(feature = "rust1", since = "1.0.0")]
2806 #[must_use]
2807 pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool {
2808 self._starts_with(base.as_ref())
2809 }
2810
2811 fn _starts_with(&self, base: &Path) -> bool {
2812 iter_after(self.components(), base.components()).is_some()
2813 }
2814
2815 #[stable(feature = "rust1", since = "1.0.0")]
2834 #[must_use]
2835 pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {
2836 self._ends_with(child.as_ref())
2837 }
2838
2839 fn _ends_with(&self, child: &Path) -> bool {
2840 iter_after(self.components().rev(), child.components().rev()).is_some()
2841 }
2842
2843 #[stable(feature = "path_is_empty", since = "1.98.0")]
2864 pub fn is_empty(&self) -> bool {
2865 self.as_os_str().is_empty()
2866 }
2867
2868 #[stable(feature = "rust1", since = "1.0.0")]
2895 #[must_use]
2896 pub fn file_stem(&self) -> Option<&OsStr> {
2897 self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.or(after))
2898 }
2899
2900 #[stable(feature = "path_file_prefix", since = "1.91.0")]
2930 #[must_use]
2931 pub fn file_prefix(&self) -> Option<&OsStr> {
2932 self.file_name().map(split_file_at_dot).map(|(before, _after)| before)
2933 }
2934
2935 #[stable(feature = "rust1", since = "1.0.0")]
2955 #[must_use]
2956 pub fn extension(&self) -> Option<&OsStr> {
2957 self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.and(after))
2958 }
2959
2960 #[unstable(feature = "path_trailing_sep", issue = "142503")]
2979 #[must_use]
2980 #[inline]
2981 pub fn has_trailing_sep(&self) -> bool {
2982 let comps = self.components();
2983 self.as_os_str().as_encoded_bytes().last().copied().is_some_and(|b| comps.is_sep_byte(b))
2984 }
2985
2986 #[unstable(feature = "path_trailing_sep", issue = "142503")]
3004 #[must_use]
3005 #[inline]
3006 pub fn with_trailing_sep(&self) -> Cow<'_, Path> {
3007 if self.has_trailing_sep() { Cow::Borrowed(self) } else { Cow::Owned(self.join("")) }
3008 }
3009
3010 #[unstable(feature = "path_trailing_sep", issue = "142503")]
3031 #[must_use]
3032 #[inline]
3033 pub fn trim_trailing_sep(&self) -> &Path {
3034 let comps = self.components();
3035 if self.has_trailing_sep() && (!self.has_root() || self.parent().is_some()) {
3036 let mut bytes = self.inner.as_encoded_bytes();
3037 while let Some((last, init)) = bytes.split_last()
3038 && comps.is_sep_byte(*last)
3039 {
3040 bytes = init;
3041 }
3042
3043 Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(bytes) })
3045 } else {
3046 self
3047 }
3048 }
3049
3050 #[stable(feature = "rust1", since = "1.0.0")]
3074 #[must_use]
3075 pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
3076 self._join(path.as_ref())
3077 }
3078
3079 fn _join(&self, path: &Path) -> PathBuf {
3080 let mut buf = self.to_path_buf();
3081 buf.push(path);
3082 buf
3083 }
3084
3085 #[stable(feature = "rust1", since = "1.0.0")]
3102 #[must_use]
3103 pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf {
3104 self._with_file_name(file_name.as_ref())
3105 }
3106
3107 fn _with_file_name(&self, file_name: &OsStr) -> PathBuf {
3108 let mut buf = self.to_path_buf();
3109 buf.set_file_name(file_name);
3110 buf
3111 }
3112
3113 #[stable(feature = "rust1", since = "1.0.0")]
3146 pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
3147 self._with_extension(extension.as_ref())
3148 }
3149
3150 fn _with_extension(&self, extension: &OsStr) -> PathBuf {
3151 let self_len = self.as_os_str().len();
3152 let self_bytes = self.as_os_str().as_encoded_bytes();
3153
3154 let (new_capacity, slice_to_copy) = match self.extension() {
3155 None => {
3156 let capacity = self_len + extension.len() + 1;
3158 let whole_path = self_bytes;
3159 (capacity, whole_path)
3160 }
3161 Some(previous_extension) => {
3162 let capacity = self_len + extension.len() - previous_extension.len();
3163 let path_till_dot = &self_bytes[..self_len - previous_extension.len()];
3164 (capacity, path_till_dot)
3165 }
3166 };
3167
3168 let mut new_path = PathBuf::with_capacity(new_capacity);
3169 unsafe { new_path.inner.extend_from_slice_unchecked(slice_to_copy) };
3171 new_path.set_extension(extension);
3172 new_path
3173 }
3174
3175 #[stable(feature = "path_add_extension", since = "1.91.0")]
3202 pub fn with_added_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
3203 let mut new_path = self.to_path_buf();
3204 new_path.add_extension(extension);
3205 new_path
3206 }
3207
3208 #[stable(feature = "rust1", since = "1.0.0")]
3242 pub fn components(&self) -> Components<'_> {
3243 let prefix = parse_prefix(self.as_os_str());
3244 Components {
3245 path: self.as_u8_slice(),
3246 prefix,
3247 has_physical_root: has_physical_root(self.as_u8_slice(), prefix),
3248 front: const { if HAS_PREFIXES { State::Prefix } else { State::StartDir } },
3251 back: State::Body,
3252 }
3253 }
3254
3255 #[stable(feature = "rust1", since = "1.0.0")]
3276 #[inline]
3277 pub fn iter(&self) -> Iter<'_> {
3278 Iter { inner: self.components() }
3279 }
3280
3281 #[stable(feature = "rust1", since = "1.0.0")]
3299 #[must_use = "this does not display the path, \
3300 it returns an object that can be displayed"]
3301 #[inline]
3302 pub fn display(&self) -> Display<'_> {
3303 Display { inner: self.inner.display() }
3304 }
3305
3306 #[inline]
3312 #[unstable(feature = "str_as_str", issue = "130366")]
3313 pub const fn as_path(&self) -> &Path {
3314 self
3315 }
3316
3317 #[stable(feature = "path_ext", since = "1.5.0")]
3334 #[inline]
3335 pub fn metadata(&self) -> io::Result<fs::Metadata> {
3336 fs::metadata(self)
3337 }
3338
3339 #[stable(feature = "path_ext", since = "1.5.0")]
3353 #[inline]
3354 pub fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
3355 fs::symlink_metadata(self)
3356 }
3357
3358 #[stable(feature = "path_ext", since = "1.5.0")]
3380 #[inline]
3381 pub fn canonicalize(&self) -> io::Result<PathBuf> {
3382 fs::canonicalize(self)
3383 }
3384
3385 #[unstable(feature = "path_absolute_method", issue = "153328")]
3408 #[inline]
3409 pub fn absolute(&self) -> io::Result<PathBuf> {
3410 absolute(self)
3411 }
3412
3413 #[unstable(feature = "normalize_lexically", issue = "134694")]
3430 pub fn normalize_lexically(&self) -> Result<PathBuf, NormalizeError> {
3431 let mut lexical = PathBuf::new();
3432 let mut iter = self.components().peekable();
3433
3434 let root = match iter.peek() {
3438 Some(Component::ParentDir) => return Err(NormalizeError),
3439 Some(p @ Component::RootDir) | Some(p @ Component::CurDir) => {
3440 lexical.push(p);
3441 iter.next();
3442 lexical.as_os_str().len()
3443 }
3444 Some(Component::Prefix(prefix)) => {
3445 lexical.push(prefix.as_os_str());
3446 iter.next();
3447 if let Some(p @ Component::RootDir) = iter.peek() {
3448 lexical.push(p);
3449 iter.next();
3450 }
3451 lexical.as_os_str().len()
3452 }
3453 None => return Ok(PathBuf::new()),
3454 Some(Component::Normal(_)) => 0,
3455 };
3456
3457 for component in iter {
3458 match component {
3459 Component::RootDir => ::core::panicking::unreachable_default_msg()unreachable!(),
3460 Component::Prefix(_) => return Err(NormalizeError),
3461 Component::CurDir => continue,
3462 Component::ParentDir => {
3463 if lexical.as_os_str().len() == root {
3465 return Err(NormalizeError);
3466 } else {
3467 lexical.pop();
3468 }
3469 }
3470 Component::Normal(path) => lexical.push(path),
3471 }
3472 }
3473 Ok(lexical)
3474 }
3475
3476 #[stable(feature = "path_ext", since = "1.5.0")]
3489 #[inline]
3490 pub fn read_link(&self) -> io::Result<PathBuf> {
3491 fs::read_link(self)
3492 }
3493
3494 #[stable(feature = "path_ext", since = "1.5.0")]
3514 #[inline]
3515 pub fn read_dir(&self) -> io::Result<fs::ReadDir> {
3516 fs::read_dir(self)
3517 }
3518
3519 #[stable(feature = "path_ext", since = "1.5.0")]
3545 #[must_use]
3546 #[inline]
3547 pub fn exists(&self) -> bool {
3548 fs::metadata(self).is_ok()
3549 }
3550
3551 #[stable(feature = "path_try_exists", since = "1.63.0")]
3579 #[inline]
3580 pub fn try_exists(&self) -> io::Result<bool> {
3581 fs::exists(self)
3582 }
3583
3584 #[stable(feature = "path_ext", since = "1.5.0")]
3612 #[must_use]
3613 pub fn is_file(&self) -> bool {
3614 fs::metadata(self).map(|m| m.is_file()).unwrap_or(false)
3615 }
3616
3617 #[stable(feature = "path_ext", since = "1.5.0")]
3639 #[must_use]
3640 pub fn is_dir(&self) -> bool {
3641 fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
3642 }
3643
3644 #[must_use]
3672 #[stable(feature = "is_symlink", since = "1.58.0")]
3673 pub fn is_symlink(&self) -> bool {
3674 fs::symlink_metadata(self).map(|m| m.is_symlink()).unwrap_or(false)
3675 }
3676
3677 #[stable(feature = "into_boxed_path", since = "1.20.0")]
3680 #[must_use = "`self` will be dropped if the result is not used"]
3681 pub fn into_path_buf(self: Box<Self>) -> PathBuf {
3682 let rw = Box::into_raw(self) as *mut OsStr;
3683 let inner = unsafe { Box::from_raw(rw) };
3684 PathBuf { inner: OsString::from(inner) }
3685 }
3686}
3687
3688#[unstable(feature = "clone_to_uninit", issue = "126799")]
3689unsafe impl CloneToUninit for Path {
3690 #[inline]
3691 #[cfg_attr(debug_assertions, track_caller)]
3692 unsafe fn clone_to_uninit(&self, dst: *mut u8) {
3693 unsafe { self.inner.clone_to_uninit(dst) }
3695 }
3696}
3697
3698#[stable(feature = "rust1", since = "1.0.0")]
3699#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
3700const impl AsRef<OsStr> for Path {
3701 #[inline]
3702 fn as_ref(&self) -> &OsStr {
3703 &self.inner
3704 }
3705}
3706
3707#[stable(feature = "rust1", since = "1.0.0")]
3708impl fmt::Debug for Path {
3709 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
3710 fmt::Debug::fmt(&self.inner, formatter)
3711 }
3712}
3713
3714#[stable(feature = "rust1", since = "1.0.0")]
3735pub struct Display<'a> {
3736 inner: os_str::Display<'a>,
3737}
3738
3739#[stable(feature = "rust1", since = "1.0.0")]
3740impl fmt::Debug for Display<'_> {
3741 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3742 fmt::Debug::fmt(&self.inner, f)
3743 }
3744}
3745
3746#[stable(feature = "rust1", since = "1.0.0")]
3747impl fmt::Display for Display<'_> {
3748 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3749 fmt::Display::fmt(&self.inner, f)
3750 }
3751}
3752
3753#[stable(feature = "rust1", since = "1.0.0")]
3754impl PartialEq for Path {
3755 #[inline]
3756 fn eq(&self, other: &Path) -> bool {
3757 self.components() == other.components()
3758 }
3759}
3760
3761#[stable(feature = "eq_str_for_path", since = "1.91.0")]
3762impl cmp::PartialEq<str> for Path {
3763 #[inline]
3764 fn eq(&self, other: &str) -> bool {
3765 let other: &OsStr = other.as_ref();
3766 self == other
3767 }
3768}
3769
3770#[stable(feature = "eq_str_for_path", since = "1.91.0")]
3771impl cmp::PartialEq<Path> for str {
3772 #[inline]
3773 fn eq(&self, other: &Path) -> bool {
3774 other == self
3775 }
3776}
3777
3778#[stable(feature = "eq_str_for_path", since = "1.91.0")]
3779impl cmp::PartialEq<String> for Path {
3780 #[inline]
3781 fn eq(&self, other: &String) -> bool {
3782 self == other.as_str()
3783 }
3784}
3785
3786#[stable(feature = "eq_str_for_path", since = "1.91.0")]
3787impl cmp::PartialEq<Path> for String {
3788 #[inline]
3789 fn eq(&self, other: &Path) -> bool {
3790 self.as_str() == other
3791 }
3792}
3793
3794#[stable(feature = "rust1", since = "1.0.0")]
3795impl Hash for Path {
3796 fn hash<H: Hasher>(&self, h: &mut H) {
3797 let bytes = self.as_u8_slice();
3798 let (prefix_len, verbatim) = match parse_prefix(&self.inner) {
3799 Some(prefix) => {
3800 prefix.hash(h);
3801 (prefix.len(), prefix.is_verbatim())
3802 }
3803 None => (0, false),
3804 };
3805 let bytes = &bytes[prefix_len..];
3806
3807 let mut component_start = 0;
3808 let mut chunk_bits: usize = 0;
3812
3813 for i in 0..bytes.len() {
3814 let is_sep = if verbatim { is_verbatim_sep(bytes[i]) } else { is_sep_byte(bytes[i]) };
3815 if is_sep {
3816 if i > component_start {
3817 let to_hash = &bytes[component_start..i];
3818 chunk_bits = chunk_bits.wrapping_add(to_hash.len());
3819 chunk_bits = chunk_bits.rotate_right(2);
3820 h.write(to_hash);
3821 }
3822
3823 component_start = i + 1;
3826
3827 let tail = &bytes[component_start..];
3828
3829 if !verbatim {
3830 component_start += match tail {
3831 [b'.'] => 1,
3832 [b'.', sep, ..] if is_sep_byte(*sep) => 1,
3833 _ => 0,
3834 };
3835 }
3836 }
3837 }
3838
3839 if component_start < bytes.len() {
3840 let to_hash = &bytes[component_start..];
3841 chunk_bits = chunk_bits.wrapping_add(to_hash.len());
3842 chunk_bits = chunk_bits.rotate_right(2);
3843 h.write(to_hash);
3844 }
3845
3846 h.write_usize(chunk_bits);
3847 }
3848}
3849
3850#[stable(feature = "rust1", since = "1.0.0")]
3851impl Eq for Path {}
3852
3853#[stable(feature = "rust1", since = "1.0.0")]
3854impl PartialOrd for Path {
3855 #[inline]
3856 fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
3857 Some(compare_components(self.components(), other.components()))
3858 }
3859}
3860
3861#[stable(feature = "rust1", since = "1.0.0")]
3862impl Ord for Path {
3863 #[inline]
3864 fn cmp(&self, other: &Path) -> cmp::Ordering {
3865 compare_components(self.components(), other.components())
3866 }
3867}
3868
3869#[stable(feature = "rust1", since = "1.0.0")]
3870#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
3871const impl AsRef<Path> for Path {
3872 #[inline]
3873 fn as_ref(&self) -> &Path {
3874 self
3875 }
3876}
3877
3878#[stable(feature = "rust1", since = "1.0.0")]
3879#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
3880const impl AsRef<Path> for OsStr {
3881 #[inline]
3882 fn as_ref(&self) -> &Path {
3883 Path::new(self)
3884 }
3885}
3886
3887#[stable(feature = "cow_os_str_as_ref_path", since = "1.8.0")]
3888impl AsRef<Path> for Cow<'_, OsStr> {
3889 #[inline]
3890 fn as_ref(&self) -> &Path {
3891 Path::new(self)
3892 }
3893}
3894
3895#[stable(feature = "rust1", since = "1.0.0")]
3896impl AsRef<Path> for OsString {
3897 #[inline]
3898 fn as_ref(&self) -> &Path {
3899 Path::new(self)
3900 }
3901}
3902
3903#[stable(feature = "rust1", since = "1.0.0")]
3904impl AsRef<Path> for str {
3905 #[inline]
3906 fn as_ref(&self) -> &Path {
3907 Path::new(self)
3908 }
3909}
3910
3911#[stable(feature = "rust1", since = "1.0.0")]
3912impl AsRef<Path> for String {
3913 #[inline]
3914 fn as_ref(&self) -> &Path {
3915 Path::new(self)
3916 }
3917}
3918
3919#[stable(feature = "rust1", since = "1.0.0")]
3920impl AsRef<Path> for PathBuf {
3921 #[inline]
3922 fn as_ref(&self) -> &Path {
3923 self
3924 }
3925}
3926
3927#[stable(feature = "path_into_iter", since = "1.6.0")]
3928impl<'a> IntoIterator for &'a PathBuf {
3929 type Item = &'a OsStr;
3930 type IntoIter = Iter<'a>;
3931 #[inline]
3932 fn into_iter(self) -> Iter<'a> {
3933 self.iter()
3934 }
3935}
3936
3937#[stable(feature = "path_into_iter", since = "1.6.0")]
3938impl<'a> IntoIterator for &'a Path {
3939 type Item = &'a OsStr;
3940 type IntoIter = Iter<'a>;
3941 #[inline]
3942 fn into_iter(self) -> Iter<'a> {
3943 self.iter()
3944 }
3945}
3946
3947macro_rules! impl_cmp {
3948 ($lhs:ty, $rhs: ty) => {
3949 #[stable(feature = "partialeq_path", since = "1.6.0")]
3950 impl PartialEq<$rhs> for $lhs {
3951 #[inline]
3952 fn eq(&self, other: &$rhs) -> bool {
3953 <Path as PartialEq>::eq(self, other)
3954 }
3955 }
3956
3957 #[stable(feature = "partialeq_path", since = "1.6.0")]
3958 impl PartialEq<$lhs> for $rhs {
3959 #[inline]
3960 fn eq(&self, other: &$lhs) -> bool {
3961 <Path as PartialEq>::eq(self, other)
3962 }
3963 }
3964
3965 #[stable(feature = "cmp_path", since = "1.8.0")]
3966 impl PartialOrd<$rhs> for $lhs {
3967 #[inline]
3968 fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
3969 <Path as PartialOrd>::partial_cmp(self, other)
3970 }
3971 }
3972
3973 #[stable(feature = "cmp_path", since = "1.8.0")]
3974 impl PartialOrd<$lhs> for $rhs {
3975 #[inline]
3976 fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
3977 <Path as PartialOrd>::partial_cmp(self, other)
3978 }
3979 }
3980 };
3981}
3982
3983#[stable(feature = "partialeq_path", since = "1.6.0")]
impl PartialEq<Path> for PathBuf {
#[inline]
fn eq(&self, other: &Path) -> bool {
<Path as PartialEq>::eq(self, other)
}
}
#[stable(feature = "partialeq_path", since = "1.6.0")]
impl PartialEq<PathBuf> for Path {
#[inline]
fn eq(&self, other: &PathBuf) -> bool {
<Path as PartialEq>::eq(self, other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Path> for PathBuf {
#[inline]
fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<PathBuf> for Path {
#[inline]
fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other)
}
}impl_cmp!(PathBuf, Path);
3984#[stable(feature = "partialeq_path", since = "1.6.0")]
impl PartialEq<&Path> for PathBuf {
#[inline]
fn eq(&self, other: &&Path) -> bool {
<Path as PartialEq>::eq(self, other)
}
}
#[stable(feature = "partialeq_path", since = "1.6.0")]
impl PartialEq<PathBuf> for &Path {
#[inline]
fn eq(&self, other: &PathBuf) -> bool {
<Path as PartialEq>::eq(self, other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<&Path> for PathBuf {
#[inline]
fn partial_cmp(&self, other: &&Path) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<PathBuf> for &Path {
#[inline]
fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other)
}
}impl_cmp!(PathBuf, &Path);
3985#[stable(feature = "partialeq_path", since = "1.6.0")]
impl PartialEq<Path> for Cow<'_, Path> {
#[inline]
fn eq(&self, other: &Path) -> bool {
<Path as PartialEq>::eq(self, other)
}
}
#[stable(feature = "partialeq_path", since = "1.6.0")]
impl PartialEq<Cow<'_, Path>> for Path {
#[inline]
fn eq(&self, other: &Cow<'_, Path>) -> bool {
<Path as PartialEq>::eq(self, other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Path> for Cow<'_, Path> {
#[inline]
fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Cow<'_, Path>> for Path {
#[inline]
fn partial_cmp(&self, other: &Cow<'_, Path>) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other)
}
}impl_cmp!(Cow<'_, Path>, Path);
3986#[stable(feature = "partialeq_path", since = "1.6.0")]
impl PartialEq<&Path> for Cow<'_, Path> {
#[inline]
fn eq(&self, other: &&Path) -> bool {
<Path as PartialEq>::eq(self, other)
}
}
#[stable(feature = "partialeq_path", since = "1.6.0")]
impl PartialEq<Cow<'_, Path>> for &Path {
#[inline]
fn eq(&self, other: &Cow<'_, Path>) -> bool {
<Path as PartialEq>::eq(self, other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<&Path> for Cow<'_, Path> {
#[inline]
fn partial_cmp(&self, other: &&Path) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Cow<'_, Path>> for &Path {
#[inline]
fn partial_cmp(&self, other: &Cow<'_, Path>) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other)
}
}impl_cmp!(Cow<'_, Path>, &Path);
3987#[stable(feature = "partialeq_path", since = "1.6.0")]
impl PartialEq<PathBuf> for Cow<'_, Path> {
#[inline]
fn eq(&self, other: &PathBuf) -> bool {
<Path as PartialEq>::eq(self, other)
}
}
#[stable(feature = "partialeq_path", since = "1.6.0")]
impl PartialEq<Cow<'_, Path>> for PathBuf {
#[inline]
fn eq(&self, other: &Cow<'_, Path>) -> bool {
<Path as PartialEq>::eq(self, other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<PathBuf> for Cow<'_, Path> {
#[inline]
fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Cow<'_, Path>> for PathBuf {
#[inline]
fn partial_cmp(&self, other: &Cow<'_, Path>) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other)
}
}impl_cmp!(Cow<'_, Path>, PathBuf);
3988
3989macro_rules! impl_cmp_os_str {
3990 ($lhs:ty, $rhs: ty) => {
3991 #[stable(feature = "cmp_path", since = "1.8.0")]
3992 impl PartialEq<$rhs> for $lhs {
3993 #[inline]
3994 fn eq(&self, other: &$rhs) -> bool {
3995 <Path as PartialEq>::eq(self, other.as_ref())
3996 }
3997 }
3998
3999 #[stable(feature = "cmp_path", since = "1.8.0")]
4000 impl PartialEq<$lhs> for $rhs {
4001 #[inline]
4002 fn eq(&self, other: &$lhs) -> bool {
4003 <Path as PartialEq>::eq(self.as_ref(), other)
4004 }
4005 }
4006
4007 #[stable(feature = "cmp_path", since = "1.8.0")]
4008 impl PartialOrd<$rhs> for $lhs {
4009 #[inline]
4010 fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
4011 <Path as PartialOrd>::partial_cmp(self, other.as_ref())
4012 }
4013 }
4014
4015 #[stable(feature = "cmp_path", since = "1.8.0")]
4016 impl PartialOrd<$lhs> for $rhs {
4017 #[inline]
4018 fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
4019 <Path as PartialOrd>::partial_cmp(self.as_ref(), other)
4020 }
4021 }
4022 };
4023}
4024
4025#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<OsStr> for PathBuf {
#[inline]
fn eq(&self, other: &OsStr) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<PathBuf> for OsStr {
#[inline]
fn eq(&self, other: &PathBuf) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<OsStr> for PathBuf {
#[inline]
fn partial_cmp(&self, other: &OsStr) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<PathBuf> for OsStr {
#[inline]
fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(PathBuf, OsStr);
4026#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<&OsStr> for PathBuf {
#[inline]
fn eq(&self, other: &&OsStr) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<PathBuf> for &OsStr {
#[inline]
fn eq(&self, other: &PathBuf) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<&OsStr> for PathBuf {
#[inline]
fn partial_cmp(&self, other: &&OsStr) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<PathBuf> for &OsStr {
#[inline]
fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(PathBuf, &OsStr);
4027#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<Cow<'_, OsStr>> for PathBuf {
#[inline]
fn eq(&self, other: &Cow<'_, OsStr>) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<PathBuf> for Cow<'_, OsStr> {
#[inline]
fn eq(&self, other: &PathBuf) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Cow<'_, OsStr>> for PathBuf {
#[inline]
fn partial_cmp(&self, other: &Cow<'_, OsStr>) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<PathBuf> for Cow<'_, OsStr> {
#[inline]
fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(PathBuf, Cow<'_, OsStr>);
4028#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<OsString> for PathBuf {
#[inline]
fn eq(&self, other: &OsString) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<PathBuf> for OsString {
#[inline]
fn eq(&self, other: &PathBuf) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<OsString> for PathBuf {
#[inline]
fn partial_cmp(&self, other: &OsString) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<PathBuf> for OsString {
#[inline]
fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(PathBuf, OsString);
4029#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<OsStr> for Path {
#[inline]
fn eq(&self, other: &OsStr) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<Path> for OsStr {
#[inline]
fn eq(&self, other: &Path) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<OsStr> for Path {
#[inline]
fn partial_cmp(&self, other: &OsStr) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Path> for OsStr {
#[inline]
fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(Path, OsStr);
4030#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<&OsStr> for Path {
#[inline]
fn eq(&self, other: &&OsStr) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<Path> for &OsStr {
#[inline]
fn eq(&self, other: &Path) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<&OsStr> for Path {
#[inline]
fn partial_cmp(&self, other: &&OsStr) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Path> for &OsStr {
#[inline]
fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(Path, &OsStr);
4031#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<Cow<'_, OsStr>> for Path {
#[inline]
fn eq(&self, other: &Cow<'_, OsStr>) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<Path> for Cow<'_, OsStr> {
#[inline]
fn eq(&self, other: &Path) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Cow<'_, OsStr>> for Path {
#[inline]
fn partial_cmp(&self, other: &Cow<'_, OsStr>) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Path> for Cow<'_, OsStr> {
#[inline]
fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(Path, Cow<'_, OsStr>);
4032#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<OsString> for Path {
#[inline]
fn eq(&self, other: &OsString) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<Path> for OsString {
#[inline]
fn eq(&self, other: &Path) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<OsString> for Path {
#[inline]
fn partial_cmp(&self, other: &OsString) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Path> for OsString {
#[inline]
fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(Path, OsString);
4033#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<OsStr> for &Path {
#[inline]
fn eq(&self, other: &OsStr) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<&Path> for OsStr {
#[inline]
fn eq(&self, other: &&Path) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<OsStr> for &Path {
#[inline]
fn partial_cmp(&self, other: &OsStr) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<&Path> for OsStr {
#[inline]
fn partial_cmp(&self, other: &&Path) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(&Path, OsStr);
4034#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<Cow<'_, OsStr>> for &Path {
#[inline]
fn eq(&self, other: &Cow<'_, OsStr>) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<&Path> for Cow<'_, OsStr> {
#[inline]
fn eq(&self, other: &&Path) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Cow<'_, OsStr>> for &Path {
#[inline]
fn partial_cmp(&self, other: &Cow<'_, OsStr>) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<&Path> for Cow<'_, OsStr> {
#[inline]
fn partial_cmp(&self, other: &&Path) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(&Path, Cow<'_, OsStr>);
4035#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<OsString> for &Path {
#[inline]
fn eq(&self, other: &OsString) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<&Path> for OsString {
#[inline]
fn eq(&self, other: &&Path) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<OsString> for &Path {
#[inline]
fn partial_cmp(&self, other: &OsString) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<&Path> for OsString {
#[inline]
fn partial_cmp(&self, other: &&Path) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(&Path, OsString);
4036#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<OsStr> for Cow<'_, Path> {
#[inline]
fn eq(&self, other: &OsStr) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<Cow<'_, Path>> for OsStr {
#[inline]
fn eq(&self, other: &Cow<'_, Path>) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<OsStr> for Cow<'_, Path> {
#[inline]
fn partial_cmp(&self, other: &OsStr) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Cow<'_, Path>> for OsStr {
#[inline]
fn partial_cmp(&self, other: &Cow<'_, Path>) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(Cow<'_, Path>, OsStr);
4037#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<&OsStr> for Cow<'_, Path> {
#[inline]
fn eq(&self, other: &&OsStr) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<Cow<'_, Path>> for &OsStr {
#[inline]
fn eq(&self, other: &Cow<'_, Path>) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<&OsStr> for Cow<'_, Path> {
#[inline]
fn partial_cmp(&self, other: &&OsStr) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Cow<'_, Path>> for &OsStr {
#[inline]
fn partial_cmp(&self, other: &Cow<'_, Path>) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(Cow<'_, Path>, &OsStr);
4038#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<OsString> for Cow<'_, Path> {
#[inline]
fn eq(&self, other: &OsString) -> bool {
<Path as PartialEq>::eq(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialEq<Cow<'_, Path>> for OsString {
#[inline]
fn eq(&self, other: &Cow<'_, Path>) -> bool {
<Path as PartialEq>::eq(self.as_ref(), other)
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<OsString> for Cow<'_, Path> {
#[inline]
fn partial_cmp(&self, other: &OsString) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other.as_ref())
}
}
#[stable(feature = "cmp_path", since = "1.8.0")]
impl PartialOrd<Cow<'_, Path>> for OsString {
#[inline]
fn partial_cmp(&self, other: &Cow<'_, Path>) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self.as_ref(), other)
}
}impl_cmp_os_str!(Cow<'_, Path>, OsString);
4039
4040#[stable(since = "1.7.0", feature = "strip_prefix")]
4041impl fmt::Display for StripPrefixError {
4042 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4043 "prefix not found".fmt(f)
4044 }
4045}
4046
4047#[stable(since = "1.7.0", feature = "strip_prefix")]
4048impl Error for StripPrefixError {}
4049
4050#[unstable(feature = "normalize_lexically", issue = "134694")]
4051impl fmt::Display for NormalizeError {
4052 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4053 f.write_str("parent reference `..` points outside of base directory")
4054 }
4055}
4056#[unstable(feature = "normalize_lexically", issue = "134694")]
4057impl Error for NormalizeError {}
4058
4059#[stable(feature = "absolute_path", since = "1.79.0")]
4144pub fn absolute<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
4145 let path = path.as_ref();
4146 if path.as_os_str().is_empty() {
4147 Err(::core::hint::must_use(::core::io::Error::from_static_message(const {
&::core::io::SimpleMessage {
kind: io::ErrorKind::InvalidInput,
message: "cannot make an empty path absolute",
}
}))io::const_error!(io::ErrorKind::InvalidInput, "cannot make an empty path absolute"))
4148 } else {
4149 sys::path::absolute(path)
4150 }
4151}