Skip to content

Commit 394d839

Browse files
authored
Merge pull request #218 from mikayelgr/main-dev
Feature: HashMap traits for Rust
2 parents 9a32744 + 9fe25df commit 394d839

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

rust/lib.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ pub mod sz {
6868

6969
// Import the functions from the StringZilla C library.
7070
extern "C" {
71-
71+
fn sz_copy(target: *const c_void, source: *const c_void, length: usize);
72+
fn sz_fill(target: *const c_void, length: usize, value: u8);
73+
fn sz_move(target: *const c_void, source: *const c_void, length: usize);
7274
fn sz_dynamic_dispatch() -> i32;
7375
fn sz_version_major() -> i32;
7476
fn sz_version_minor() -> i32;
@@ -151,6 +153,13 @@ pub mod sz {
151153
result: *mut isize,
152154
) -> Status;
153155

156+
fn sz_hash_state_init(state: *const c_void, seed: u64);
157+
158+
fn sz_hash_state_stream(state: *const c_void, text: *const c_void, length: usize);
159+
160+
fn sz_hash_state_fold(state: *const c_void) -> u64;
161+
162+
fn sz_lookup(target: *const c_void, length: usize, source: *const c_void, lut: *const u8) -> *const c_void;
154163
}
155164

156165
/// A simple semantic version structure.
@@ -273,6 +282,57 @@ pub mod sz {
273282
return result;
274283
}
275284

285+
/// Moves the contents of `source` into `target`, overwriting the existing contents of `target`.
286+
/// This function is useful for scenarios where you need to replace the contents of a byte slice
287+
/// with the contents of another byte slice.
288+
pub fn move_bytes<T, S>(target: &mut T, source: &S)
289+
where
290+
T: AsMut<[u8]> + ?Sized,
291+
S: AsRef<[u8]> + ?Sized,
292+
{
293+
let target_slice = target.as_mut();
294+
let source_slice = source.as_ref();
295+
unsafe {
296+
sz_move(
297+
target_slice.as_mut_ptr() as *const c_void,
298+
source_slice.as_ptr() as *const c_void,
299+
source_slice.len(),
300+
);
301+
}
302+
}
303+
304+
/// Fills the contents of `target` with the specified `value`. This function is useful for
305+
/// scenarios where you need to set all bytes in a byte slice to a specific value, such as
306+
/// zeroing out a buffer or initializing a buffer with a specific byte pattern.
307+
pub fn fill<T>(target: &mut T, value: u8)
308+
where
309+
T: AsMut<[u8]> + ?Sized,
310+
{
311+
let target_slice = target.as_mut();
312+
unsafe {
313+
sz_fill(target_slice.as_ptr() as *const c_void, target_slice.len(), value);
314+
}
315+
}
316+
317+
/// Copies the contents of `source` into `target`, overwriting the existing contents of `target`.
318+
/// This function is useful for scenarios where you need to replace the contents of a byte slice
319+
/// with the contents of another byte slice.
320+
pub fn copy<T, S>(target: &mut T, source: &S)
321+
where
322+
T: AsMut<[u8]> + ?Sized,
323+
S: AsRef<[u8]> + ?Sized,
324+
{
325+
let target_slice = target.as_mut();
326+
let source_slice = source.as_ref();
327+
unsafe {
328+
sz_copy(
329+
target_slice.as_mut_ptr() as *mut c_void,
330+
source_slice.as_ptr() as *const c_void,
331+
source_slice.len(),
332+
);
333+
}
334+
}
335+
276336
/// Computes a 64-bit AES-based hash value for a given byte slice `text`.
277337
/// This function is designed to provide a high-quality hash value for use in
278338
/// hash tables, data structures, and cryptographic applications.

0 commit comments

Comments
 (0)