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