From a6f13837e18f7180864494480c4085844b44263f Mon Sep 17 00:00:00 2001 From: rowan Date: Wed, 9 Jul 2025 13:47:37 -0400 Subject: [PATCH] impl for borrowed osstr --- crates/osstr_traits/src/impls.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/crates/osstr_traits/src/impls.rs b/crates/osstr_traits/src/impls.rs index a861a09..f8ab5d3 100644 --- a/crates/osstr_traits/src/impls.rs +++ b/crates/osstr_traits/src/impls.rs @@ -1,13 +1,7 @@ use crate::{OsDisplay, OsStringFormatter}; -macro_rules! impl_os_from_str { +macro_rules! impl_borrowed { ($type:ty) => { - impl $crate::OsDisplay for $type { - fn fmt_os(&self, f: &mut OsStringFormatter) -> std::fmt::Result { - f.write_str(&self.to_string()) - } - } - impl $crate::OsDisplay for &$type { fn fmt_os(&self, f: &mut OsStringFormatter) -> std::fmt::Result { (*self).fmt_os(f) @@ -21,6 +15,17 @@ macro_rules! impl_os_from_str { } }; } +macro_rules! impl_os_from_str { + ($type:ty) => { + impl $crate::OsDisplay for $type { + fn fmt_os(&self, f: &mut OsStringFormatter) -> std::fmt::Result { + f.write_str(&self.to_string()) + } + } + + impl_borrowed!($type); + }; +} impl_os_from_str!(bool); @@ -49,20 +54,28 @@ impl OsDisplay for std::ffi::OsString { } } -impl OsDisplay for &std::ffi::OsStr { +impl_borrowed!(std::ffi::OsString); + +impl OsDisplay for std::ffi::OsStr { fn fmt_os(&self, f: &mut OsStringFormatter) -> std::fmt::Result { f.write_os_str(self) } } +impl_borrowed!(std::ffi::OsStr); + impl OsDisplay for std::path::Path { fn fmt_os(&self, f: &mut OsStringFormatter) -> std::fmt::Result { f.write_os_str(&self.as_os_str()) } } +impl_borrowed!(std::path::Path); + impl OsDisplay for std::path::PathBuf { fn fmt_os(&self, f: &mut OsStringFormatter) -> std::fmt::Result { f.write_os_str(&self.as_os_str()) } } + +impl_borrowed!(std::path::PathBuf);