pth/
try_into_path.rs

1/// Define a private namespace for all its items.
2mod private
3{
4  #[ allow( unused_imports, clippy ::wildcard_imports ) ]
5  #[ allow( clippy ::std_instead_of_alloc, clippy ::std_instead_of_core ) ]
6  use crate :: *;
7
8  #[ cfg( not( feature = "no_std" ) ) ]
9  use std ::
10  {
11  io,
12  path :: { Component, Path, PathBuf },
13  string ::String,
14 };
15  
16  #[ cfg( feature = "no_std" ) ]
17  extern crate std;
18  
19  #[ cfg( feature = "no_std" ) ]
20  use std ::
21  {
22  io,
23  path :: { Component, Path, PathBuf },
24 };
25  
26  #[ cfg( feature = "no_std" ) ]
27  use alloc ::string ::String;
28
29  /// A trait for converting various types into a `PathBuf`.
30  ///
31  /// This trait is used to convert any path-like type into an owned `PathBuf`.
32  /// Unlike `TryIntoCowPath`, it always returns an owned `PathBuf`, so there is no need to differentiate between borrowed and owned paths at runtime.
33  /// Unlike `AsPath`, it is implemented for a wider range of path-like types, similar to `TryIntoCowPath`.
34  pub trait TryIntoPath
35  {
36  /// Converts the implementing type into a `PathBuf`.
37  ///
38  /// # Returns
39  ///
40  /// * `Ok(PathBuf)` - The owned path buffer.
41  /// * `Err(io ::Error)` - An error if the conversion fails.
42  /// # Errors
43  /// qqq: doc
44  fn try_into_path( self ) -> Result< PathBuf, io ::Error >;
45 }
46
47  /// Implementation of `TryIntoPath` for `&str`.
48  impl TryIntoPath for &str
49  {
50  fn try_into_path( self ) -> Result< PathBuf, io ::Error >
51  {
52   Ok( PathBuf ::from( self ) )
53 }
54 }
55
56  /// Implementation of `TryIntoPath` for `String`.
57  impl TryIntoPath for String
58  {
59  fn try_into_path( self ) -> Result< PathBuf, io ::Error >
60  {
61   Ok( PathBuf ::from( self ) )
62 }
63 }
64
65  /// Implementation of `TryIntoPath` for a reference to `Path`.
66  impl TryIntoPath for &Path
67  {
68  fn try_into_path( self ) -> Result< PathBuf, io ::Error >
69  {
70   Ok( self.to_path_buf() )
71 }
72 }
73
74  /// Implementation of `TryIntoPath` for `PathBuf`.
75  impl TryIntoPath for PathBuf
76  {
77  fn try_into_path( self ) -> Result< PathBuf, io ::Error >
78  {
79   Ok( self )
80 }
81 }
82
83  /// Implementation of `TryIntoPath` for a reference to `Utf8Path`.
84  #[ cfg( feature = "path_utf8" ) ]
85  impl TryIntoPath for &Utf8Path
86  {
87  fn try_into_path( self ) -> Result< PathBuf, io ::Error >
88  {
89   Ok( self.as_std_path().to_path_buf() )
90 }
91 }
92
93  /// Implementation of `TryIntoPath` for `Utf8PathBuf`.
94  #[ cfg( feature = "path_utf8" ) ]
95  impl TryIntoPath for Utf8PathBuf
96  {
97  fn try_into_path( self ) -> Result< PathBuf, io ::Error >
98  {
99   Ok( self.as_std_path().to_path_buf() )
100 }
101 }
102
103  /// Implementation of `TryIntoPath` for `std ::path ::Component`.
104  impl TryIntoPath for Component< '_ >
105  {
106  fn try_into_path( self ) -> Result< PathBuf, io ::Error >
107  {
108   Ok( self.as_os_str().into() )
109 }
110 }
111
112  /// Blanket implementation of `TryIntoPath` for references to types implementing `AsRef< Path >`.
113  impl< T > TryIntoPath for &T
114  where
115  T: AsRef< Path >,
116  {
117  fn try_into_path( self ) -> Result< PathBuf, io ::Error >
118  {
119   Ok( self.as_ref().to_path_buf() )
120 }
121 }
122}
123
124crate ::mod_interface!
125{
126  orphan use TryIntoPath;
127}