X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-c-bindings;a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Fc_types%2Fmod.rs;h=6267b80bc2dd007ba9476f76d01127d2b9a74937;hp=b28bc80fd264f8ea232723adabc07614a1525b6c;hb=d3d51441b4cd98389308d4cfaba6548f25598c4c;hpb=9069bc0ea8b7fdaa9ee276d78fb0610157edbcf2 diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index b28bc80..6267b80 100644 --- a/lightning-c-bindings/src/c_types/mod.rs +++ b/lightning-c-bindings/src/c_types/mod.rs @@ -382,11 +382,27 @@ impl Into 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 for String { fn into(self) -> Str {