Skip to content

Commit b48be5e

Browse files
committed
Rename to convert_string_pointer_array_to_vector and add documentation.
Signed-off-by: currantw <[email protected]>
1 parent 3e90678 commit b48be5e

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

rust/src/ffi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ pub(crate) unsafe fn create_route(
314314
/// * `data`, `data_len` and also each pointer stored in `data` must be able to be safely casted to a valid to a slice of the corresponding type via [`from_raw_parts`].
315315
/// See the safety documentation of [`from_raw_parts`].
316316
/// * The caller is responsible of freeing the allocated memory.
317-
pub(crate) unsafe fn convert_double_pointer_to_vec<'a>(
317+
pub(crate) unsafe fn convert_string_pointer_array_to_vector<'a>(
318318
data: *const *const u8,
319319
len: usize,
320320
data_len: *const usize,
@@ -527,11 +527,11 @@ pub struct BatchOptionsInfo {
527527
/// * `cmd_ptr` must be able to be safely casted to a valid [`CmdInfo`]
528528
/// * `args` and `args_len` in a referred [`CmdInfo`] structure must not be `null`.
529529
/// * `data` in a referred [`CmdInfo`] structure must point to `arg_count` consecutive string pointers.
530-
/// * `args_len` in a referred [`CmdInfo`] structure must point to `arg_count` consecutive string lengths. See the safety documentation of [`convert_double_pointer_to_vec`].
530+
/// * `args_len` in a referred [`CmdInfo`] structure must point to `arg_count` consecutive string lengths. See the safety documentation of [`convert_string_pointer_array_to_vector`].
531531
pub(crate) unsafe fn create_cmd(ptr: *const CmdInfo) -> Result<Cmd, String> {
532532
let info = unsafe { *ptr };
533533
let arg_vec =
534-
unsafe { convert_double_pointer_to_vec(info.args, info.arg_count, info.args_len) };
534+
unsafe { convert_string_pointer_array_to_vector(info.args, info.arg_count, info.args_len) };
535535

536536
let Some(mut cmd) = info.request_type.get_command() else {
537537
return Err("Couldn't fetch command type".into());

rust/src/lib.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,14 @@ pub unsafe extern "C" fn remove_cluster_scan_cursor(cursor_id: *const c_char) {
561561

562562
/// Build cluster scan arguments from C-style arrays.
563563
///
564+
/// # Arguments
565+
///
566+
/// * `arg_count` - The number of arguments in the arrays
567+
/// * `args` - Pointer to an array of pointers to argument data
568+
/// * `arg_lengths` - Pointer to an array of argument lengths
569+
/// * `failure_callback` - Callback function to invoke on error
570+
/// * `callback_index` - Index to pass to the callback function
571+
///
564572
/// # Safety
565573
/// * `args` and `arg_lengths` must be valid arrays of length `arg_count`
566574
/// * Each pointer in `args` must point to valid memory of the corresponding length
@@ -575,7 +583,7 @@ unsafe fn build_cluster_scan_args(
575583
return Some(redis::ClusterScanArgs::builder().build());
576584
}
577585

578-
let arg_vec = unsafe { convert_double_pointer_to_vec(args, arg_count, arg_lengths) };
586+
let arg_vec = unsafe { convert_string_pointer_array_to_vector(args, arg_count, arg_lengths) };
579587

580588
// Parse arguments from vector.
581589
let mut pattern_arg: &[u8] = &[];
@@ -704,24 +712,32 @@ unsafe fn build_cluster_scan_args(
704712
Some(cluster_scan_args_builder.build())
705713
}
706714

707-
/// Converts a double pointer to a vec.
715+
/// Converts an array of pointers to strings to a vector of strings.
716+
///
717+
/// # Arguments
718+
///
719+
/// * `data` - Pointer to an array of pointers to string data
720+
/// * `len` - The number of strings in the array
721+
/// * `data_len` - Pointer to an array of string lengths
708722
///
709723
/// # Safety
710724
///
711-
/// `convert_double_pointer_to_vec` returns a `Vec` of u8 slice which holds pointers of C
725+
/// `convert_string_pointer_array_to_vector` returns a `Vec` of u8 slice which holds pointers of C
712726
/// strings. The returned `Vec<&'a [u8]>` is meant to be copied into Rust code. Storing them
713727
/// for later use will cause the program to crash as the pointers will be freed by the caller.
714-
unsafe fn convert_double_pointer_to_vec<'a>(
728+
unsafe fn convert_string_pointer_array_to_vector<'a>(
715729
data: *const usize,
716730
len: u64,
717731
data_len: *const u64,
718732
) -> Vec<&'a [u8]> {
719733
let string_ptrs = unsafe { from_raw_parts(data, len as usize) };
720734
let string_lengths = unsafe { from_raw_parts(data_len, len as usize) };
735+
721736
let mut result = Vec::<&[u8]>::with_capacity(string_ptrs.len());
722737
for (i, &str_ptr) in string_ptrs.iter().enumerate() {
723738
let slice = unsafe { from_raw_parts(str_ptr as *const u8, string_lengths[i] as usize) };
724739
result.push(slice);
725740
}
741+
726742
result
727743
}

0 commit comments

Comments
 (0)