Skip to content

Commit

Permalink
Update lib.h and lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-congo committed Mar 11, 2024
1 parent 697a94f commit d00c559
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 12 additions & 2 deletions go/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef enum RequestErrorType {
*
* It contains either a connection or an error. It is represented as a struct instead of an enum for ease of use in the wrapper language.
*
* This struct should be freed using both `free_connection_response` and `free_error` to avoid memory leaks.
* This struct should be freed using `free_connection_response` to avoid memory leaks.
*/
typedef struct ConnectionResponse {
const void *conn_ptr;
Expand Down Expand Up @@ -69,7 +69,7 @@ void close_client(const void *client_ptr);
/**
* Deallocates a `ConnectionResponse`.
*
* This function does not free the contained error, which needs to be freed separately using `free_error` afterwards to avoid memory leaks.
* This function also frees the contained error using `free_error`.
*
* # Safety
*
Expand All @@ -78,3 +78,13 @@ void close_client(const void *client_ptr);
* * The contained `error_message` must be able to be safely casted to a valid `CString` via `CString::from_raw`. See the safety documentation of [`std::ffi::CString::from_raw`](https://doc.rust-lang.org/std/ffi/struct.CString.html#method.from_raw).
*/
void free_connection_response(const struct ConnectionResponse *connection_response_ptr);

/**
* Deallocates an error message `CString`.
*
* # Safety
*
* * `error_msg_ptr` must be able to be safely casted to a valid `CString` via `CString::from_raw`. See the safety documentation of [`std::ffi::CString::from_raw`](https://doc.rust-lang.org/std/ffi/struct.CString.html#method.from_raw).
* * `error_msg_ptr` must not be null.
*/
void free_error(const char *error_msg_ptr);
7 changes: 4 additions & 3 deletions go/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub type FailureCallback = unsafe extern "C" fn(
///
/// It contains either a connection or an error. It is represented as a struct instead of an enum for ease of use in the wrapper language.
///
/// This struct should be freed using both `free_connection_response` and `free_error` to avoid memory leaks.
/// This struct should be freed using `free_connection_response` to avoid memory leaks.
#[repr(C)]
pub struct ConnectionResponse {
conn_ptr: *const c_void,
Expand Down Expand Up @@ -136,7 +136,7 @@ pub unsafe extern "C" fn close_client(client_ptr: *const c_void) {

/// Deallocates a `ConnectionResponse`.
///
/// This function does not free the contained error, which needs to be freed separately using `free_error` afterwards to avoid memory leaks.
/// This function also frees the contained error using `free_error`.
///
/// # Safety
///
Expand All @@ -162,7 +162,8 @@ pub unsafe extern "C" fn free_connection_response(
///
/// * `error_msg_ptr` must be able to be safely casted to a valid `CString` via `CString::from_raw`. See the safety documentation of [`std::ffi::CString::from_raw`](https://doc.rust-lang.org/std/ffi/struct.CString.html#method.from_raw).
/// * `error_msg_ptr` must not be null.
unsafe fn free_error(error_msg_ptr: *const c_char) {
#[no_mangle]
pub unsafe extern "C" fn free_error(error_msg_ptr: *const c_char) {
let error_msg = unsafe { CString::from_raw(error_msg_ptr as *mut c_char) };
drop(error_msg);
}

0 comments on commit d00c559

Please sign in to comment.