Free strings on filter

This commit is contained in:
Daniel Sanchez Quiros 2023-02-13 18:02:53 +01:00
parent bb9e3bcf56
commit 98da6f99a1
1 changed files with 41 additions and 29 deletions

View File

@ -16,29 +16,37 @@ pub fn waku_filter_subscribe(
peer_id: PeerId,
timeout: Duration,
) -> Result<()> {
let result = unsafe {
CStr::from_ptr(waku_sys::waku_filter_subscribe(
CString::new(
serde_json::to_string(filter_subscription)
.expect("FilterSubscription should always be able to be serialized"),
)
.expect("CString should build properly from the serialized filter subscription")
.into_raw(),
CString::new(peer_id)
.expect("CString should build properly from peer id")
.into_raw(),
let filter_subscription = CString::new(
serde_json::to_string(filter_subscription)
.expect("FilterSubscription should always succeed to serialize"),
)
.expect("FilterSubscription should always be able to be serialized");
let peer_id = CString::new(peer_id).expect("PeerId should always be able to be serialized");
let result_ptr = unsafe {
let filter_subscription_ptr = filter_subscription.into_raw();
let peer_id_ptr = peer_id.into_raw();
let result_ptr = waku_sys::waku_filter_subscribe(
filter_subscription_ptr,
peer_id_ptr,
timeout
.as_millis()
.try_into()
.expect("Duration as milliseconds should fit in a i32"),
))
}
.to_str()
.expect("Response should always succeed to load to a &str");
);
drop(CString::from_raw(filter_subscription_ptr));
drop(CString::from_raw(peer_id_ptr));
result_ptr
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(result_ptr);
}
Result::from(response).map(|_| ())
}
@ -48,25 +56,29 @@ pub fn waku_filter_unsubscribe(
filter_subscription: &FilterSubscription,
timeout: Duration,
) -> Result<()> {
let result = unsafe {
CStr::from_ptr(waku_sys::waku_filter_unsubscribe(
CString::new(
serde_json::to_string(filter_subscription)
.expect("FilterSubscription should always be able to be serialized"),
)
.expect("CString should build properly from the serialized filter subscription")
.into_raw(),
let filter_subscription = CString::new(
serde_json::to_string(filter_subscription)
.expect("FilterSubscription should always succeed to serialize"),
)
.expect("CString should build properly from the serialized filter subscription");
let result_ptr = unsafe {
let filter_subscription_ptr = filter_subscription.into_raw();
let res = waku_sys::waku_filter_unsubscribe(
filter_subscription_ptr,
timeout
.as_millis()
.try_into()
.expect("Duration as milliseconds should fit in a i32"),
))
}
.to_str()
.expect("Response should always succeed to load to a &str");
);
drop(CString::from_raw(filter_subscription_ptr));
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
Result::from(response).map(|_| ())
}