rasi_syscall/path/path.rs
1use std::borrow::Cow;
2use std::cmp::Ordering;
3use std::ffi::{OsStr, OsString};
4use std::rc::Rc;
5use std::sync::Arc;
6
7use crate::path::{Ancestors, Components, Display, Iter, PathBuf, StripPrefixError};
8
9/// A slice of a path.
10///
11/// This struct is an async version of [`std::path::Path`].
12///
13/// This type supports a number of operations for inspecting a path, including
14/// breaking the path into its components (separated by `/` on Unix and by either
15/// `/` or `\` on Windows), extracting the file name, determining whether the path
16/// is absolute, and so on.
17///
18/// This is an *unsized* type, meaning that it must always be used behind a
19/// pointer like `&` or `Box`. For an owned version of this type,
20/// see [`PathBuf`].
21///
22/// [`PathBuf`]: struct.PathBuf.html
23/// [`std::path::Path`]: https://doc.rust-lang.org/std/path/struct.Path.html
24///
25/// More details about the overall approach can be found in
26/// the [module documentation](index.html).
27///
28/// # Examples
29///
30/// ```
31/// use std::path::Path;
32/// use std::ffi::OsStr;
33///
34/// // Note: this example does work on Windows
35/// let path = Path::new("./foo/bar.txt");
36///
37/// let parent = path.parent();
38/// assert_eq!(parent, Some(Path::new("./foo")));
39///
40/// let file_stem = path.file_stem();
41/// assert_eq!(file_stem, Some(OsStr::new("bar")));
42///
43/// let extension = path.extension();
44/// assert_eq!(extension, Some(OsStr::new("txt")));
45/// ```
46#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
47pub struct Path {
48 inner: std::path::Path,
49}
50
51impl Path {
52 /// Directly wraps a string slice as a `Path` slice.
53 ///
54 /// This is a cost-free conversion.
55 ///
56 /// # Examples
57 ///
58 /// ```
59 /// use rasi_syscall::path::Path;
60 ///
61 /// Path::new("foo.txt");
62 /// ```
63 ///
64 /// You can create `Path`s from `String`s, or even other `Path`s:
65 ///
66 /// ```
67 /// use rasi_syscall::path::Path;
68 ///
69 /// let string = String::from("foo.txt");
70 /// let from_string = Path::new(&string);
71 /// let from_path = Path::new(&from_string);
72 /// assert_eq!(from_string, from_path);
73 /// ```
74 pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &Path {
75 unsafe { &*(std::path::Path::new(s) as *const std::path::Path as *const Path) }
76 }
77
78 /// Returns the underlying [`OsStr`] slice.
79 ///
80 /// [`OsStr`]: https://doc.rust-lang.org/std/ffi/struct.OsStr.html
81 ///
82 /// # Examples
83 ///
84 /// ```
85 /// use std::ffi::OsStr;
86 ///
87 /// use rasi_syscall::path::Path;
88 ///
89 /// let os_str = Path::new("foo.txt").as_os_str();
90 /// assert_eq!(os_str, OsStr::new("foo.txt"));
91 /// ```
92 pub fn as_os_str(&self) -> &OsStr {
93 self.inner.as_os_str()
94 }
95
96 /// Returns a [`&str`] slice if the `Path` is valid unicode.
97 ///
98 /// This conversion may entail doing a check for UTF-8 validity.
99 /// Note that validation is performed because non-UTF-8 strings are
100 /// perfectly valid for some OS.
101 ///
102 /// [`&str`]: https://doc.rust-lang.org/std/primitive.str.html
103 ///
104 /// # Examples
105 ///
106 /// ```
107 /// use rasi_syscall::path::Path;
108 ///
109 /// let path = Path::new("foo.txt");
110 /// assert_eq!(path.to_str(), Some("foo.txt"));
111 /// ```
112 pub fn to_str(&self) -> Option<&str> {
113 self.inner.to_str()
114 }
115
116 /// Converts a `Path` to a [`Cow<str>`].
117 ///
118 /// Any non-Unicode sequences are replaced with
119 /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD].
120 ///
121 /// [`Cow<str>`]: https://doc.rust-lang.org/std/borrow/enum.Cow.html
122 /// [U+FFFD]: https://doc.rust-lang.org/std/char/constant.REPLACEMENT_CHARACTER.html
123 ///
124 /// # Examples
125 ///
126 /// Calling `to_string_lossy` on a `Path` with valid unicode:
127 ///
128 /// ```
129 /// use rasi_syscall::path::Path;
130 ///
131 /// let path = Path::new("foo.txt");
132 /// assert_eq!(path.to_string_lossy(), "foo.txt");
133 /// ```
134 ///
135 /// Had `path` contained invalid unicode, the `to_string_lossy` call might
136 /// have returned `"fo�.txt"`.
137 pub fn to_string_lossy(&self) -> Cow<'_, str> {
138 self.inner.to_string_lossy()
139 }
140
141 /// Converts a `Path` to an owned [`PathBuf`].
142 ///
143 /// [`PathBuf`]: struct.PathBuf.html
144 ///
145 /// # Examples
146 ///
147 /// ```
148 /// use rasi_syscall::path::{Path, PathBuf};
149 ///
150 /// let path_buf = Path::new("foo.txt").to_path_buf();
151 /// assert_eq!(path_buf, PathBuf::from("foo.txt"));
152 /// ```
153 pub fn to_path_buf(&self) -> PathBuf {
154 PathBuf::from(self.inner.to_path_buf())
155 }
156
157 /// Returns `true` if the `Path` is absolute, i.e. if it is independent of
158 /// the current directory.
159 ///
160 /// * On Unix, a path is absolute if it starts with the root, so
161 /// `is_absolute` and [`has_root`] are equivalent.
162 ///
163 /// * On Windows, a path is absolute if it has a prefix and starts with the
164 /// root: `c:\windows` is absolute, while `c:temp` and `\temp` are not.
165 ///
166 /// [`has_root`]: #method.has_root
167 ///
168 /// # Examples
169 ///
170 /// ```
171 /// use rasi_syscall::path::Path;
172 ///
173 /// assert!(!Path::new("foo.txt").is_absolute());
174 /// ```
175 pub fn is_absolute(&self) -> bool {
176 self.inner.is_absolute()
177 }
178
179 /// Returns `true` if the `Path` is relative, i.e. not absolute.
180 ///
181 /// See [`is_absolute`]'s documentation for more details.
182 ///
183 /// [`is_absolute`]: #method.is_absolute
184 ///
185 /// # Examples
186 ///
187 /// ```
188 /// use rasi_syscall::path::Path;
189 ///
190 /// assert!(Path::new("foo.txt").is_relative());
191 /// ```
192 pub fn is_relative(&self) -> bool {
193 self.inner.is_relative()
194 }
195
196 /// Returns `true` if the `Path` has a root.
197 ///
198 /// * On Unix, a path has a root if it begins with `/`.
199 ///
200 /// * On Windows, a path has a root if it:
201 /// * has no prefix and begins with a separator, e.g. `\windows`
202 /// * has a prefix followed by a separator, e.g. `c:\windows` but not `c:windows`
203 /// * has any non-disk prefix, e.g. `\\server\share`
204 ///
205 /// # Examples
206 ///
207 /// ```
208 /// use rasi_syscall::path::Path;
209 ///
210 /// assert!(Path::new("/etc/passwd").has_root());
211 /// ```
212 pub fn has_root(&self) -> bool {
213 self.inner.has_root()
214 }
215
216 /// Returns the `Path` without its final component, if there is one.
217 ///
218 /// Returns [`None`] if the path terminates in a root or prefix.
219 ///
220 /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
221 ///
222 /// # Examples
223 ///
224 /// ```
225 /// use rasi_syscall::path::Path;
226 ///
227 /// let path = Path::new("/foo/bar");
228 /// let parent = path.parent().unwrap();
229 /// assert_eq!(parent, Path::new("/foo"));
230 ///
231 /// let grand_parent = parent.parent().unwrap();
232 /// assert_eq!(grand_parent, Path::new("/"));
233 /// assert_eq!(grand_parent.parent(), None);
234 /// ```
235 pub fn parent(&self) -> Option<&Path> {
236 self.inner.parent().map(|p| p.into())
237 }
238
239 /// Produces an iterator over `Path` and its ancestors.
240 ///
241 /// The iterator will yield the `Path` that is returned if the [`parent`] method is used zero
242 /// or more times. That means, the iterator will yield `&self`, `&self.parent().unwrap()`,
243 /// `&self.parent().unwrap().parent().unwrap()` and so on. If the [`parent`] method returns
244 /// [`None`], the iterator will do likewise. The iterator will always yield at least one value,
245 /// namely `&self`.
246 ///
247 /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html
248 /// [`parent`]: struct.Path.html#method.parent
249 ///
250 /// # Examples
251 ///
252 /// ```
253 /// use rasi_syscall::path::Path;
254 ///
255 /// let mut ancestors = Path::new("/foo/bar").ancestors();
256 /// assert_eq!(ancestors.next(), Some(Path::new("/foo/bar").into()));
257 /// assert_eq!(ancestors.next(), Some(Path::new("/foo").into()));
258 /// assert_eq!(ancestors.next(), Some(Path::new("/").into()));
259 /// assert_eq!(ancestors.next(), None);
260 /// ```
261 pub fn ancestors(&self) -> Ancestors<'_> {
262 Ancestors { next: Some(&self) }
263 }
264
265 /// Returns the final component of the `Path`, if there is one.
266 ///
267 /// If the path is a normal file, this is the file name. If it's the path of a directory, this
268 /// is the directory name.
269 ///
270 /// Returns [`None`] if the path terminates in `..`.
271 ///
272 /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
273 ///
274 /// # Examples
275 ///
276 /// ```
277 /// use std::ffi::OsStr;
278 ///
279 /// use rasi_syscall::path::Path;
280 ///
281 /// assert_eq!(Some(OsStr::new("bin")), Path::new("/usr/bin/").file_name());
282 /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("tmp/foo.txt").file_name());
283 /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.").file_name());
284 /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.//").file_name());
285 /// assert_eq!(None, Path::new("foo.txt/..").file_name());
286 /// assert_eq!(None, Path::new("/").file_name());
287 /// ```
288 pub fn file_name(&self) -> Option<&OsStr> {
289 self.inner.file_name()
290 }
291
292 /// Returns a path that becomes `self` when joined onto `base`.
293 ///
294 /// # Errors
295 ///
296 /// If `base` is not a prefix of `self` (i.e., [`starts_with`]
297 /// returns `false`), returns [`Err`].
298 ///
299 /// [`starts_with`]: #method.starts_with
300 /// [`Err`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Err
301 ///
302 /// # Examples
303 ///
304 /// ```
305 /// use rasi_syscall::path::{Path, PathBuf};
306 ///
307 /// let path = Path::new("/test/haha/foo.txt");
308 ///
309 /// assert_eq!(path.strip_prefix("/"), Ok(Path::new("test/haha/foo.txt")));
310 /// assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
311 /// assert_eq!(path.strip_prefix("/test/"), Ok(Path::new("haha/foo.txt")));
312 /// assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(Path::new("")));
313 /// assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Path::new("")));
314 /// assert_eq!(path.strip_prefix("test").is_ok(), false);
315 /// assert_eq!(path.strip_prefix("/haha").is_ok(), false);
316 ///
317 /// let prefix = PathBuf::from("/test/");
318 /// assert_eq!(path.strip_prefix(prefix), Ok(Path::new("haha/foo.txt")));
319 /// ```
320 pub fn strip_prefix<P>(&self, base: P) -> Result<&Path, StripPrefixError>
321 where
322 P: AsRef<Path>,
323 {
324 Ok(self.inner.strip_prefix(base.as_ref())?.into())
325 }
326
327 /// Determines whether `base` is a prefix of `self`.
328 ///
329 /// Only considers whole path components to match.
330 ///
331 /// # Examples
332 ///
333 /// ```
334 /// use rasi_syscall::path::Path;
335 ///
336 /// let path = Path::new("/etc/passwd");
337 ///
338 /// assert!(path.starts_with("/etc"));
339 /// assert!(path.starts_with("/etc/"));
340 /// assert!(path.starts_with("/etc/passwd"));
341 /// assert!(path.starts_with("/etc/passwd/"));
342 ///
343 /// assert!(!path.starts_with("/e"));
344 /// ```
345 pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool {
346 self.inner.starts_with(base.as_ref())
347 }
348
349 /// Determines whether `child` is a suffix of `self`.
350 ///
351 /// Only considers whole path components to match.
352 ///
353 /// # Examples
354 ///
355 /// ```
356 /// use rasi_syscall::path::Path;
357 ///
358 /// let path = Path::new("/etc/passwd");
359 ///
360 /// assert!(path.ends_with("passwd"));
361 /// ```
362 pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {
363 self.inner.ends_with(child.as_ref())
364 }
365
366 /// Extracts the stem (non-extension) portion of [`file_name`].
367 ///
368 /// [`file_name`]: struct.Path.html#method.file_name
369 ///
370 /// The stem is:
371 ///
372 /// * [`None`], if there is no file name
373 /// * The entire file name if there is no embedded `.`
374 /// * The entire file name if the file name begins with `.` and has no other `.`s within
375 /// * Otherwise, the portion of the file name before the final `.`
376 ///
377 /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
378 ///
379 /// # Examples
380 ///
381 /// ```
382 /// use rasi_syscall::path::Path;
383 ///
384 /// let path = Path::new("foo.rs");
385 ///
386 /// assert_eq!("foo", path.file_stem().unwrap());
387 /// ```
388 pub fn file_stem(&self) -> Option<&OsStr> {
389 self.inner.file_stem()
390 }
391
392 /// Extracts the extension of [`file_name`], if possible.
393 ///
394 /// The extension is:
395 ///
396 /// * [`None`], if there is no file name
397 /// * [`None`], if there is no embedded `.`
398 /// * [`None`], if the file name begins with `.` and has no other `.`s within
399 /// * Otherwise, the portion of the file name after the final `.`
400 ///
401 /// [`file_name`]: struct.Path.html#method.file_name
402 /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
403 ///
404 /// # Examples
405 ///
406 /// ```
407 /// use rasi_syscall::path::Path;
408 ///
409 /// let path = Path::new("foo.rs");
410 ///
411 /// assert_eq!("rs", path.extension().unwrap());
412 /// ```
413 pub fn extension(&self) -> Option<&OsStr> {
414 self.inner.extension()
415 }
416
417 /// Creates an owned [`PathBuf`] with `path` adjoined to `self`.
418 ///
419 /// See [`PathBuf::push`] for more details on what it means to adjoin a path.
420 ///
421 /// [`PathBuf`]: struct.PathBuf.html
422 /// [`PathBuf::push`]: struct.PathBuf.html#method.push
423 ///
424 /// # Examples
425 ///
426 /// ```
427 /// use rasi_syscall::path::{Path, PathBuf};
428 ///
429 /// assert_eq!(Path::new("/etc").join("passwd"), PathBuf::from("/etc/passwd"));
430 /// ```
431 pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
432 self.inner.join(path.as_ref()).into()
433 }
434
435 /// Creates an owned [`PathBuf`] like `self` but with the given file name.
436 ///
437 /// See [`PathBuf::set_file_name`] for more details.
438 ///
439 /// [`PathBuf`]: struct.PathBuf.html
440 /// [`PathBuf::set_file_name`]: struct.PathBuf.html#method.set_file_name
441 ///
442 /// # Examples
443 ///
444 /// ```
445 /// use rasi_syscall::path::{Path, PathBuf};
446 ///
447 /// let path = Path::new("/tmp/foo.txt");
448 /// assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt"));
449 ///
450 /// let path = Path::new("/tmp");
451 /// assert_eq!(path.with_file_name("var"), PathBuf::from("/var"));
452 /// ```
453 pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf {
454 self.inner.with_file_name(file_name).into()
455 }
456
457 /// Creates an owned [`PathBuf`] like `self` but with the given extension.
458 ///
459 /// See [`PathBuf::set_extension`] for more details.
460 ///
461 /// [`PathBuf`]: struct.PathBuf.html
462 /// [`PathBuf::set_extension`]: struct.PathBuf.html#method.set_extension
463 ///
464 /// # Examples
465 ///
466 /// ```
467 /// use rasi_syscall::path::{Path, PathBuf};
468 ///
469 /// let path = Path::new("foo.rs");
470 /// assert_eq!(path.with_extension("txt"), PathBuf::from("foo.txt"));
471 /// ```
472 pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
473 self.inner.with_extension(extension).into()
474 }
475
476 /// Produces an iterator over the [`Component`]s of the path.
477 ///
478 /// When parsing the path, there is a small amount of normalization:
479 ///
480 /// * Repeated separators are ignored, so `a/b` and `a//b` both have
481 /// `a` and `b` as components.
482 ///
483 /// * Occurrences of `.` are normalized away, except if they are at the
484 /// beginning of the path. For example, `a/./b`, `a/b/`, `a/b/.` and
485 /// `a/b` all have `a` and `b` as components, but `./a/b` starts with
486 /// an additional [`CurDir`] component.
487 ///
488 /// * A trailing slash is normalized away, `/a/b` and `/a/b/` are equivalent.
489 ///
490 /// Note that no other normalization takes place; in particular, `a/c`
491 /// and `a/b/../c` are distinct, to account for the possibility that `b`
492 /// is a symbolic link (so its parent isn't `a`).
493 ///
494 /// [`Component`]: enum.Component.html
495 /// [`CurDir`]: enum.Component.html#variant.CurDir
496 ///
497 /// # Examples
498 ///
499 /// ```
500 /// use std::ffi::OsStr;
501 ///
502 /// use rasi_syscall::path::{Path, Component};
503 ///
504 /// let mut components = Path::new("/tmp/foo.txt").components();
505 ///
506 /// assert_eq!(components.next(), Some(Component::RootDir));
507 /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("tmp"))));
508 /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("foo.txt"))));
509 /// assert_eq!(components.next(), None);
510 /// ```
511 pub fn components(&self) -> Components<'_> {
512 Components {
513 inner: self.inner.components(),
514 }
515 }
516
517 /// Produces an iterator over the path's components viewed as [`OsStr`]
518 /// slices.
519 ///
520 /// For more information about the particulars of how the path is separated
521 /// into components, see [`components`].
522 ///
523 /// [`components`]: #method.components
524 /// [`OsStr`]: https://doc.rust-lang.org/std/ffi/struct.OsStr.html
525 ///
526 /// # Examples
527 ///
528 /// ```
529 /// use std::ffi::OsStr;
530 ///
531 /// use rasi_syscall::path::{self, Path};
532 ///
533 /// let mut it = Path::new("/tmp/foo.txt").iter();
534 /// assert_eq!(it.next(), Some(OsStr::new(&path::MAIN_SEPARATOR.to_string())));
535 /// assert_eq!(it.next(), Some(OsStr::new("tmp")));
536 /// assert_eq!(it.next(), Some(OsStr::new("foo.txt")));
537 /// assert_eq!(it.next(), None)
538 /// ```
539 pub fn iter(&self) -> Iter<'_> {
540 Iter {
541 inner: self.components(),
542 }
543 }
544
545 /// Returns an object that implements [`Display`] for safely printing paths
546 /// that may contain non-Unicode data.
547 ///
548 /// [`Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html
549 ///
550 /// # Examples
551 ///
552 /// ```
553 /// use rasi_syscall::path::Path;
554 ///
555 /// let path = Path::new("/tmp/foo.rs");
556 ///
557 /// println!("{}", path.display());
558 /// ```
559 pub fn display(&self) -> Display<'_> {
560 self.inner.display()
561 }
562
563 /// Converts a [`Box<Path>`][`Box`] into a [`PathBuf`] without copying or
564 /// allocating.
565 ///
566 /// [`Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html
567 /// [`PathBuf`]: struct.PathBuf.html
568 ///
569 /// # Examples
570 ///
571 /// ```
572 /// use rasi_syscall::path::Path;
573 ///
574 /// let path: Box<Path> = Path::new("foo.txt").into();
575 /// let path_buf = path.into_path_buf();
576 /// ```
577 pub fn into_path_buf(self: Box<Path>) -> PathBuf {
578 let rw = Box::into_raw(self) as *mut std::path::Path;
579 let inner = unsafe { Box::from_raw(rw) };
580 inner.into_path_buf().into()
581 }
582}
583
584impl From<&Path> for Box<Path> {
585 fn from(path: &Path) -> Box<Path> {
586 let boxed: Box<std::path::Path> = path.inner.into();
587 let rw = Box::into_raw(boxed) as *mut Path;
588 unsafe { Box::from_raw(rw) }
589 }
590}
591
592impl From<&Path> for Arc<Path> {
593 /// Converts a Path into a Rc by copying the Path data into a new Rc buffer.
594 #[inline]
595 fn from(s: &Path) -> Arc<Path> {
596 let arc: Arc<OsStr> = Arc::from(s.as_os_str());
597 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Path) }
598 }
599}
600
601impl From<&Path> for Rc<Path> {
602 #[inline]
603 fn from(s: &Path) -> Rc<Path> {
604 let rc: Rc<OsStr> = Rc::from(s.as_os_str());
605 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Path) }
606 }
607}
608
609impl ToOwned for Path {
610 type Owned = PathBuf;
611
612 fn to_owned(&self) -> PathBuf {
613 self.to_path_buf()
614 }
615}
616
617impl AsRef<OsStr> for Path {
618 fn as_ref(&self) -> &OsStr {
619 self.inner.as_ref()
620 }
621}
622
623impl AsRef<Path> for Path {
624 fn as_ref(&self) -> &Path {
625 self
626 }
627}
628
629impl AsRef<Path> for OsStr {
630 fn as_ref(&self) -> &Path {
631 Path::new(self)
632 }
633}
634
635impl<'a> From<&'a Path> for Cow<'a, Path> {
636 #[inline]
637 fn from(s: &'a Path) -> Cow<'a, Path> {
638 Cow::Borrowed(s)
639 }
640}
641
642impl AsRef<Path> for Cow<'_, OsStr> {
643 fn as_ref(&self) -> &Path {
644 Path::new(self)
645 }
646}
647
648impl AsRef<Path> for OsString {
649 fn as_ref(&self) -> &Path {
650 Path::new(self)
651 }
652}
653
654impl AsRef<Path> for str {
655 fn as_ref(&self) -> &Path {
656 Path::new(self)
657 }
658}
659
660impl AsRef<Path> for String {
661 fn as_ref(&self) -> &Path {
662 Path::new(self)
663 }
664}
665
666impl AsRef<Path> for PathBuf {
667 fn as_ref(&self) -> &Path {
668 self
669 }
670}
671
672impl<'a> IntoIterator for &'a PathBuf {
673 type Item = &'a OsStr;
674 type IntoIter = Iter<'a>;
675
676 fn into_iter(self) -> Iter<'a> {
677 self.iter()
678 }
679}
680
681impl<'a> IntoIterator for &'a Path {
682 type Item = &'a OsStr;
683 type IntoIter = Iter<'a>;
684
685 fn into_iter(self) -> Iter<'a> {
686 self.iter()
687 }
688}
689
690macro_rules! impl_cmp {
691 ($lhs:ty, $rhs: ty) => {
692 impl<'a, 'b> PartialEq<$rhs> for $lhs {
693 #[inline]
694 fn eq(&self, other: &$rhs) -> bool {
695 <Path as PartialEq>::eq(self, other)
696 }
697 }
698
699 impl<'a, 'b> PartialEq<$lhs> for $rhs {
700 #[inline]
701 fn eq(&self, other: &$lhs) -> bool {
702 <Path as PartialEq>::eq(self, other)
703 }
704 }
705
706 impl<'a, 'b> PartialOrd<$rhs> for $lhs {
707 #[inline]
708 fn partial_cmp(&self, other: &$rhs) -> Option<Ordering> {
709 <Path as PartialOrd>::partial_cmp(self, other)
710 }
711 }
712
713 impl<'a, 'b> PartialOrd<$lhs> for $rhs {
714 #[inline]
715 fn partial_cmp(&self, other: &$lhs) -> Option<Ordering> {
716 <Path as PartialOrd>::partial_cmp(self, other)
717 }
718 }
719 };
720}
721
722impl_cmp!(PathBuf, Path);
723impl_cmp!(PathBuf, &'a Path);
724impl_cmp!(Cow<'a, Path>, Path);
725impl_cmp!(Cow<'a, Path>, &'b Path);
726impl_cmp!(Cow<'a, Path>, PathBuf);
727
728macro_rules! impl_cmp_os_str {
729 ($lhs:ty, $rhs: ty) => {
730 impl<'a, 'b> PartialEq<$rhs> for $lhs {
731 #[inline]
732 fn eq(&self, other: &$rhs) -> bool {
733 <Path as PartialEq>::eq(self, other.as_ref())
734 }
735 }
736
737 impl<'a, 'b> PartialEq<$lhs> for $rhs {
738 #[inline]
739 fn eq(&self, other: &$lhs) -> bool {
740 <Path as PartialEq>::eq(self.as_ref(), other)
741 }
742 }
743
744 impl<'a, 'b> PartialOrd<$rhs> for $lhs {
745 #[inline]
746 fn partial_cmp(&self, other: &$rhs) -> Option<Ordering> {
747 <Path as PartialOrd>::partial_cmp(self, other.as_ref())
748 }
749 }
750
751 impl<'a, 'b> PartialOrd<$lhs> for $rhs {
752 #[inline]
753 fn partial_cmp(&self, other: &$lhs) -> Option<Ordering> {
754 <Path as PartialOrd>::partial_cmp(self.as_ref(), other)
755 }
756 }
757 };
758}
759
760impl_cmp_os_str!(PathBuf, OsStr);
761impl_cmp_os_str!(PathBuf, &'a OsStr);
762impl_cmp_os_str!(PathBuf, Cow<'a, OsStr>);
763impl_cmp_os_str!(PathBuf, OsString);
764impl_cmp_os_str!(Path, OsStr);
765impl_cmp_os_str!(Path, &'a OsStr);
766impl_cmp_os_str!(Path, Cow<'a, OsStr>);
767impl_cmp_os_str!(Path, OsString);
768impl_cmp_os_str!(&'a Path, OsStr);
769impl_cmp_os_str!(&'a Path, Cow<'b, OsStr>);
770impl_cmp_os_str!(&'a Path, OsString);
771
772impl<'a> From<&'a std::path::Path> for &'a Path {
773 fn from(path: &'a std::path::Path) -> &'a Path {
774 &Path::new(path.as_os_str())
775 }
776}
777
778impl<'a> Into<&'a std::path::Path> for &'a Path {
779 fn into(self) -> &'a std::path::Path {
780 std::path::Path::new(&self.inner)
781 }
782}
783
784impl AsRef<std::path::Path> for Path {
785 fn as_ref(&self) -> &std::path::Path {
786 self.into()
787 }
788}
789
790impl AsRef<Path> for std::path::Path {
791 fn as_ref(&self) -> &Path {
792 self.into()
793 }
794}
795
796impl AsRef<Path> for std::path::PathBuf {
797 fn as_ref(&self) -> &Path {
798 let p: &std::path::Path = self.as_ref();
799 p.into()
800 }
801}