Free strings on discovery

This commit is contained in:
Daniel Sanchez Quiros 2023-02-13 17:50:44 +01:00
parent 48300c15a2
commit bb9e3bcf56

View File

@ -15,18 +15,20 @@ pub fn waku_dns_discovery(
server: Option<&Host>, server: Option<&Host>,
timeout: Option<Duration>, timeout: Option<Duration>,
) -> Result<Vec<Multiaddr>> { ) -> Result<Vec<Multiaddr>> {
let result = unsafe { let url = CString::new(url.to_string())
CStr::from_ptr(waku_sys::waku_dns_discovery( .expect("CString should build properly from a valid Url")
CString::new(url.to_string()) .into_raw();
.expect("CString should build properly from a valid Url") let server = CString::new(
.into_raw(), server
CString::new( .map(|host| host.to_string())
server .unwrap_or_else(|| "".to_string()),
.map(|host| host.to_string()) )
.unwrap_or_else(|| "".to_string()), .expect("CString should build properly from a String nameserver")
) .into_raw();
.expect("CString should build properly from a String nameserver") let result_ptr = unsafe {
.into_raw(), let res = waku_sys::waku_dns_discovery(
url,
server,
timeout timeout
.map(|timeout| { .map(|timeout| {
timeout timeout
@ -35,13 +37,20 @@ pub fn waku_dns_discovery(
.expect("Duration as milliseconds should fit in a i32") .expect("Duration as milliseconds should fit in a i32")
}) })
.unwrap_or(0), .unwrap_or(0),
)) );
} // Recover strings and drop them
.to_str() drop(CString::from_raw(url));
.expect("Response should always succeed to load to a &str"); drop(CString::from_raw(server));
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<Vec<Multiaddr>> = let response: JsonResponse<Vec<Multiaddr>> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(result_ptr);
}
response.into() response.into()
} }