From 35616c895f3a8b820ac2003a5e0cf4a0d78e3141 Mon Sep 17 00:00:00 2001 From: polazarus Date: Sat, 22 Jun 2024 22:47:10 +0200 Subject: [PATCH] feat: add into cow conversion --- src/bytes/convert.rs | 23 +++++++++++++++++++++++ src/os_string/convert.rs | 23 +++++++++++++++++++++++ src/path/convert.rs | 23 +++++++++++++++++++++++ src/string/convert.rs | 23 +++++++++++++++++++++++ 4 files changed, 92 insertions(+) diff --git a/src/bytes/convert.rs b/src/bytes/convert.rs index 49043c8..eab1a7e 100644 --- a/src/bytes/convert.rs +++ b/src/bytes/convert.rs @@ -84,6 +84,18 @@ where } } +impl<'borrow, B> From> for Cow<'borrow, [u8]> +where + B: Backend, +{ + #[inline] + fn from(value: HipByt<'borrow, B>) -> Self { + value + .into_borrowed() + .map_or_else(|value| Cow::Owned(value.into()), Cow::Borrowed) + } +} + // Fallible conversions // => none for now @@ -150,4 +162,15 @@ mod tests { let v: Vec<_> = a.into(); assert_eq!(v.as_slice(), &arr); } + + #[test] + fn test_into_cow() { + let h = HipByt::from_static(b"abc"); + let c: Cow<'static, [u8]> = h.into(); + assert_eq!(c, Cow::Borrowed(b"abc")); + + let h = HipByt::from(b"abc"); + let c: Cow<'static, [u8]> = h.into(); + assert_eq!(c, Cow::<'static, [u8]>::Owned(b"abc".into())); + } } diff --git a/src/os_string/convert.rs b/src/os_string/convert.rs index 952bd34..bcb674c 100644 --- a/src/os_string/convert.rs +++ b/src/os_string/convert.rs @@ -165,6 +165,18 @@ where } } +impl<'borrow, B> From> for Cow<'borrow, OsStr> +where + B: Backend, +{ + #[inline] + fn from(value: HipOsStr<'borrow, B>) -> Self { + value + .into_borrowed() + .map_or_else(|value| Cow::Owned(value.into()), Cow::Borrowed) + } +} + // Fallible conversions //? @@ -258,6 +270,17 @@ mod tests { assert_eq!(v, "abc"); } + #[test] + fn test_into_cow() { + let h = HipOsStr::from_static("abc"); + let c: Cow<'static, OsStr> = h.into(); + assert_eq!(c, Cow::Borrowed("abc")); + + let h = HipOsStr::from("abc"); + let c: Cow<'static, OsStr> = h.into(); + assert_eq!(c, Cow::<'static, OsStr>::Owned("abc".into())); + } + #[test] fn test_into_hipbyt() { let v = "a".repeat(42); // string's length > inline capacity diff --git a/src/path/convert.rs b/src/path/convert.rs index ca80a54..74a4e00 100644 --- a/src/path/convert.rs +++ b/src/path/convert.rs @@ -254,6 +254,18 @@ where } } +impl<'borrow, B> From> for Cow<'borrow, Path> +where + B: Backend, +{ + #[inline] + fn from(value: HipPath<'borrow, B>) -> Self { + value + .into_borrowed() + .map_or_else(|value| Cow::Owned(value.into()), Cow::Borrowed) + } +} + // Fallible conversions //? @@ -401,6 +413,17 @@ mod tests { assert_eq!(v, Path::new("abc")); } + #[test] + fn test_into_cow() { + let h = HipPath::from_static("abc"); + let c: Cow<'static, Path> = h.into(); + assert_eq!(c, Cow::Borrowed(Path::new("abc"))); + + let h = HipPath::from("abc"); + let c: Cow<'static, Path> = h.into(); + assert_eq!(c, Cow::<'static, Path>::Owned("abc".into())); + } + #[test] fn test_into_os_string() { let v = "a".repeat(42); // string's length > inline capacity diff --git a/src/string/convert.rs b/src/string/convert.rs index 59da180..e1c6e7e 100644 --- a/src/string/convert.rs +++ b/src/string/convert.rs @@ -142,6 +142,18 @@ where } } +impl<'borrow, B> From> for Cow<'borrow, str> +where + B: Backend, +{ + #[inline] + fn from(value: HipStr<'borrow, B>) -> Self { + value + .into_borrowed() + .map_or_else(|value| Cow::Owned(value.into()), Cow::Borrowed) + } +} + // Fallible conversions impl<'borrow, B> TryFrom> for HipStr<'borrow, B> @@ -270,6 +282,17 @@ mod tests { assert_eq!(v.as_str(), "abc"); } + #[test] + fn test_into_cow() { + let h = HipStr::from_static("abc"); + let c: Cow<'static, str> = h.into(); + assert_eq!(c, Cow::Borrowed("abc")); + + let h = HipStr::from("abc"); + let c: Cow<'static, str> = h.into(); + assert_eq!(c, Cow::<'static, str>::Owned("abc".into())); + } + #[test] #[cfg(feature = "std")] fn into_os_string() {