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=cd96dffe1969bbe6c845fcfa0ff2ec73e64dff4a;hp=2248ec2befbfec64e08fd43484cbac004c3e02e5;hb=5d83aff2dd572685d7f76406422167e12c836b0e;hpb=09c8d62d937e86eee350898c7187dad498f0ec81 diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index 2248ec2..cd96dff 100644 --- a/lightning-c-bindings/src/c_types/mod.rs +++ b/lightning-c-bindings/src/c_types/mod.rs @@ -338,18 +338,20 @@ pub(crate) fn deserialize_obj_arg>(s } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Clone)] /// A Rust str object, ie a reference to a UTF8-valid string. /// This is *not* null-terminated so cannot be used directly as a C string! pub struct Str { /// A pointer to the string's bytes, in UTF8 encoding pub chars: *const u8, /// The number of bytes (not characters!) pointed to by `chars` - pub len: usize + pub len: usize, + /// Whether the data pointed to by `chars` should be freed or not. + pub chars_is_owned: bool, } impl Into for &'static str { fn into(self) -> Str { - Str { chars: self.as_ptr(), len: self.len() } + Str { chars: self.as_ptr(), len: self.len(), chars_is_owned: false } } } impl Into<&'static str> for Str { @@ -358,6 +360,23 @@ impl Into<&'static str> for Str { std::str::from_utf8(unsafe { std::slice::from_raw_parts(self.chars, self.len) }).unwrap() } } +impl Into for String { + fn into(self) -> Str { + let s = Box::leak(self.into_boxed_str()); + Str { chars: s.as_ptr(), len: s.len(), chars_is_owned: true } + } +} + +impl Drop for Str { + fn drop(&mut self) { + if self.chars_is_owned && self.len != 0 { + let _ = derived::CVec_u8Z { data: self.chars as *mut u8, datalen: self.len }; + } + } +} +#[no_mangle] +/// Frees the data buffer, if chars_is_owned is set and len > 0. +pub extern "C" fn Str_free(_res: Str) { } // Note that the C++ headers memset(0) all the Templ types to avoid deallocation! // Thus, they must gracefully handle being completely null in _free.