Skip to content

Commit 60b953e

Browse files
committed
Add CompactCowStr::into_owned(self) -> String, to mimic Cow<str>.
1 parent 6d7bd9d commit 60b953e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/compact_cow_str.rs

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

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

0 commit comments

Comments
 (0)