proper_path_tools/
try_into_cow_path.rs

1/// Internal namespace.
2mod private
3{
4  use crate::*;
5  use std::
6  {
7    borrow::Cow,
8    io,
9    path::{ Component, Path, PathBuf },
10  };
11  // use camino::{ Utf8Path, Utf8PathBuf };
12
13  /// A trait for converting various types into a `Cow<Path>`.
14  ///
15  /// This trait is designed to avoid redundant memory allocation.
16  /// Unlike `TryIntoPath`, it does not allocate memory on the heap if it's not necessary.
17  /// Unlike `AsPath`, it is implemented for a wider number of path-like types, similar to `TryIntoPath`.
18  /// The drawback is the necessity to differentiate borrowed and owned paths at runtime.
19  pub trait TryIntoCowPath<'a>
20  {
21    /// Converts the implementing type into a `Cow<Path>`.
22    ///
23    /// # Returns
24    ///
25    /// * `Ok(Cow<Path>)` - A `Cow` that may be either borrowed or owned, depending on the input type.
26    /// * `Err(io::Error)` - An error if the conversion fails.
27    fn try_into_cow_path( self ) -> Result< Cow<'a, Path>, io::Error >;
28  }
29
30  /// Implementation of `TryIntoCowPath` for `String`.
31  impl<'a> TryIntoCowPath<'a> for &'a str
32  {
33    fn try_into_cow_path( self ) -> Result< Cow<'a, Path>, io::Error >
34    {
35      Ok( Cow::Borrowed( self.as_path() ) )
36    }
37  }
38
39  /// Implementation of `TryIntoCowPath` for `String`.
40  impl<'a> TryIntoCowPath<'a> for String
41  {
42    fn try_into_cow_path( self ) -> Result< Cow<'a, Path>, io::Error >
43    {
44      Ok( Cow::Owned( PathBuf::from( self ) ) )
45    }
46  }
47
48  /// Implementation of `TryIntoCowPath` for `PathBuf`.
49  impl<'a> TryIntoCowPath<'a> for PathBuf
50  {
51    fn try_into_cow_path( self ) -> Result< Cow<'a, Path>, io::Error >
52    {
53      Ok( Cow::Owned( self ) )
54    }
55  }
56
57  /// Implementation of `TryIntoCowPath` for a reference to `Path`.
58  impl<'a> TryIntoCowPath<'a> for &'a Path
59  {
60    fn try_into_cow_path( self ) -> Result< Cow<'a, Path>, io::Error >
61    {
62      Ok( Cow::Borrowed( self ) )
63    }
64  }
65
66  /// Implementation of `TryIntoCowPath` for a reference to `Utf8Path`.
67  #[cfg( feature = "path_utf8" )]
68  impl< 'a > TryIntoCowPath< 'a > for &'a Utf8Path
69  {
70    fn try_into_cow_path( self ) -> Result< Cow<'a, Path>, io::Error >
71    {
72      Ok( Cow::Borrowed( self.as_std_path() ) )
73    }
74  }
75
76  /// Implementation of `TryIntoCowPath` for `Utf8PathBuf`.
77  #[cfg( feature = "path_utf8" )]
78  impl<'a> TryIntoCowPath<'a> for Utf8PathBuf
79  {
80    fn try_into_cow_path( self ) -> Result< Cow<'a, Path>, io::Error >
81    {
82      Ok( Cow::Owned( self.as_std_path().to_path_buf() ) )
83    }
84  }
85
86  /// Implementation of `TryIntoCowPath` for `std::path::Component`.
87  impl<'a> TryIntoCowPath<'a> for Component<'a>
88  {
89    fn try_into_cow_path( self ) -> Result< Cow<'a, Path>, io::Error >
90    {
91      Ok( Cow::Owned( PathBuf::from( self.as_os_str() ) ) )
92    }
93  }
94
95  /// Blanket implementation of `TryIntoCowPath` for references to types implementing `AsPath`.
96  impl<'a, T> TryIntoCowPath< 'a > for &'a T
97  where
98    T : AsPath,
99  {
100    fn try_into_cow_path( self ) -> Result< Cow<'a, Path>, io::Error >
101    {
102      Ok( Cow::Borrowed( self.as_path() ) )
103    }
104  }
105
106}
107
108crate::mod_interface!
109{
110  orphan use TryIntoCowPath;
111}