1#![stable(feature = "rust1", since = "1.0.0")]
82#![deny(unsafe_op_in_unsafe_fn)]
83
84use core::clone::CloneToUninit;
85
86use crate::borrow::{Borrow, Cow};
87use crate::collections::TryReserveError;
88use crate::error::Error;
89use crate::ffi::{OsStr, OsString, os_str};
90use crate::hash::{Hash, Hasher};
91use crate::iter::FusedIterator;
92use crate::ops::{self, Deref};
93use crate::rc::Rc;
94use crate::str::FromStr;
95use crate::sync::Arc;
96use crate::sys::path::{HAS_PREFIXES, is_sep_byte, is_verbatim_sep, parse_prefix};
97use crate::{cmp, fmt, fs, io, sys};
98
99#[derive(#[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::marker::Copy for Prefix<'a> { }Copy, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::clone::Clone for Prefix<'a> {
#[inline]
fn clone(&self) -> Prefix<'a> {
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]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Prefix::Verbatim(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Verbatim", &__self_0),
Prefix::VerbatimUNC(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"VerbatimUNC", __self_0, &__self_1),
Prefix::VerbatimDisk(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"VerbatimDisk", &__self_0),
Prefix::DeviceNS(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"DeviceNS", &__self_0),
Prefix::UNC(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f, "UNC",
__self_0, &__self_1),
Prefix::Disk(__self_0) =>
::core::fmt::Formatter::debug_tuple_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) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state);
match self {
Prefix::Verbatim(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
Prefix::VerbatimUNC(__self_0, __self_1) => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
Prefix::VerbatimDisk(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
Prefix::DeviceNS(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
Prefix::UNC(__self_0, __self_1) => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
Prefix::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: &Prefix<'a>)
-> ::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: &Prefix<'a>) -> ::core::cmp::Ordering {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
match ::core::cmp::Ord::cmp(&__self_discr, &__arg1_discr) {
::core::cmp::Ordering::Equal =>
match (self, other) {
(Prefix::Verbatim(__self_0), Prefix::Verbatim(__arg1_0)) =>
::core::cmp::Ord::cmp(__self_0, __arg1_0),
(Prefix::VerbatimUNC(__self_0, __self_1),
Prefix::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,
},
(Prefix::VerbatimDisk(__self_0),
Prefix::VerbatimDisk(__arg1_0)) =>
::core::cmp::Ord::cmp(__self_0, __arg1_0),
(Prefix::DeviceNS(__self_0), Prefix::DeviceNS(__arg1_0)) =>
::core::cmp::Ord::cmp(__self_0, __arg1_0),
(Prefix::UNC(__self_0, __self_1),
Prefix::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,
},
(Prefix::Disk(__self_0), Prefix::Disk(__arg1_0)) =>
::core::cmp::Ord::cmp(__self_0, __arg1_0),
_ => unsafe { ::core::intrinsics::unreachable() }
},
cmp => cmp,
}
}
}Ord, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::cmp::PartialEq for Prefix<'a> {
#[inline]
fn eq(&self, other: &Prefix<'a>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(Prefix::Verbatim(__self_0), Prefix::Verbatim(__arg1_0)) =>
__self_0 == __arg1_0,
(Prefix::VerbatimUNC(__self_0, __self_1),
Prefix::VerbatimUNC(__arg1_0, __arg1_1)) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(Prefix::VerbatimDisk(__self_0),
Prefix::VerbatimDisk(__arg1_0)) => __self_0 == __arg1_0,
(Prefix::DeviceNS(__self_0), Prefix::DeviceNS(__arg1_0)) =>
__self_0 == __arg1_0,
(Prefix::UNC(__self_0, __self_1),
Prefix::UNC(__arg1_0, __arg1_1)) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(Prefix::Disk(__self_0), Prefix::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)]
150#[stable(feature = "rust1", since = "1.0.0")]
151pub enum Prefix<'a> {
152 #[stable(feature = "rust1", since = "1.0.0")]
157 Verbatim(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
158
159 #[stable(feature = "rust1", since = "1.0.0")]
165 VerbatimUNC(
166 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
167 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
168 ),
169
170 #[stable(feature = "rust1", since = "1.0.0")]
175 VerbatimDisk(#[stable(feature = "rust1", since = "1.0.0")] u8),
176
177 #[stable(feature = "rust1", since = "1.0.0")]
182 DeviceNS(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
183
184 #[stable(feature = "rust1", since = "1.0.0")]
189 UNC(
190 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
191 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
192 ),
193
194 #[stable(feature = "rust1", since = "1.0.0")]
196 Disk(#[stable(feature = "rust1", since = "1.0.0")] u8),
197}
198
199impl<'a> Prefix<'a> {
200 #[inline]
201 fn len(&self) -> usize {
202 use self::Prefix::*;
203 fn os_str_len(s: &OsStr) -> usize {
204 s.as_encoded_bytes().len()
205 }
206 match *self {
207 Verbatim(x) => 4 + os_str_len(x),
208 VerbatimUNC(x, y) => {
209 8 + os_str_len(x) + if os_str_len(y) > 0 { 1 + os_str_len(y) } else { 0 }
210 }
211 VerbatimDisk(_) => 6,
212 UNC(x, y) => 2 + os_str_len(x) + if os_str_len(y) > 0 { 1 + os_str_len(y) } else { 0 },
213 DeviceNS(x) => 4 + os_str_len(x),
214 Disk(_) => 2,
215 }
216 }
217
218 #[inline]
234 #[must_use]
235 #[stable(feature = "rust1", since = "1.0.0")]
236 pub fn is_verbatim(&self) -> bool {
237 use self::Prefix::*;
238 #[allow(non_exhaustive_omitted_patterns)] match *self {
Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..) => true,
_ => false,
}matches!(*self, Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..))
239 }
240
241 #[inline]
242 fn is_drive(&self) -> bool {
243 #[allow(non_exhaustive_omitted_patterns)] match *self {
Prefix::Disk(_) => true,
_ => false,
}matches!(*self, Prefix::Disk(_))
244 }
245
246 #[inline]
247 fn has_implicit_root(&self) -> bool {
248 !self.is_drive()
249 }
250}
251
252#[must_use]
268#[stable(feature = "rust1", since = "1.0.0")]
269#[rustc_const_unstable(feature = "const_path_separators", issue = "153106")]
270pub const fn is_separator(c: char) -> bool {
271 c.is_ascii() && is_sep_byte(c as u8)
272}
273
274#[unstable(feature = "const_path_separators", issue = "153106")]
278pub const SEPARATORS: &[char] = crate::sys::path::SEPARATORS;
279
280#[unstable(feature = "const_path_separators", issue = "153106")]
284pub const SEPARATORS_STR: &[&str] = crate::sys::path::SEPARATORS_STR;
285
286#[stable(feature = "rust1", since = "1.0.0")]
289#[cfg_attr(not(test), rustc_diagnostic_item = "path_main_separator")]
290pub const MAIN_SEPARATOR: char = SEPARATORS[0];
291
292#[stable(feature = "main_separator_str", since = "1.68.0")]
295pub const MAIN_SEPARATOR_STR: &str = SEPARATORS_STR[0];
296
297fn iter_after<'a, 'b, I, J>(mut iter: I, mut prefix: J) -> Option<I>
305where
306 I: Iterator<Item = Component<'a>> + Clone,
307 J: Iterator<Item = Component<'b>>,
308{
309 loop {
310 let mut iter_next = iter.clone();
311 match (iter_next.next(), prefix.next()) {
312 (Some(ref x), Some(ref y)) if x == y => (),
313 (Some(_), Some(_)) => return None,
314 (Some(_), None) => return Some(iter),
315 (None, None) => return Some(iter),
316 (None, Some(_)) => return None,
317 }
318 iter = iter_next;
319 }
320}
321
322fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
328 let path = if let Some(p) = prefix { &s[p.len()..] } else { s };
329 !path.is_empty() && is_sep_byte(path[0])
330}
331
332fn rsplit_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
334 if file.as_encoded_bytes() == b".." {
335 return (Some(file), None);
336 }
337
338 let mut iter = file.as_encoded_bytes().rsplitn(2, |b| *b == b'.');
343 let after = iter.next();
344 let before = iter.next();
345 if before == Some(b"") {
346 (Some(file), None)
347 } else {
348 unsafe {
349 (
350 before.map(|s| OsStr::from_encoded_bytes_unchecked(s)),
351 after.map(|s| OsStr::from_encoded_bytes_unchecked(s)),
352 )
353 }
354 }
355}
356
357fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
358 let slice = file.as_encoded_bytes();
359 if slice == b".." {
360 return (file, None);
361 }
362
363 let i = match slice[1..].iter().position(|b| *b == b'.') {
368 Some(i) => i + 1,
369 None => return (file, None),
370 };
371 let before = &slice[..i];
372 let after = &slice[i + 1..];
373 unsafe {
374 (
375 OsStr::from_encoded_bytes_unchecked(before),
376 Some(OsStr::from_encoded_bytes_unchecked(after)),
377 )
378 }
379}
380
381fn validate_extension(extension: &OsStr) {
383 for &b in extension.as_encoded_bytes() {
384 if is_sep_byte(b) {
385 {
::core::panicking::panic_fmt(format_args!("extension cannot contain path separators: {0:?}",
extension));
};panic!("extension cannot contain path separators: {extension:?}");
386 }
387 }
388}
389
390#[derive(#[automatically_derived]
impl ::core::marker::Copy for State { }Copy, #[automatically_derived]
impl ::core::clone::Clone for State {
#[inline]
fn clone(&self) -> State { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for State {
#[inline]
fn eq(&self, other: &State) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::PartialOrd for State {
#[inline]
fn partial_cmp(&self, other: &State)
-> ::core::option::Option<::core::cmp::Ordering> {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
::core::cmp::PartialOrd::partial_cmp(&__self_discr, &__arg1_discr)
}
}PartialOrd, #[automatically_derived]
impl ::core::fmt::Debug for State {
#[inline]
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)]
401enum State {
402 Prefix = 0, StartDir = 1, Body = 2, Done = 3,
406}
407
408#[stable(feature = "rust1", since = "1.0.0")]
442#[derive(#[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::marker::Copy for PrefixComponent<'a> { }Copy, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::clone::Clone for PrefixComponent<'a> {
#[inline]
fn clone(&self) -> PrefixComponent<'a> {
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]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"PrefixComponent", "raw", &self.raw, "parsed", &&self.parsed)
}
}Debug)]
443pub struct PrefixComponent<'a> {
444 raw: &'a OsStr,
446
447 parsed: Prefix<'a>,
449}
450
451impl<'a> PrefixComponent<'a> {
452 #[stable(feature = "rust1", since = "1.0.0")]
457 #[must_use]
458 #[inline]
459 pub fn kind(&self) -> Prefix<'a> {
460 self.parsed
461 }
462
463 #[stable(feature = "rust1", since = "1.0.0")]
465 #[must_use]
466 #[inline]
467 pub fn as_os_str(&self) -> &'a OsStr {
468 self.raw
469 }
470}
471
472#[stable(feature = "rust1", since = "1.0.0")]
473impl<'a> PartialEq for PrefixComponent<'a> {
474 #[inline]
475 fn eq(&self, other: &PrefixComponent<'a>) -> bool {
476 self.parsed == other.parsed
477 }
478}
479
480#[stable(feature = "rust1", since = "1.0.0")]
481impl<'a> PartialOrd for PrefixComponent<'a> {
482 #[inline]
483 fn partial_cmp(&self, other: &PrefixComponent<'a>) -> Option<cmp::Ordering> {
484 PartialOrd::partial_cmp(&self.parsed, &other.parsed)
485 }
486}
487
488#[stable(feature = "rust1", since = "1.0.0")]
489impl Ord for PrefixComponent<'_> {
490 #[inline]
491 fn cmp(&self, other: &Self) -> cmp::Ordering {
492 Ord::cmp(&self.parsed, &other.parsed)
493 }
494}
495
496#[stable(feature = "rust1", since = "1.0.0")]
497impl Hash for PrefixComponent<'_> {
498 fn hash<H: Hasher>(&self, h: &mut H) {
499 self.parsed.hash(h);
500 }
501}
502
503#[derive(#[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::marker::Copy for Component<'a> { }Copy, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::clone::Clone for Component<'a> {
#[inline]
fn clone(&self) -> Component<'a> {
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::cmp::PartialEq for Component<'a> {
#[inline]
fn eq(&self, other: &Component<'a>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(Component::Prefix(__self_0), Component::Prefix(__arg1_0)) =>
__self_0 == __arg1_0,
(Component::Normal(__self_0), Component::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: &Component<'a>)
-> ::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: &Component<'a>) -> ::core::cmp::Ordering {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
match ::core::cmp::Ord::cmp(&__self_discr, &__arg1_discr) {
::core::cmp::Ordering::Equal =>
match (self, other) {
(Component::Prefix(__self_0), Component::Prefix(__arg1_0))
=> ::core::cmp::Ord::cmp(__self_0, __arg1_0),
(Component::Normal(__self_0), Component::Normal(__arg1_0))
=> ::core::cmp::Ord::cmp(__self_0, __arg1_0),
_ => ::core::cmp::Ordering::Equal,
},
cmp => cmp,
}
}
}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) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state);
match self {
Component::Prefix(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
Component::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]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Component::Prefix(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Prefix",
&__self_0),
Component::RootDir =>
::core::fmt::Formatter::write_str(f, "RootDir"),
Component::CurDir =>
::core::fmt::Formatter::write_str(f, "CurDir"),
Component::ParentDir =>
::core::fmt::Formatter::write_str(f, "ParentDir"),
Component::Normal(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Normal",
&__self_0),
}
}
}Debug)]
526#[stable(feature = "rust1", since = "1.0.0")]
527pub enum Component<'a> {
528 #[stable(feature = "rust1", since = "1.0.0")]
535 Prefix(#[stable(feature = "rust1", since = "1.0.0")] PrefixComponent<'a>),
536
537 #[stable(feature = "rust1", since = "1.0.0")]
541 RootDir,
542
543 #[stable(feature = "rust1", since = "1.0.0")]
545 CurDir,
546
547 #[stable(feature = "rust1", since = "1.0.0")]
549 ParentDir,
550
551 #[stable(feature = "rust1", since = "1.0.0")]
556 Normal(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
557}
558
559impl<'a> Component<'a> {
560 #[must_use = "`self` will be dropped if the result is not used"]
572 #[stable(feature = "rust1", since = "1.0.0")]
573 pub fn as_os_str(self) -> &'a OsStr {
574 match self {
575 Component::Prefix(p) => p.as_os_str(),
576 Component::RootDir => OsStr::new(MAIN_SEPARATOR_STR),
577 Component::CurDir => OsStr::new("."),
578 Component::ParentDir => OsStr::new(".."),
579 Component::Normal(path) => path,
580 }
581 }
582}
583
584#[stable(feature = "rust1", since = "1.0.0")]
585impl AsRef<OsStr> for Component<'_> {
586 #[inline]
587 fn as_ref(&self) -> &OsStr {
588 self.as_os_str()
589 }
590}
591
592#[stable(feature = "path_component_asref", since = "1.25.0")]
593impl AsRef<Path> for Component<'_> {
594 #[inline]
595 fn as_ref(&self) -> &Path {
596 self.as_os_str().as_ref()
597 }
598}
599
600#[derive(#[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::clone::Clone for Components<'a> {
#[inline]
fn clone(&self) -> Components<'a> {
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)]
619#[must_use = "iterators are lazy and do nothing unless consumed"]
620#[stable(feature = "rust1", since = "1.0.0")]
621pub struct Components<'a> {
622 path: &'a [u8],
624
625 prefix: Option<Prefix<'a>>,
627
628 has_physical_root: bool,
632
633 front: State,
636 back: State,
637}
638
639#[derive(#[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> ::core::clone::Clone for Iter<'a> {
#[inline]
fn clone(&self) -> Iter<'a> {
Iter { inner: ::core::clone::Clone::clone(&self.inner) }
}
}Clone)]
646#[must_use = "iterators are lazy and do nothing unless consumed"]
647#[stable(feature = "rust1", since = "1.0.0")]
648pub struct Iter<'a> {
649 inner: Components<'a>,
650}
651
652#[stable(feature = "path_components_debug", since = "1.13.0")]
653impl fmt::Debug for Components<'_> {
654 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
655 struct DebugHelper<'a>(&'a Path);
656
657 impl fmt::Debug for DebugHelper<'_> {
658 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
659 f.debug_list().entries(self.0.components()).finish()
660 }
661 }
662
663 f.debug_tuple("Components").field(&DebugHelper(self.as_path())).finish()
664 }
665}
666
667impl<'a> Components<'a> {
668 #[inline]
670 fn prefix_len(&self) -> usize {
671 if !HAS_PREFIXES {
672 return 0;
673 }
674 self.prefix.as_ref().map(Prefix::len).unwrap_or(0)
675 }
676
677 #[inline]
678 fn prefix_verbatim(&self) -> bool {
679 if !HAS_PREFIXES {
680 return false;
681 }
682 self.prefix.as_ref().map(Prefix::is_verbatim).unwrap_or(false)
683 }
684
685 #[inline]
687 fn prefix_remaining(&self) -> usize {
688 if !HAS_PREFIXES {
689 return 0;
690 }
691 if self.front == State::Prefix { self.prefix_len() } else { 0 }
692 }
693
694 #[inline]
696 fn len_before_body(&self) -> usize {
697 let root = if self.front <= State::StartDir && self.has_physical_root { 1 } else { 0 };
698 let cur_dir = if self.front <= State::StartDir && self.include_cur_dir() { 1 } else { 0 };
699 self.prefix_remaining() + root + cur_dir
700 }
701
702 #[inline]
704 fn finished(&self) -> bool {
705 self.front == State::Done || self.back == State::Done || self.front > self.back
706 }
707
708 #[inline]
709 fn is_sep_byte(&self, b: u8) -> bool {
710 if self.prefix_verbatim() { is_verbatim_sep(b) } else { is_sep_byte(b) }
711 }
712
713 #[must_use]
727 #[stable(feature = "rust1", since = "1.0.0")]
728 pub fn as_path(&self) -> &'a Path {
729 let mut comps = self.clone();
730 if comps.front == State::Body {
731 comps.trim_left();
732 }
733 if comps.back == State::Body {
734 comps.trim_right();
735 }
736 unsafe { Path::from_u8_slice(comps.path) }
737 }
738
739 fn has_root(&self) -> bool {
741 if self.has_physical_root {
742 return true;
743 }
744 if HAS_PREFIXES && let Some(p) = self.prefix {
745 if p.has_implicit_root() {
746 return true;
747 }
748 }
749 false
750 }
751
752 fn include_cur_dir(&self) -> bool {
754 if self.has_root() {
755 return false;
756 }
757 let slice = &self.path[self.prefix_remaining()..];
758 match slice {
759 [b'.'] => true,
760 [b'.', b, ..] => self.is_sep_byte(*b),
761 _ => false,
762 }
763 }
764
765 unsafe fn parse_single_component<'b>(&self, comp: &'b [u8]) -> Option<Component<'b>> {
768 match comp {
769 b"." if HAS_PREFIXES && self.prefix_verbatim() => Some(Component::CurDir),
770 b"." => None, b".." => Some(Component::ParentDir),
774 b"" => None,
775 _ => Some(Component::Normal(unsafe { OsStr::from_encoded_bytes_unchecked(comp) })),
776 }
777 }
778
779 fn parse_next_component(&self) -> (usize, Option<Component<'a>>) {
782 if true {
if !(self.front == State::Body) {
::core::panicking::panic("assertion failed: self.front == State::Body")
};
};debug_assert!(self.front == State::Body);
783 let (extra, comp) = match self.path.iter().position(|b| self.is_sep_byte(*b)) {
784 None => (0, self.path),
785 Some(i) => (1, &self.path[..i]),
786 };
787 (comp.len() + extra, unsafe { self.parse_single_component(comp) })
789 }
790
791 fn parse_next_component_back(&self) -> (usize, Option<Component<'a>>) {
794 if true {
if !(self.back == State::Body) {
::core::panicking::panic("assertion failed: self.back == State::Body")
};
};debug_assert!(self.back == State::Body);
795 let start = self.len_before_body();
796 let (extra, comp) = match self.path[start..].iter().rposition(|b| self.is_sep_byte(*b)) {
797 None => (0, &self.path[start..]),
798 Some(i) => (1, &self.path[start + i + 1..]),
799 };
800 (comp.len() + extra, unsafe { self.parse_single_component(comp) })
802 }
803
804 fn trim_left(&mut self) {
806 while !self.path.is_empty() {
807 let (size, comp) = self.parse_next_component();
808 if comp.is_some() {
809 return;
810 } else {
811 self.path = &self.path[size..];
812 }
813 }
814 }
815
816 fn trim_right(&mut self) {
818 while self.path.len() > self.len_before_body() {
819 let (size, comp) = self.parse_next_component_back();
820 if comp.is_some() {
821 return;
822 } else {
823 self.path = &self.path[..self.path.len() - size];
824 }
825 }
826 }
827}
828
829#[stable(feature = "rust1", since = "1.0.0")]
830impl AsRef<Path> for Components<'_> {
831 #[inline]
832 fn as_ref(&self) -> &Path {
833 self.as_path()
834 }
835}
836
837#[stable(feature = "rust1", since = "1.0.0")]
838impl AsRef<OsStr> for Components<'_> {
839 #[inline]
840 fn as_ref(&self) -> &OsStr {
841 self.as_path().as_os_str()
842 }
843}
844
845#[stable(feature = "path_iter_debug", since = "1.13.0")]
846impl fmt::Debug for Iter<'_> {
847 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
848 struct DebugHelper<'a>(&'a Path);
849
850 impl fmt::Debug for DebugHelper<'_> {
851 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
852 f.debug_list().entries(self.0.iter()).finish()
853 }
854 }
855
856 f.debug_tuple("Iter").field(&DebugHelper(self.as_path())).finish()
857 }
858}
859
860impl<'a> Iter<'a> {
861 #[stable(feature = "rust1", since = "1.0.0")]
875 #[must_use]
876 #[inline]
877 pub fn as_path(&self) -> &'a Path {
878 self.inner.as_path()
879 }
880}
881
882#[stable(feature = "rust1", since = "1.0.0")]
883impl AsRef<Path> for Iter<'_> {
884 #[inline]
885 fn as_ref(&self) -> &Path {
886 self.as_path()
887 }
888}
889
890#[stable(feature = "rust1", since = "1.0.0")]
891impl AsRef<OsStr> for Iter<'_> {
892 #[inline]
893 fn as_ref(&self) -> &OsStr {
894 self.as_path().as_os_str()
895 }
896}
897
898#[stable(feature = "rust1", since = "1.0.0")]
899impl<'a> Iterator for Iter<'a> {
900 type Item = &'a OsStr;
901
902 #[inline]
903 fn next(&mut self) -> Option<&'a OsStr> {
904 self.inner.next().map(Component::as_os_str)
905 }
906}
907
908#[stable(feature = "rust1", since = "1.0.0")]
909impl<'a> DoubleEndedIterator for Iter<'a> {
910 #[inline]
911 fn next_back(&mut self) -> Option<&'a OsStr> {
912 self.inner.next_back().map(Component::as_os_str)
913 }
914}
915
916#[stable(feature = "fused", since = "1.26.0")]
917impl FusedIterator for Iter<'_> {}
918
919#[stable(feature = "rust1", since = "1.0.0")]
920impl<'a> Iterator for Components<'a> {
921 type Item = Component<'a>;
922
923 fn next(&mut self) -> Option<Component<'a>> {
924 while !self.finished() {
925 match self.front {
926 State::Body if !self.path.is_empty() => {
928 let (size, comp) = self.parse_next_component();
929 self.path = &self.path[size..];
930 if comp.is_some() {
931 return comp;
932 }
933 }
934 State::Body => {
935 self.front = State::Done;
936 }
937 State::StartDir => {
938 self.front = State::Body;
939 if self.has_physical_root {
940 if true {
if !!self.path.is_empty() {
::core::panicking::panic("assertion failed: !self.path.is_empty()")
};
};debug_assert!(!self.path.is_empty());
941 self.path = &self.path[1..];
942 return Some(Component::RootDir);
943 } else if HAS_PREFIXES && let Some(p) = self.prefix {
944 if p.has_implicit_root() && !p.is_verbatim() {
945 return Some(Component::RootDir);
946 }
947 } else if self.include_cur_dir() {
948 if true {
if !!self.path.is_empty() {
::core::panicking::panic("assertion failed: !self.path.is_empty()")
};
};debug_assert!(!self.path.is_empty());
949 self.path = &self.path[1..];
950 return Some(Component::CurDir);
951 }
952 }
953 _ if const { !HAS_PREFIXES } => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
954 State::Prefix if self.prefix_len() == 0 => {
955 self.front = State::StartDir;
956 }
957 State::Prefix => {
958 self.front = State::StartDir;
959 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());
960 let raw = &self.path[..self.prefix_len()];
961 self.path = &self.path[self.prefix_len()..];
962 return Some(Component::Prefix(PrefixComponent {
963 raw: unsafe { OsStr::from_encoded_bytes_unchecked(raw) },
964 parsed: self.prefix.unwrap(),
965 }));
966 }
967 State::Done => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
968 }
969 }
970 None
971 }
972}
973
974#[stable(feature = "rust1", since = "1.0.0")]
975impl<'a> DoubleEndedIterator for Components<'a> {
976 fn next_back(&mut self) -> Option<Component<'a>> {
977 while !self.finished() {
978 match self.back {
979 State::Body if self.path.len() > self.len_before_body() => {
980 let (size, comp) = self.parse_next_component_back();
981 self.path = &self.path[..self.path.len() - size];
982 if comp.is_some() {
983 return comp;
984 }
985 }
986 State::Body => {
987 self.back = State::StartDir;
988 }
989 State::StartDir => {
990 self.back = if HAS_PREFIXES { State::Prefix } else { State::Done };
991 if self.has_physical_root {
992 self.path = &self.path[..self.path.len() - 1];
993 return Some(Component::RootDir);
994 } else if HAS_PREFIXES && let Some(p) = self.prefix {
995 if p.has_implicit_root() && !p.is_verbatim() {
996 return Some(Component::RootDir);
997 }
998 } else if self.include_cur_dir() {
999 self.path = &self.path[..self.path.len() - 1];
1000 return Some(Component::CurDir);
1001 }
1002 }
1003 _ if !HAS_PREFIXES => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1004 State::Prefix if self.prefix_len() > 0 => {
1005 self.back = State::Done;
1006 return Some(Component::Prefix(PrefixComponent {
1007 raw: unsafe { OsStr::from_encoded_bytes_unchecked(self.path) },
1008 parsed: self.prefix.unwrap(),
1009 }));
1010 }
1011 State::Prefix => {
1012 self.back = State::Done;
1013 return None;
1014 }
1015 State::Done => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1016 }
1017 }
1018 None
1019 }
1020}
1021
1022#[stable(feature = "fused", since = "1.26.0")]
1023impl FusedIterator for Components<'_> {}
1024
1025#[stable(feature = "rust1", since = "1.0.0")]
1026impl<'a> PartialEq for Components<'a> {
1027 #[inline]
1028 fn eq(&self, other: &Components<'a>) -> bool {
1029 let Components { path: _, front: _, back: _, has_physical_root: _, prefix: _ } = self;
1030
1031 if self.path.len() == other.path.len()
1035 && self.front == other.front
1036 && self.back == State::Body
1037 && other.back == State::Body
1038 && self.prefix_verbatim() == other.prefix_verbatim()
1039 {
1040 if self.path == other.path {
1043 return true;
1044 }
1045 }
1046
1047 Iterator::eq(self.clone().rev(), other.clone().rev())
1049 }
1050}
1051
1052#[stable(feature = "rust1", since = "1.0.0")]
1053impl Eq for Components<'_> {}
1054
1055#[stable(feature = "rust1", since = "1.0.0")]
1056impl<'a> PartialOrd for Components<'a> {
1057 #[inline]
1058 fn partial_cmp(&self, other: &Components<'a>) -> Option<cmp::Ordering> {
1059 Some(compare_components(self.clone(), other.clone()))
1060 }
1061}
1062
1063#[stable(feature = "rust1", since = "1.0.0")]
1064impl Ord for Components<'_> {
1065 #[inline]
1066 fn cmp(&self, other: &Self) -> cmp::Ordering {
1067 compare_components(self.clone(), other.clone())
1068 }
1069}
1070
1071fn compare_components(mut left: Components<'_>, mut right: Components<'_>) -> cmp::Ordering {
1072 if left.prefix.is_none() && right.prefix.is_none() && left.front == right.front {
1082 let first_difference = match left.path.iter().zip(right.path).position(|(&a, &b)| a != b) {
1084 None if left.path.len() == right.path.len() => return cmp::Ordering::Equal,
1085 None => left.path.len().min(right.path.len()),
1086 Some(diff) => diff,
1087 };
1088
1089 if let Some(previous_sep) =
1090 left.path[..first_difference].iter().rposition(|&b| left.is_sep_byte(b))
1091 {
1092 let mismatched_component_start = previous_sep + 1;
1093 left.path = &left.path[mismatched_component_start..];
1094 left.front = State::Body;
1095 right.path = &right.path[mismatched_component_start..];
1096 right.front = State::Body;
1097 }
1098 }
1099
1100 Iterator::cmp(left, right)
1101}
1102
1103#[derive(#[automatically_derived]
#[stable(feature = "path_ancestors", since = "1.28.0")]
impl<'a> ::core::marker::Copy for Ancestors<'a> { }Copy, #[automatically_derived]
#[stable(feature = "path_ancestors", since = "1.28.0")]
impl<'a> ::core::clone::Clone for Ancestors<'a> {
#[inline]
fn clone(&self) -> Ancestors<'a> {
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]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "Ancestors",
"next", &&self.next)
}
}Debug)]
1122#[must_use = "iterators are lazy and do nothing unless consumed"]
1123#[stable(feature = "path_ancestors", since = "1.28.0")]
1124pub struct Ancestors<'a> {
1125 next: Option<&'a Path>,
1126}
1127
1128#[stable(feature = "path_ancestors", since = "1.28.0")]
1129impl<'a> Iterator for Ancestors<'a> {
1130 type Item = &'a Path;
1131
1132 #[inline]
1133 fn next(&mut self) -> Option<Self::Item> {
1134 let next = self.next;
1135 self.next = next.and_then(Path::parent);
1136 next
1137 }
1138}
1139
1140#[stable(feature = "path_ancestors", since = "1.28.0")]
1141impl FusedIterator for Ancestors<'_> {}
1142
1143#[cfg_attr(not(test), rustc_diagnostic_item = "PathBuf")]
1213#[stable(feature = "rust1", since = "1.0.0")]
1214pub struct PathBuf {
1215 inner: OsString,
1216}
1217
1218impl PathBuf {
1219 #[stable(feature = "rust1", since = "1.0.0")]
1229 #[must_use]
1230 #[inline]
1231 #[rustc_const_stable(feature = "const_pathbuf_osstring_new", since = "1.91.0")]
1232 pub const fn new() -> PathBuf {
1233 PathBuf { inner: OsString::new() }
1234 }
1235
1236 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1255 #[must_use]
1256 #[inline]
1257 pub fn with_capacity(capacity: usize) -> PathBuf {
1258 PathBuf { inner: OsString::with_capacity(capacity) }
1259 }
1260
1261 #[cfg_attr(not(test), rustc_diagnostic_item = "pathbuf_as_path")]
1272 #[stable(feature = "rust1", since = "1.0.0")]
1273 #[must_use]
1274 #[inline]
1275 pub fn as_path(&self) -> &Path {
1276 self
1277 }
1278
1279 #[stable(feature = "os_string_pathbuf_leak", since = "1.89.0")]
1293 #[inline]
1294 pub fn leak<'a>(self) -> &'a mut Path {
1295 Path::from_inner_mut(self.inner.leak())
1296 }
1297
1298 #[stable(feature = "rust1", since = "1.0.0")]
1336 #[rustc_confusables("append", "put")]
1337 pub fn push<P: AsRef<Path>>(&mut self, path: P) {
1338 self._push(path.as_ref())
1339 }
1340
1341 fn _push(&mut self, path: &Path) {
1342 let buf = self.inner.as_encoded_bytes();
1344 let mut need_sep = buf.last().map(|c| !is_sep_byte(*c)).unwrap_or(false);
1345
1346 let comps = self.components();
1348
1349 if comps.prefix_len() > 0
1350 && comps.prefix_len() == comps.path.len()
1351 && comps.prefix.unwrap().is_drive()
1352 {
1353 need_sep = false
1354 }
1355
1356 let need_clear = if falsecfg!(target_os = "cygwin") {
1357 path.prefix().is_some()
1360 } else {
1361 path.is_absolute() || path.prefix().is_some()
1363 };
1364
1365 if need_clear {
1367 self.inner.clear();
1368
1369 } else if comps.prefix_verbatim() && !path.inner.is_empty() {
1371 let mut buf: Vec<_> = comps.collect();
1372 for c in path.components() {
1373 match c {
1374 Component::RootDir => {
1375 buf.truncate(1);
1376 buf.push(c);
1377 }
1378 Component::CurDir => (),
1379 Component::ParentDir => {
1380 if let Some(Component::Normal(_)) = buf.last() {
1381 buf.pop();
1382 }
1383 }
1384 _ => buf.push(c),
1385 }
1386 }
1387
1388 let mut res = OsString::new();
1389 let mut need_sep = false;
1390
1391 for c in buf {
1392 if need_sep && c != Component::RootDir {
1393 res.push(MAIN_SEPARATOR_STR);
1394 }
1395 res.push(c.as_os_str());
1396
1397 need_sep = match c {
1398 Component::RootDir => false,
1399 Component::Prefix(prefix) => {
1400 !prefix.parsed.is_drive() && prefix.parsed.len() > 0
1401 }
1402 _ => true,
1403 }
1404 }
1405
1406 self.inner = res;
1407 return;
1408
1409 } else if path.has_root() {
1411 let prefix_len = self.components().prefix_remaining();
1412 self.inner.truncate(prefix_len);
1413
1414 } else if need_sep {
1416 self.inner.push(MAIN_SEPARATOR_STR);
1417 }
1418
1419 self.inner.push(path);
1420 }
1421
1422 #[stable(feature = "rust1", since = "1.0.0")]
1442 pub fn pop(&mut self) -> bool {
1443 match self.parent().map(|p| p.as_u8_slice().len()) {
1444 Some(len) => {
1445 self.inner.truncate(len);
1446 true
1447 }
1448 None => false,
1449 }
1450 }
1451
1452 #[unstable(feature = "path_trailing_sep", issue = "142503")]
1479 pub fn set_trailing_sep(&mut self, trailing_sep: bool) {
1480 if trailing_sep { self.push_trailing_sep() } else { self.pop_trailing_sep() }
1481 }
1482
1483 #[unstable(feature = "path_trailing_sep", issue = "142503")]
1507 pub fn push_trailing_sep(&mut self) {
1508 if !self.has_trailing_sep() {
1509 self.push("");
1510 }
1511 }
1512
1513 #[unstable(feature = "path_trailing_sep", issue = "142503")]
1541 pub fn pop_trailing_sep(&mut self) {
1542 self.inner.truncate(self.trim_trailing_sep().as_os_str().len());
1543 }
1544
1545 #[stable(feature = "rust1", since = "1.0.0")]
1585 pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
1586 self._set_file_name(file_name.as_ref())
1587 }
1588
1589 fn _set_file_name(&mut self, file_name: &OsStr) {
1590 if self.file_name().is_some() {
1591 let popped = self.pop();
1592 if true {
if !popped { ::core::panicking::panic("assertion failed: popped") };
};debug_assert!(popped);
1593 }
1594 self.push(file_name);
1595 }
1596
1597 #[stable(feature = "rust1", since = "1.0.0")]
1654 pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
1655 self._set_extension(extension.as_ref())
1656 }
1657
1658 fn _set_extension(&mut self, extension: &OsStr) -> bool {
1659 validate_extension(extension);
1660
1661 let file_stem = match self.file_stem() {
1662 None => return false,
1663 Some(f) => f.as_encoded_bytes(),
1664 };
1665
1666 let end_file_stem = file_stem[file_stem.len()..].as_ptr().addr();
1668 let start = self.inner.as_encoded_bytes().as_ptr().addr();
1669 self.inner.truncate(end_file_stem.wrapping_sub(start));
1670
1671 let new = extension.as_encoded_bytes();
1673 if !new.is_empty() {
1674 self.inner.reserve_exact(new.len() + 1);
1675 self.inner.push(".");
1676 unsafe { self.inner.extend_from_slice_unchecked(new) };
1679 }
1680
1681 true
1682 }
1683
1684 #[stable(feature = "path_add_extension", since = "1.91.0")]
1728 pub fn add_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
1729 self._add_extension(extension.as_ref())
1730 }
1731
1732 fn _add_extension(&mut self, extension: &OsStr) -> bool {
1733 validate_extension(extension);
1734
1735 let file_name = match self.file_name() {
1736 None => return false,
1737 Some(f) => f.as_encoded_bytes(),
1738 };
1739
1740 let new = extension.as_encoded_bytes();
1741 if !new.is_empty() {
1742 let end_file_name = file_name[file_name.len()..].as_ptr().addr();
1745 let start = self.inner.as_encoded_bytes().as_ptr().addr();
1746 self.inner.truncate(end_file_name.wrapping_sub(start));
1747
1748 self.inner.reserve_exact(new.len() + 1);
1750 self.inner.push(".");
1751 unsafe { self.inner.extend_from_slice_unchecked(new) };
1754 }
1755
1756 true
1757 }
1758
1759 #[stable(feature = "path_as_mut_os_str", since = "1.70.0")]
1776 #[must_use]
1777 #[inline]
1778 pub fn as_mut_os_string(&mut self) -> &mut OsString {
1779 &mut self.inner
1780 }
1781
1782 #[stable(feature = "rust1", since = "1.0.0")]
1793 #[must_use = "`self` will be dropped if the result is not used"]
1794 #[inline]
1795 pub fn into_os_string(self) -> OsString {
1796 self.inner
1797 }
1798
1799 #[stable(feature = "pathbuf_into_string", since = "1.98.0")]
1813 pub fn into_string(self) -> Result<String, PathBuf> {
1814 self.into_os_string().into_string().map_err(PathBuf::from)
1815 }
1816
1817 #[stable(feature = "into_boxed_path", since = "1.20.0")]
1819 #[must_use = "`self` will be dropped if the result is not used"]
1820 #[inline]
1821 pub fn into_boxed_path(self) -> Box<Path> {
1822 let rw = Box::into_raw(self.inner.into_boxed_os_str()) as *mut Path;
1823 unsafe { Box::from_raw(rw) }
1824 }
1825
1826 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1830 #[must_use]
1831 #[inline]
1832 pub fn capacity(&self) -> usize {
1833 self.inner.capacity()
1834 }
1835
1836 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1840 #[inline]
1841 pub fn clear(&mut self) {
1842 self.inner.clear()
1843 }
1844
1845 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1849 #[inline]
1850 pub fn reserve(&mut self, additional: usize) {
1851 self.inner.reserve(additional)
1852 }
1853
1854 #[stable(feature = "try_reserve_2", since = "1.63.0")]
1858 #[inline]
1859 pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
1860 self.inner.try_reserve(additional)
1861 }
1862
1863 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1867 #[inline]
1868 pub fn reserve_exact(&mut self, additional: usize) {
1869 self.inner.reserve_exact(additional)
1870 }
1871
1872 #[stable(feature = "try_reserve_2", since = "1.63.0")]
1876 #[inline]
1877 pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
1878 self.inner.try_reserve_exact(additional)
1879 }
1880
1881 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1885 #[inline]
1886 pub fn shrink_to_fit(&mut self) {
1887 self.inner.shrink_to_fit()
1888 }
1889
1890 #[stable(feature = "shrink_to", since = "1.56.0")]
1894 #[inline]
1895 pub fn shrink_to(&mut self, min_capacity: usize) {
1896 self.inner.shrink_to(min_capacity)
1897 }
1898}
1899
1900#[stable(feature = "rust1", since = "1.0.0")]
1901impl Clone for PathBuf {
1902 #[inline]
1903 fn clone(&self) -> Self {
1904 PathBuf { inner: self.inner.clone() }
1905 }
1906
1907 #[inline]
1912 fn clone_from(&mut self, source: &Self) {
1913 self.inner.clone_from(&source.inner)
1914 }
1915}
1916
1917#[stable(feature = "box_from_path", since = "1.17.0")]
1918impl From<&Path> for Box<Path> {
1919 fn from(path: &Path) -> Box<Path> {
1923 Box::clone_from_ref(path)
1924 }
1925}
1926
1927#[stable(feature = "box_from_mut_slice", since = "1.84.0")]
1928impl From<&mut Path> for Box<Path> {
1929 fn from(path: &mut Path) -> Box<Path> {
1933 Self::from(&*path)
1934 }
1935}
1936
1937#[stable(feature = "box_from_cow", since = "1.45.0")]
1938impl From<Cow<'_, Path>> for Box<Path> {
1939 #[inline]
1943 fn from(cow: Cow<'_, Path>) -> Box<Path> {
1944 match cow {
1945 Cow::Borrowed(path) => Box::from(path),
1946 Cow::Owned(path) => Box::from(path),
1947 }
1948 }
1949}
1950
1951#[stable(feature = "path_buf_from_box", since = "1.18.0")]
1952impl From<Box<Path>> for PathBuf {
1953 #[inline]
1957 fn from(boxed: Box<Path>) -> PathBuf {
1958 boxed.into_path_buf()
1959 }
1960}
1961
1962#[stable(feature = "box_from_path_buf", since = "1.20.0")]
1963impl From<PathBuf> for Box<Path> {
1964 #[inline]
1969 fn from(p: PathBuf) -> Box<Path> {
1970 p.into_boxed_path()
1971 }
1972}
1973
1974#[stable(feature = "more_box_slice_clone", since = "1.29.0")]
1975impl Clone for Box<Path> {
1976 #[inline]
1977 fn clone(&self) -> Self {
1978 self.to_path_buf().into_boxed_path()
1979 }
1980}
1981
1982#[stable(feature = "rust1", since = "1.0.0")]
1983impl<T: ?Sized + AsRef<OsStr>> From<&T> for PathBuf {
1984 #[inline]
1988 fn from(s: &T) -> PathBuf {
1989 PathBuf::from(s.as_ref().to_os_string())
1990 }
1991}
1992
1993#[stable(feature = "rust1", since = "1.0.0")]
1994impl From<OsString> for PathBuf {
1995 #[inline]
1999 fn from(s: OsString) -> PathBuf {
2000 PathBuf { inner: s }
2001 }
2002}
2003
2004#[stable(feature = "from_path_buf_for_os_string", since = "1.14.0")]
2005impl From<PathBuf> for OsString {
2006 #[inline]
2010 fn from(path_buf: PathBuf) -> OsString {
2011 path_buf.inner
2012 }
2013}
2014
2015#[stable(feature = "rust1", since = "1.0.0")]
2016impl From<String> for PathBuf {
2017 #[inline]
2021 fn from(s: String) -> PathBuf {
2022 PathBuf::from(OsString::from(s))
2023 }
2024}
2025
2026#[stable(feature = "path_from_str", since = "1.32.0")]
2027impl FromStr for PathBuf {
2028 type Err = core::convert::Infallible;
2029
2030 #[inline]
2031 fn from_str(s: &str) -> Result<Self, Self::Err> {
2032 Ok(PathBuf::from(s))
2033 }
2034}
2035
2036#[stable(feature = "rust1", since = "1.0.0")]
2037impl<P: AsRef<Path>> FromIterator<P> for PathBuf {
2038 fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> PathBuf {
2052 let mut buf = PathBuf::new();
2053 buf.extend(iter);
2054 buf
2055 }
2056}
2057
2058#[stable(feature = "rust1", since = "1.0.0")]
2059impl<P: AsRef<Path>> Extend<P> for PathBuf {
2060 fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) {
2075 iter.into_iter().for_each(move |p| self.push(p.as_ref()));
2076 }
2077
2078 #[inline]
2079 fn extend_one(&mut self, p: P) {
2080 self.push(p.as_ref());
2081 }
2082}
2083
2084#[stable(feature = "rust1", since = "1.0.0")]
2085impl fmt::Debug for PathBuf {
2086 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2087 fmt::Debug::fmt(&**self, formatter)
2088 }
2089}
2090
2091#[stable(feature = "rust1", since = "1.0.0")]
2092impl ops::Deref for PathBuf {
2093 type Target = Path;
2094 #[inline]
2095 fn deref(&self) -> &Path {
2096 Path::new(&self.inner)
2097 }
2098}
2099
2100#[stable(feature = "path_buf_deref_mut", since = "1.68.0")]
2101impl ops::DerefMut for PathBuf {
2102 #[inline]
2103 fn deref_mut(&mut self) -> &mut Path {
2104 Path::from_inner_mut(&mut self.inner)
2105 }
2106}
2107
2108#[stable(feature = "rust1", since = "1.0.0")]
2109impl Borrow<Path> for PathBuf {
2110 #[inline]
2111 fn borrow(&self) -> &Path {
2112 self.deref()
2113 }
2114}
2115
2116#[stable(feature = "default_for_pathbuf", since = "1.17.0")]
2117impl Default for PathBuf {
2118 #[inline]
2119 fn default() -> Self {
2120 PathBuf::new()
2121 }
2122}
2123
2124#[stable(feature = "cow_from_path", since = "1.6.0")]
2125impl<'a> From<&'a Path> for Cow<'a, Path> {
2126 #[inline]
2131 fn from(s: &'a Path) -> Cow<'a, Path> {
2132 Cow::Borrowed(s)
2133 }
2134}
2135
2136#[stable(feature = "cow_from_path", since = "1.6.0")]
2137impl<'a> From<PathBuf> for Cow<'a, Path> {
2138 #[inline]
2143 fn from(s: PathBuf) -> Cow<'a, Path> {
2144 Cow::Owned(s)
2145 }
2146}
2147
2148#[stable(feature = "cow_from_pathbuf_ref", since = "1.28.0")]
2149impl<'a> From<&'a PathBuf> for Cow<'a, Path> {
2150 #[inline]
2155 fn from(p: &'a PathBuf) -> Cow<'a, Path> {
2156 Cow::Borrowed(p.as_path())
2157 }
2158}
2159
2160#[stable(feature = "pathbuf_from_cow_path", since = "1.28.0")]
2161impl<'a> From<Cow<'a, Path>> for PathBuf {
2162 #[inline]
2166 fn from(p: Cow<'a, Path>) -> Self {
2167 p.into_owned()
2168 }
2169}
2170
2171#[stable(feature = "shared_from_slice2", since = "1.24.0")]
2172impl From<PathBuf> for Arc<Path> {
2173 #[inline]
2176 fn from(s: PathBuf) -> Arc<Path> {
2177 let arc: Arc<OsStr> = Arc::from(s.into_os_string());
2178 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Path) }
2179 }
2180}
2181
2182#[stable(feature = "shared_from_slice2", since = "1.24.0")]
2183impl From<&Path> for Arc<Path> {
2184 #[inline]
2186 fn from(s: &Path) -> Arc<Path> {
2187 let arc: Arc<OsStr> = Arc::from(s.as_os_str());
2188 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Path) }
2189 }
2190}
2191
2192#[stable(feature = "shared_from_mut_slice", since = "1.84.0")]
2193impl From<&mut Path> for Arc<Path> {
2194 #[inline]
2196 fn from(s: &mut Path) -> Arc<Path> {
2197 Arc::from(&*s)
2198 }
2199}
2200
2201#[stable(feature = "shared_from_slice2", since = "1.24.0")]
2202impl From<PathBuf> for Rc<Path> {
2203 #[inline]
2206 fn from(s: PathBuf) -> Rc<Path> {
2207 let rc: Rc<OsStr> = Rc::from(s.into_os_string());
2208 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Path) }
2209 }
2210}
2211
2212#[stable(feature = "shared_from_slice2", since = "1.24.0")]
2213impl From<&Path> for Rc<Path> {
2214 #[inline]
2216 fn from(s: &Path) -> Rc<Path> {
2217 let rc: Rc<OsStr> = Rc::from(s.as_os_str());
2218 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Path) }
2219 }
2220}
2221
2222#[stable(feature = "shared_from_mut_slice", since = "1.84.0")]
2223impl From<&mut Path> for Rc<Path> {
2224 #[inline]
2226 fn from(s: &mut Path) -> Rc<Path> {
2227 Rc::from(&*s)
2228 }
2229}
2230
2231#[stable(feature = "rust1", since = "1.0.0")]
2232impl ToOwned for Path {
2233 type Owned = PathBuf;
2234 #[inline]
2235 fn to_owned(&self) -> PathBuf {
2236 self.to_path_buf()
2237 }
2238 #[inline]
2239 fn clone_into(&self, target: &mut PathBuf) {
2240 self.inner.clone_into(&mut target.inner);
2241 }
2242}
2243
2244#[stable(feature = "rust1", since = "1.0.0")]
2245impl PartialEq for PathBuf {
2246 #[inline]
2247 fn eq(&self, other: &PathBuf) -> bool {
2248 self.components() == other.components()
2249 }
2250}
2251
2252#[stable(feature = "eq_str_for_path", since = "1.91.0")]
2253impl cmp::PartialEq<str> for PathBuf {
2254 #[inline]
2255 fn eq(&self, other: &str) -> bool {
2256 self.as_path() == other
2257 }
2258}
2259
2260#[stable(feature = "eq_str_for_path", since = "1.91.0")]
2261impl cmp::PartialEq<PathBuf> for str {
2262 #[inline]
2263 fn eq(&self, other: &PathBuf) -> bool {
2264 self == other.as_path()
2265 }
2266}
2267
2268#[stable(feature = "eq_str_for_path", since = "1.91.0")]
2269impl cmp::PartialEq<String> for PathBuf {
2270 #[inline]
2271 fn eq(&self, other: &String) -> bool {
2272 self.as_path() == other.as_str()
2273 }
2274}
2275
2276#[stable(feature = "eq_str_for_path", since = "1.91.0")]
2277impl cmp::PartialEq<PathBuf> for String {
2278 #[inline]
2279 fn eq(&self, other: &PathBuf) -> bool {
2280 self.as_str() == other.as_path()
2281 }
2282}
2283
2284#[stable(feature = "rust1", since = "1.0.0")]
2285impl Hash for PathBuf {
2286 fn hash<H: Hasher>(&self, h: &mut H) {
2287 self.as_path().hash(h)
2288 }
2289}
2290
2291#[stable(feature = "rust1", since = "1.0.0")]
2292impl Eq for PathBuf {}
2293
2294#[stable(feature = "rust1", since = "1.0.0")]
2295impl PartialOrd for PathBuf {
2296 #[inline]
2297 fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
2298 Some(compare_components(self.components(), other.components()))
2299 }
2300}
2301
2302#[stable(feature = "rust1", since = "1.0.0")]
2303impl Ord for PathBuf {
2304 #[inline]
2305 fn cmp(&self, other: &PathBuf) -> cmp::Ordering {
2306 compare_components(self.components(), other.components())
2307 }
2308}
2309
2310#[stable(feature = "rust1", since = "1.0.0")]
2311impl AsRef<OsStr> for PathBuf {
2312 #[inline]
2313 fn as_ref(&self) -> &OsStr {
2314 &self.inner[..]
2315 }
2316}
2317
2318#[cfg_attr(not(test), rustc_diagnostic_item = "Path")]
2351#[stable(feature = "rust1", since = "1.0.0")]
2352#[repr(transparent)]
2356pub struct Path {
2357 inner: OsStr,
2358}
2359
2360#[derive(#[automatically_derived]
#[stable(since = "1.7.0", feature = "strip_prefix")]
impl ::core::fmt::Debug for StripPrefixError {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"StripPrefixError", &&self.0)
}
}Debug, #[automatically_derived]
#[stable(since = "1.7.0", feature = "strip_prefix")]
impl ::core::clone::Clone for StripPrefixError {
#[inline]
fn clone(&self) -> StripPrefixError {
StripPrefixError(::core::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]
#[stable(since = "1.7.0", feature = "strip_prefix")]
impl ::core::cmp::PartialEq for StripPrefixError {
#[inline]
fn eq(&self, other: &StripPrefixError) -> 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)]
2367#[stable(since = "1.7.0", feature = "strip_prefix")]
2368pub struct StripPrefixError(());
2369
2370#[unstable(feature = "normalize_lexically", issue = "134694")]
2373#[derive(#[automatically_derived]
#[unstable(feature = "normalize_lexically", issue = "134694")]
impl ::core::fmt::Debug for NormalizeError {
#[inline]
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::cmp::PartialEq for NormalizeError {
#[inline]
fn eq(&self, other: &NormalizeError) -> bool { true }
}PartialEq)]
2374#[non_exhaustive]
2375pub struct NormalizeError;
2376
2377impl Path {
2378 unsafe fn from_u8_slice(s: &[u8]) -> &Path {
2381 unsafe { Path::new(OsStr::from_encoded_bytes_unchecked(s)) }
2382 }
2383 pub(crate) fn as_u8_slice(&self) -> &[u8] {
2385 self.inner.as_encoded_bytes()
2386 }
2387
2388 #[stable(feature = "rust1", since = "1.0.0")]
2411 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2412 pub const fn new<S: [const] AsRef<OsStr> + ?Sized>(s: &S) -> &Path {
2413 unsafe { &*(s.as_ref() as *const OsStr as *const Path) }
2414 }
2415
2416 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2417 const fn from_inner_mut(inner: &mut OsStr) -> &mut Path {
2418 unsafe { &mut *(inner as *mut OsStr as *mut Path) }
2421 }
2422
2423 #[stable(feature = "rust1", since = "1.0.0")]
2434 #[must_use]
2435 #[inline]
2436 pub fn as_os_str(&self) -> &OsStr {
2437 &self.inner
2438 }
2439
2440 #[stable(feature = "path_as_mut_os_str", since = "1.70.0")]
2455 #[must_use]
2456 #[inline]
2457 pub fn as_mut_os_str(&mut self) -> &mut OsStr {
2458 &mut self.inner
2459 }
2460
2461 #[stable(feature = "rust1", since = "1.0.0")]
2478 #[must_use = "this returns the result of the operation, \
2479 without modifying the original"]
2480 #[inline]
2481 pub fn to_str(&self) -> Option<&str> {
2482 self.inner.to_str()
2483 }
2484
2485 #[stable(feature = "rust1", since = "1.0.0")]
2506 #[must_use = "this returns the result of the operation, \
2507 without modifying the original"]
2508 #[inline]
2509 pub fn to_string_lossy(&self) -> Cow<'_, str> {
2510 self.inner.to_string_lossy()
2511 }
2512
2513 #[rustc_conversion_suggestion]
2524 #[must_use = "this returns the result of the operation, \
2525 without modifying the original"]
2526 #[stable(feature = "rust1", since = "1.0.0")]
2527 #[cfg_attr(not(test), rustc_diagnostic_item = "path_to_pathbuf")]
2528 pub fn to_path_buf(&self) -> PathBuf {
2529 PathBuf::from(self.inner.to_os_string())
2530 }
2531
2532 #[stable(feature = "rust1", since = "1.0.0")]
2551 #[must_use]
2552 #[allow(deprecated)]
2553 pub fn is_absolute(&self) -> bool {
2554 sys::path::is_absolute(self)
2555 }
2556
2557 #[stable(feature = "rust1", since = "1.0.0")]
2571 #[must_use]
2572 #[inline]
2573 pub fn is_relative(&self) -> bool {
2574 !self.is_absolute()
2575 }
2576
2577 pub(crate) fn prefix(&self) -> Option<Prefix<'_>> {
2578 self.components().prefix
2579 }
2580
2581 #[stable(feature = "rust1", since = "1.0.0")]
2598 #[must_use]
2599 #[inline]
2600 pub fn has_root(&self) -> bool {
2601 self.components().has_root()
2602 }
2603
2604 #[stable(feature = "rust1", since = "1.0.0")]
2633 #[doc(alias = "dirname")]
2634 #[must_use]
2635 pub fn parent(&self) -> Option<&Path> {
2636 let mut comps = self.components();
2637 let comp = comps.next_back();
2638 comp.and_then(|p| match p {
2639 Component::Normal(_) | Component::CurDir | Component::ParentDir => {
2640 Some(comps.as_path())
2641 }
2642 _ => None,
2643 })
2644 }
2645
2646 #[stable(feature = "path_ancestors", since = "1.28.0")]
2674 #[inline]
2675 pub fn ancestors(&self) -> Ancestors<'_> {
2676 Ancestors { next: Some(&self) }
2677 }
2678
2679 #[stable(feature = "rust1", since = "1.0.0")]
2700 #[doc(alias = "basename")]
2701 #[must_use]
2702 pub fn file_name(&self) -> Option<&OsStr> {
2703 self.components().next_back().and_then(|p| match p {
2704 Component::Normal(p) => Some(p),
2705 _ => None,
2706 })
2707 }
2708
2709 #[stable(since = "1.7.0", feature = "path_strip_prefix")]
2739 pub fn strip_prefix<P>(&self, base: P) -> Result<&Path, StripPrefixError>
2740 where
2741 P: AsRef<Path>,
2742 {
2743 self._strip_prefix(base.as_ref())
2744 }
2745
2746 #[must_use = "this returns the remaining path as a new path, without modifying the original"]
2773 #[unstable(feature = "trim_prefix_suffix", issue = "142312")]
2774 pub fn trim_prefix<P>(&self, base: P) -> &Path
2775 where
2776 P: AsRef<Path>,
2777 {
2778 self._strip_prefix(base.as_ref()).unwrap_or(self)
2779 }
2780
2781 fn _strip_prefix(&self, base: &Path) -> Result<&Path, StripPrefixError> {
2782 iter_after(self.components(), base.components())
2783 .map(|c| c.as_path())
2784 .ok_or(StripPrefixError(()))
2785 }
2786
2787 #[stable(feature = "rust1", since = "1.0.0")]
2810 #[must_use]
2811 pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool {
2812 self._starts_with(base.as_ref())
2813 }
2814
2815 fn _starts_with(&self, base: &Path) -> bool {
2816 iter_after(self.components(), base.components()).is_some()
2817 }
2818
2819 #[stable(feature = "rust1", since = "1.0.0")]
2838 #[must_use]
2839 pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {
2840 self._ends_with(child.as_ref())
2841 }
2842
2843 fn _ends_with(&self, child: &Path) -> bool {
2844 iter_after(self.components().rev(), child.components().rev()).is_some()
2845 }
2846
2847 #[stable(feature = "path_is_empty", since = "1.98.0")]
2868 pub fn is_empty(&self) -> bool {
2869 self.as_os_str().is_empty()
2870 }
2871
2872 #[stable(feature = "rust1", since = "1.0.0")]
2899 #[must_use]
2900 pub fn file_stem(&self) -> Option<&OsStr> {
2901 self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.or(after))
2902 }
2903
2904 #[stable(feature = "path_file_prefix", since = "1.91.0")]
2934 #[must_use]
2935 pub fn file_prefix(&self) -> Option<&OsStr> {
2936 self.file_name().map(split_file_at_dot).and_then(|(before, _after)| Some(before))
2937 }
2938
2939 #[stable(feature = "rust1", since = "1.0.0")]
2959 #[must_use]
2960 pub fn extension(&self) -> Option<&OsStr> {
2961 self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.and(after))
2962 }
2963
2964 #[unstable(feature = "path_trailing_sep", issue = "142503")]
2983 #[must_use]
2984 #[inline]
2985 pub fn has_trailing_sep(&self) -> bool {
2986 self.as_os_str().as_encoded_bytes().last().copied().is_some_and(is_sep_byte)
2987 }
2988
2989 #[unstable(feature = "path_trailing_sep", issue = "142503")]
3007 #[must_use]
3008 #[inline]
3009 pub fn with_trailing_sep(&self) -> Cow<'_, Path> {
3010 if self.has_trailing_sep() { Cow::Borrowed(self) } else { Cow::Owned(self.join("")) }
3011 }
3012
3013 #[unstable(feature = "path_trailing_sep", issue = "142503")]
3034 #[must_use]
3035 #[inline]
3036 pub fn trim_trailing_sep(&self) -> &Path {
3037 if self.has_trailing_sep() && (!self.has_root() || self.parent().is_some()) {
3038 let mut bytes = self.inner.as_encoded_bytes();
3039 while let Some((last, init)) = bytes.split_last()
3040 && is_sep_byte(*last)
3041 {
3042 bytes = init;
3043 }
3044
3045 Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(bytes) })
3047 } else {
3048 self
3049 }
3050 }
3051
3052 #[stable(feature = "rust1", since = "1.0.0")]
3076 #[must_use]
3077 pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
3078 self._join(path.as_ref())
3079 }
3080
3081 fn _join(&self, path: &Path) -> PathBuf {
3082 let mut buf = self.to_path_buf();
3083 buf.push(path);
3084 buf
3085 }
3086
3087 #[stable(feature = "rust1", since = "1.0.0")]
3104 #[must_use]
3105 pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf {
3106 self._with_file_name(file_name.as_ref())
3107 }
3108
3109 fn _with_file_name(&self, file_name: &OsStr) -> PathBuf {
3110 let mut buf = self.to_path_buf();
3111 buf.set_file_name(file_name);
3112 buf
3113 }
3114
3115 #[stable(feature = "rust1", since = "1.0.0")]
3148 pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
3149 self._with_extension(extension.as_ref())
3150 }
3151
3152 fn _with_extension(&self, extension: &OsStr) -> PathBuf {
3153 let self_len = self.as_os_str().len();
3154 let self_bytes = self.as_os_str().as_encoded_bytes();
3155
3156 let (new_capacity, slice_to_copy) = match self.extension() {
3157 None => {
3158 let capacity = self_len + extension.len() + 1;
3160 let whole_path = self_bytes;
3161 (capacity, whole_path)
3162 }
3163 Some(previous_extension) => {
3164 let capacity = self_len + extension.len() - previous_extension.len();
3165 let path_till_dot = &self_bytes[..self_len - previous_extension.len()];
3166 (capacity, path_till_dot)
3167 }
3168 };
3169
3170 let mut new_path = PathBuf::with_capacity(new_capacity);
3171 unsafe { new_path.inner.extend_from_slice_unchecked(slice_to_copy) };
3173 new_path.set_extension(extension);
3174 new_path
3175 }
3176
3177 #[stable(feature = "path_add_extension", since = "1.91.0")]
3204 pub fn with_added_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
3205 let mut new_path = self.to_path_buf();
3206 new_path.add_extension(extension);
3207 new_path
3208 }
3209
3210 #[stable(feature = "rust1", since = "1.0.0")]
3244 pub fn components(&self) -> Components<'_> {
3245 let prefix = parse_prefix(self.as_os_str());
3246 Components {
3247 path: self.as_u8_slice(),
3248 prefix,
3249 has_physical_root: has_physical_root(self.as_u8_slice(), prefix),
3250 front: const { if HAS_PREFIXES { State::Prefix } else { State::StartDir } },
3253 back: State::Body,
3254 }
3255 }
3256
3257 #[stable(feature = "rust1", since = "1.0.0")]
3278 #[inline]
3279 pub fn iter(&self) -> Iter<'_> {
3280 Iter { inner: self.components() }
3281 }
3282
3283 #[stable(feature = "rust1", since = "1.0.0")]
3301 #[must_use = "this does not display the path, \
3302 it returns an object that can be displayed"]
3303 #[inline]
3304 pub fn display(&self) -> Display<'_> {
3305 Display { inner: self.inner.display() }
3306 }
3307
3308 #[inline]
3314 #[unstable(feature = "str_as_str", issue = "130366")]
3315 pub const fn as_path(&self) -> &Path {
3316 self
3317 }
3318
3319 #[stable(feature = "path_ext", since = "1.5.0")]
3336 #[inline]
3337 pub fn metadata(&self) -> io::Result<fs::Metadata> {
3338 fs::metadata(self)
3339 }
3340
3341 #[stable(feature = "path_ext", since = "1.5.0")]
3355 #[inline]
3356 pub fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
3357 fs::symlink_metadata(self)
3358 }
3359
3360 #[stable(feature = "path_ext", since = "1.5.0")]
3382 #[inline]
3383 pub fn canonicalize(&self) -> io::Result<PathBuf> {
3384 fs::canonicalize(self)
3385 }
3386
3387 #[unstable(feature = "path_absolute_method", issue = "153328")]
3410 #[inline]
3411 pub fn absolute(&self) -> io::Result<PathBuf> {
3412 absolute(self)
3413 }
3414
3415 #[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::panic("internal error: entered unreachable code")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}