Implementing From<Arc<str>> for SmolStr and From<SmolStr> for Arc<str>

Also adding one test to verify
This commit is contained in:
Scott Driggers 2023-08-25 09:39:14 -04:00
parent b04898eb32
commit ced7e87db1
2 changed files with 24 additions and 1 deletions

View File

@ -334,6 +334,13 @@ impl From<Box<str>> for SmolStr {
}
}
impl From<Arc<str>> for SmolStr {
#[inline]
fn from(s: Arc<str>) -> SmolStr {
SmolStr(Repr::Heap(s))
}
}
impl<'a> From<Cow<'a, str>> for SmolStr {
#[inline]
fn from(s: Cow<'a, str>) -> SmolStr {
@ -341,6 +348,16 @@ impl<'a> From<Cow<'a, str>> for SmolStr {
}
}
impl From<SmolStr> for Arc<str> {
#[inline(always)]
fn from(text: SmolStr) -> Self {
match text.0 {
Repr::Heap(data) => data,
_ => text.as_str().into(),
}
}
}
impl From<SmolStr> for String {
#[inline(always)]
fn from(text: SmolStr) -> Self {

View File

@ -1,3 +1,5 @@
use std::sync::Arc;
use proptest::{prop_assert, prop_assert_eq, proptest};
use smol_str::SmolStr;
@ -21,7 +23,11 @@ fn assert_traits() {
fn conversions() {
let s: SmolStr = "Hello, World!".into();
let s: String = s.into();
assert_eq!(s, "Hello, World!")
assert_eq!(s, "Hello, World!");
let s: SmolStr = Arc::<str>::from("Hello, World!").into();
let s: Arc<str> = s.into();
assert_eq!(s.as_ref(), "Hello, World!");
}
#[test]