mirror of
https://github.com/roc-lang/roc.git
synced 2025-07-24 15:03:46 +00:00
Support converting Lowercase to Box<str>
This commit is contained in:
parent
e1359c3025
commit
0bff2c6674
2 changed files with 38 additions and 0 deletions
|
@ -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 {
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue