From 07b21fdb75bde2cd8a67a7c816bf8192ace55ce3 Mon Sep 17 00:00:00 2001 From: "U-ALDEBERAN\\Nate" Date: Sun, 29 Sep 2024 13:58:59 -0400 Subject: [PATCH 1/4] Implemented [&]Cow for SharedString --- internal/core/string.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/internal/core/string.rs b/internal/core/string.rs index de8e584b020..852d3c1b988 100644 --- a/internal/core/string.rs +++ b/internal/core/string.rs @@ -12,6 +12,8 @@ use alloc::string::String; use core::fmt::{Debug, Display, Write}; use core::ops::Deref; +use std::borrow::Cow; + /// This macro is the same as [`std::format!`], but it returns a [`SharedString`] instead. /// /// ### Example @@ -212,6 +214,18 @@ impl From<&String> for SharedString { } } +impl From> for SharedString { + fn from(s: Cow<'_, str>) -> Self { + s.as_ref().into() + } +} + +impl From<&Cow<'_, str>> for SharedString { + fn from(s: &Cow<'_, str>) -> Self { + s.as_ref().into() + } +} + impl From for SharedString { fn from(c: char) -> Self { SharedString::from(c.encode_utf8(&mut [0; 6]) as &str) From 43200092c624065437096eaed5a9488ec8232f2d Mon Sep 17 00:00:00 2001 From: "U-ALDEBERAN\\Nate" Date: Sun, 29 Sep 2024 14:51:56 -0400 Subject: [PATCH 2/4] Added `#[cfg(feature = "std")]` --- internal/core/string.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/core/string.rs b/internal/core/string.rs index 852d3c1b988..dabc50464b1 100644 --- a/internal/core/string.rs +++ b/internal/core/string.rs @@ -12,6 +12,7 @@ use alloc::string::String; use core::fmt::{Debug, Display, Write}; use core::ops::Deref; +#[cfg(feature = "std")] use std::borrow::Cow; /// This macro is the same as [`std::format!`], but it returns a [`SharedString`] instead. @@ -214,12 +215,14 @@ impl From<&String> for SharedString { } } +#[cfg(feature = "std")] impl From> for SharedString { fn from(s: Cow<'_, str>) -> Self { s.as_ref().into() } } +#[cfg(feature = "std")] impl From<&Cow<'_, str>> for SharedString { fn from(s: &Cow<'_, str>) -> Self { s.as_ref().into() From 3c40aa2008918d10b35673e80a525d82a31d6d29 Mon Sep 17 00:00:00 2001 From: "U-ALDEBERAN\\Nate" Date: Tue, 1 Oct 2024 09:17:15 -0400 Subject: [PATCH 3/4] Added a unit test --- internal/core/string.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/core/string.rs b/internal/core/string.rs index dabc50464b1..39497a6b846 100644 --- a/internal/core/string.rs +++ b/internal/core/string.rs @@ -459,3 +459,14 @@ fn test_serialize_deserialize_sharedstring() { let deserialized: SharedString = serde_json::from_str(&serialized).unwrap(); assert_eq!(v, deserialized); } + +#[cfg(feature = "std")] +#[test] +fn test_from_cow() { + let borrowed = Cow::from("Foo"); + let owned = Cow::from("Bar".to_string()); + assert_eq!(SharedString::from("Foo"), SharedString::from(&borrowed)); + assert_eq!(SharedString::from("Foo"), SharedString::from(borrowed)); + assert_eq!(SharedString::from("Bar"), SharedString::from(&owned)); + assert_eq!(SharedString::from("Bar"), SharedString::from(owned)); +} \ No newline at end of file From 5d8eb68b2b9408d5941fb3fc0a829ad2ce302637 Mon Sep 17 00:00:00 2001 From: "U-ALDEBERAN\\Nate" Date: Tue, 1 Oct 2024 09:22:09 -0400 Subject: [PATCH 4/4] Importing different `Cow` and removing unnecessary `#[cfg(feature = "std")]` --- internal/core/string.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/internal/core/string.rs b/internal/core/string.rs index 39497a6b846..0b272a5fe13 100644 --- a/internal/core/string.rs +++ b/internal/core/string.rs @@ -9,12 +9,10 @@ use crate::SharedVector; #[cfg(not(feature = "std"))] use alloc::string::String; +use alloc::borrow::Cow; use core::fmt::{Debug, Display, Write}; use core::ops::Deref; -#[cfg(feature = "std")] -use std::borrow::Cow; - /// This macro is the same as [`std::format!`], but it returns a [`SharedString`] instead. /// /// ### Example @@ -215,14 +213,12 @@ impl From<&String> for SharedString { } } -#[cfg(feature = "std")] impl From> for SharedString { fn from(s: Cow<'_, str>) -> Self { s.as_ref().into() } } -#[cfg(feature = "std")] impl From<&Cow<'_, str>> for SharedString { fn from(s: &Cow<'_, str>) -> Self { s.as_ref().into() @@ -460,7 +456,6 @@ fn test_serialize_deserialize_sharedstring() { assert_eq!(v, deserialized); } -#[cfg(feature = "std")] #[test] fn test_from_cow() { let borrowed = Cow::from("Foo");