Merge pull request #19 from TheBlueMatt/2021-04-invoice-incl
[ldk-c-bindings] / lightning-c-bindings / src / c_types / mod.rs
index b28bc80fd264f8ea232723adabc07614a1525b6c..3d45802e8063850cc2f71983e47daeefce11a304 100644 (file)
@@ -382,11 +382,25 @@ 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(self) -> String {
+               let bytes = if self.len == 0 {
+                       Vec::new()
+               } else if self.chars_is_owned {
+                       unsafe {
+                               Box::from_raw(std::slice::from_raw_parts_mut(unsafe { self.chars as *mut u8 }, self.len))
+                       }.into()
+               } 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 {