Support converting Lowercase to Box<str>

This commit is contained in:
Richard Feldman 2022-08-09 15:36:38 -04:00 committed by Ayaz Hafiz
parent e1359c3025
commit 0bff2c6674
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
2 changed files with 38 additions and 0 deletions

View file

@ -203,6 +203,30 @@ impl From<&str> for IdentStr {
}
}
impl From<IdentStr> for String {
fn from(ident_str: IdentStr) -> Self {
if ident_str.is_small_str() {
// Copy it to a heap allocation
ident_str.as_str().to_string()
} else {
// Reuse the existing heap allocation
let string = unsafe {
String::from_raw_parts(
ident_str.as_ptr() as *mut u8,
ident_str.len(),
ident_str.len(),
)
};
// Make sure not to drop the IdentStr, since now there's
// a String referencing its heap-allocated contents.
std::mem::forget(ident_str);
string
}
}
}
impl From<String> for IdentStr {
fn from(string: String) -> Self {
if string.len() <= Self::SMALL_STR_BYTES {

View file

@ -193,6 +193,20 @@ impl Lowercase {
}
}
impl From<Lowercase> for String {
fn from(lowercase: Lowercase) -> Self {
lowercase.0.into()
}
}
impl From<Lowercase> for Box<str> {
fn from(lowercase: Lowercase) -> Self {
let string: String = lowercase.0.into();
string.into()
}
}
impl<'a> From<&'a str> for Lowercase {
fn from(string: &'a str) -> Self {
Self(string.into())