Skip to content

Commit

Permalink
Wrap from_raw_part
Browse files Browse the repository at this point in the history
  • Loading branch information
define-null committed Sep 20, 2024
1 parent a389096 commit f40ff8e
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/merge_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ pub unsafe extern "C" fn full_merge_callback(
) -> *const c_char {
let cb: &mut MergeOperatorCallback = &mut *(raw_cb as *mut MergeOperatorCallback);
let operands = &mut MergeOperands::new(operands_list, operands_list_len, num_operands);
let key: &[u8] = slice::from_raw_parts(raw_key as *const u8, key_len);
let oldval: &[u8] = slice::from_raw_parts(existing_value as *const u8, existing_value_len);
let key: &[u8] = from_raw_parts(raw_key as *const u8, key_len);
let oldval: &[u8] = from_raw_parts(existing_value as *const u8, existing_value_len);
let mut result = (cb.merge_fn)(key, Some(oldval), operands);
result.shrink_to_fit();
// TODO(tan) investigate zero-copy techniques to improve performance
Expand All @@ -77,7 +77,7 @@ pub unsafe extern "C" fn partial_merge_callback(
) -> *const c_char {
let cb: &mut MergeOperatorCallback = &mut *(raw_cb as *mut MergeOperatorCallback);
let operands = &mut MergeOperands::new(operands_list, operands_list_len, num_operands);
let key: &[u8] = slice::from_raw_parts(raw_key as *const u8, key_len);
let key: &[u8] = from_raw_parts(raw_key as *const u8, key_len);
let mut result = (cb.merge_fn)(key, None, operands);
result.shrink_to_fit();
// TODO(tan) investigate zero-copy techniques to improve performance
Expand All @@ -90,6 +90,14 @@ pub unsafe extern "C" fn partial_merge_callback(
buf as *const c_char
}

unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
if data.is_null() || len == 0 {
&[]
} else {
slice::from_raw_parts(data, len)
}
}

pub struct MergeOperands {
operands_list: *const *const c_char,
operands_list_len: *const size_t,
Expand Down

0 comments on commit f40ff8e

Please sign in to comment.