Skip to content

Commit 8db65bb

Browse files
committed
Add CompactCowStr::into_owned(self) -> String, to mimic Cow<str>.
1 parent 31e5acd commit 8db65bb

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/compact_cow_str.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ impl<'a> CompactCowStr<'a> {
9696
None
9797
}
9898
}
99+
100+
/// Convert into `String`, re-using the memory allocation it was already owned.
101+
#[inline]
102+
pub fn into_owned(self) -> String {
103+
unsafe {
104+
let raw = self.as_raw_str();
105+
let is_borrowed = self.is_borrowed();
106+
mem::forget(self);
107+
if is_borrowed {
108+
String::from(&*raw)
109+
} else {
110+
Box::from_raw(raw as *mut str).into_string()
111+
}
112+
}
113+
}
99114
}
100115

101116
impl<'a> Clone for CompactCowStr<'a> {

0 commit comments

Comments
 (0)