From b2085ccc4ee3cdfbca595f73c2cc9bcfe69e2587 Mon Sep 17 00:00:00 2001 From: Mikayel Grigoryan Date: Sun, 2 Mar 2025 10:43:29 +0400 Subject: [PATCH 1/4] Improve: Exposed sz_move, sz_fill, and sz_copy for Rust --- rust/lib.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/rust/lib.rs b/rust/lib.rs index c8dd629f..26a86ae8 100644 --- a/rust/lib.rs +++ b/rust/lib.rs @@ -68,7 +68,9 @@ pub mod sz { // Import the functions from the StringZilla C library. extern "C" { - + fn sz_copy(target: *const c_void, source: *const c_void, length: usize); + fn sz_fill(target: *const c_void, length: usize, value: u8); + fn sz_move(target: *const c_void, source: *const c_void, length: usize); fn sz_dynamic_dispatch() -> i32; fn sz_version_major() -> i32; fn sz_version_minor() -> i32; @@ -273,6 +275,57 @@ pub mod sz { return result; } + /// Moves the contents of `source` into `target`, overwriting the existing contents of `target`. + /// This function is useful for scenarios where you need to replace the contents of a byte slice + /// with the contents of another byte slice. + pub fn move_bytes(target: &mut T, source: &S) + where + T: AsMut<[u8]> + ?Sized, + S: AsRef<[u8]> + ?Sized, + { + let target_slice = target.as_mut(); + let source_slice = source.as_ref(); + unsafe { + sz_move( + target_slice.as_mut_ptr() as *const c_void, + source_slice.as_ptr() as *const c_void, + source_slice.len(), + ); + } + } + + /// Fills the contents of `target` with the specified `value`. This function is useful for + /// scenarios where you need to set all bytes in a byte slice to a specific value, such as + /// zeroing out a buffer or initializing a buffer with a specific byte pattern. + pub fn fill(target: &mut T, value: u8) + where + T: AsMut<[u8]> + ?Sized, + { + let target_slice = target.as_mut(); + unsafe { + sz_fill(target_slice.as_ptr() as *const c_void, target_slice.len(), value); + } + } + + /// Copies the contents of `source` into `target`, overwriting the existing contents of `target`. + /// This function is useful for scenarios where you need to replace the contents of a byte slice + /// with the contents of another byte slice. + pub fn copy(target: &mut T, source: &S) + where + T: AsMut<[u8]> + ?Sized, + S: AsRef<[u8]> + ?Sized, + { + let target_slice = target.as_mut(); + let source_slice = source.as_ref(); + unsafe { + sz_copy( + target_slice.as_mut_ptr() as *mut c_void, + source_slice.as_ptr() as *const c_void, + source_slice.len(), + ); + } + } + /// Computes a 64-bit AES-based hash value for a given byte slice `text`. /// This function is designed to provide a high-quality hash value for use in /// hash tables, data structures, and cryptographic applications. From 1757e4ec50d9a177cd5f64d96337d51cbe28d4fc Mon Sep 17 00:00:00 2001 From: Mikayel Grigoryan Date: Sun, 2 Mar 2025 11:08:59 +0400 Subject: [PATCH 2/4] Improve: Expose sz_hash_state_init, sz_hash_state_stream, and sz_hash_state_fold to Rust --- rust/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rust/lib.rs b/rust/lib.rs index 26a86ae8..784c5454 100644 --- a/rust/lib.rs +++ b/rust/lib.rs @@ -153,6 +153,14 @@ pub mod sz { result: *mut isize, ) -> Status; + /// Initializes a hash state with a given seed value. + fn sz_hash_state_init(state: *const c_void, seed: u64); + + /// Updates the hash state with a new byte slice. + fn sz_hash_state_stream(state: *const c_void, text: *const c_void, length: usize); + + /// Finalizes the hash state and returns the computed hash value. + fn sz_hash_state_fold(state: *const c_void) -> u64; } /// A simple semantic version structure. From 471b0024db2b4c183f3277ca08f59a2af89eec24 Mon Sep 17 00:00:00 2001 From: Mikayel Grigoryan Date: Sun, 2 Mar 2025 11:14:19 +0400 Subject: [PATCH 3/4] Improve: Expose sz_lookup --- rust/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/lib.rs b/rust/lib.rs index 784c5454..14f10c7e 100644 --- a/rust/lib.rs +++ b/rust/lib.rs @@ -161,6 +161,8 @@ pub mod sz { /// Finalizes the hash state and returns the computed hash value. fn sz_hash_state_fold(state: *const c_void) -> u64; + + fn sz_lookup(target: *const c_void, length: usize, source: *const c_void, lut: *const u8) -> *const c_void; } /// A simple semantic version structure. From 9fe25df8abbe428fc7b2255dce19e96acd3d444f Mon Sep 17 00:00:00 2001 From: Mikayel Grigoryan Date: Sun, 2 Mar 2025 11:26:45 +0400 Subject: [PATCH 4/4] Improve: Remove redundant comments from sz_hash_state functions in Rust --- rust/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/rust/lib.rs b/rust/lib.rs index 14f10c7e..b3cbca4e 100644 --- a/rust/lib.rs +++ b/rust/lib.rs @@ -153,13 +153,10 @@ pub mod sz { result: *mut isize, ) -> Status; - /// Initializes a hash state with a given seed value. fn sz_hash_state_init(state: *const c_void, seed: u64); - /// Updates the hash state with a new byte slice. fn sz_hash_state_stream(state: *const c_void, text: *const c_void, length: usize); - /// Finalizes the hash state and returns the computed hash value. fn sz_hash_state_fold(state: *const c_void) -> u64; fn sz_lookup(target: *const c_void, length: usize, source: *const c_void, lut: *const u8) -> *const c_void;