Unnamed repository; edit this file 'description' to name the repository.
Implement AsRef<str> in favor of generic From impls
Lukas Wirth 2023-03-31
parent fef9abc · commit 8f9fefd
-rw-r--r--lib/smol_str/src/lib.rs54
1 files changed, 49 insertions, 5 deletions
diff --git a/lib/smol_str/src/lib.rs b/lib/smol_str/src/lib.rs
index 775c5d8f22..a67a7fc096 100644
--- a/lib/smol_str/src/lib.rs
+++ b/lib/smol_str/src/lib.rs
@@ -2,6 +2,8 @@
extern crate alloc;
use alloc::{
+ borrow::Cow,
+ boxed::Box,
string::{String, ToString},
sync::Arc,
};
@@ -290,22 +292,64 @@ impl<'a> iter::FromIterator<&'a str> for SmolStr {
}
}
-impl<T> From<T> for SmolStr
-where
- T: AsRef<str>,
-{
- fn from(text: T) -> Self {
+impl AsRef<str> for SmolStr {
+ #[inline(always)]
+ fn as_ref(&self) -> &str {
+ self.as_str()
+ }
+}
+
+impl From<&str> for SmolStr {
+ #[inline]
+ fn from(s: &str) -> SmolStr {
+ SmolStr::new(s)
+ }
+}
+
+impl From<&mut str> for SmolStr {
+ #[inline]
+ fn from(s: &mut str) -> SmolStr {
+ SmolStr::new(s)
+ }
+}
+
+impl From<&String> for SmolStr {
+ #[inline]
+ fn from(s: &String) -> SmolStr {
+ SmolStr::new(s)
+ }
+}
+
+impl From<String> for SmolStr {
+ #[inline(always)]
+ fn from(text: String) -> Self {
Self::new(text)
}
}
+impl From<Box<str>> for SmolStr {
+ #[inline]
+ fn from(s: Box<str>) -> SmolStr {
+ SmolStr::new(s)
+ }
+}
+
+impl<'a> From<Cow<'a, str>> for SmolStr {
+ #[inline]
+ fn from(s: Cow<'a, str>) -> SmolStr {
+ SmolStr::new(s)
+ }
+}
+
impl From<SmolStr> for String {
+ #[inline(always)]
fn from(text: SmolStr) -> Self {
text.as_str().into()
}
}
impl Borrow<str> for SmolStr {
+ #[inline(always)]
fn borrow(&self) -> &str {
self.as_str()
}