From 16c6cc38b261fa3bd67f9a5b186e0ad0afcc5120 Mon Sep 17 00:00:00 2001 From: Vasilii Demidenok <879658+define-null@users.noreply.github.com> Date: Fri, 20 Sep 2024 10:45:40 +0200 Subject: [PATCH] Wrap from_raw_part Signed-off-by: Vasilii Demidenok <879658+define-null@users.noreply.github.com> --- src/merge_operator.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/merge_operator.rs b/src/merge_operator.rs index 6a3fe649a..de876170f 100644 --- a/src/merge_operator.rs +++ b/src/merge_operator.rs @@ -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 @@ -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 @@ -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,