Merge pull request #22 from TheBlueMatt/main
[ldk-c-bindings] / lightning-c-bindings / src / c_types / mod.rs
index b28bc80fd264f8ea232723adabc07614a1525b6c..6267b80bc2dd007ba9476f76d01127d2b9a74937 100644 (file)
@@ -382,11 +382,27 @@ impl Into<Str> for &'static str {
                Str { chars: self.as_ptr(), len: self.len(), chars_is_owned: false }
        }
 }
-impl Into<&'static str> for Str {
-       fn into(self) -> &'static str {
+impl Str {
+       pub(crate) fn into_str(&self) -> &'static str {
                if self.len == 0 { return ""; }
                std::str::from_utf8(unsafe { std::slice::from_raw_parts(self.chars, self.len) }).unwrap()
        }
+       pub(crate) fn into_string(mut self) -> String {
+               let bytes = if self.len == 0 {
+                       Vec::new()
+               } else if self.chars_is_owned {
+                       let ret = unsafe {
+                               Box::from_raw(std::slice::from_raw_parts_mut(unsafe { self.chars as *mut u8 }, self.len))
+                       }.into();
+                       self.chars_is_owned = false;
+                       ret
+               } else {
+                       let mut ret = Vec::with_capacity(self.len);
+                       ret.extend_from_slice(unsafe { std::slice::from_raw_parts(self.chars, self.len) });
+                       ret
+               };
+               String::from_utf8(bytes).unwrap()
+       }
 }
 impl Into<Str> for String {
        fn into(self) -> Str {