Skip to content

Commit 16c6cc3

Browse files
committed
Wrap from_raw_part
Signed-off-by: Vasilii Demidenok <[email protected]>
1 parent a389096 commit 16c6cc3

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/merge_operator.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ pub unsafe extern "C" fn full_merge_callback(
5151
) -> *const c_char {
5252
let cb: &mut MergeOperatorCallback = &mut *(raw_cb as *mut MergeOperatorCallback);
5353
let operands = &mut MergeOperands::new(operands_list, operands_list_len, num_operands);
54-
let key: &[u8] = slice::from_raw_parts(raw_key as *const u8, key_len);
55-
let oldval: &[u8] = slice::from_raw_parts(existing_value as *const u8, existing_value_len);
54+
let key: &[u8] = from_raw_parts(raw_key as *const u8, key_len);
55+
let oldval: &[u8] = from_raw_parts(existing_value as *const u8, existing_value_len);
5656
let mut result = (cb.merge_fn)(key, Some(oldval), operands);
5757
result.shrink_to_fit();
5858
// TODO(tan) investigate zero-copy techniques to improve performance
@@ -77,7 +77,7 @@ pub unsafe extern "C" fn partial_merge_callback(
7777
) -> *const c_char {
7878
let cb: &mut MergeOperatorCallback = &mut *(raw_cb as *mut MergeOperatorCallback);
7979
let operands = &mut MergeOperands::new(operands_list, operands_list_len, num_operands);
80-
let key: &[u8] = slice::from_raw_parts(raw_key as *const u8, key_len);
80+
let key: &[u8] = from_raw_parts(raw_key as *const u8, key_len);
8181
let mut result = (cb.merge_fn)(key, None, operands);
8282
result.shrink_to_fit();
8383
// TODO(tan) investigate zero-copy techniques to improve performance
@@ -90,6 +90,14 @@ pub unsafe extern "C" fn partial_merge_callback(
9090
buf as *const c_char
9191
}
9292

93+
unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
94+
if data.is_null() || len == 0 {
95+
&[]
96+
} else {
97+
slice::from_raw_parts(data, len)
98+
}
99+
}
100+
93101
pub struct MergeOperands {
94102
operands_list: *const *const c_char,
95103
operands_list_len: *const size_t,

0 commit comments

Comments
 (0)