feat(compress): move C parameter overrides to Rust
The compression-parameter override helper previously lived in C and selectively copied each nonzero field from a caller-provided override structure. Move that pure field-wise operation into the Rust compression-parameter module, while keeping the existing repr(C) structure and pointer-based ABI at both C call sites. Zero-valued overrides continue to leave the current parameter untouched. Test Plan: - `cargo test --manifest-path rust/Cargo.toml --no-default-features --features compression` -- passed (252 tests) - `make -B -C lib -j2 lib` -- passed - `make -C tests -j2 fuzzer` and `tests/fuzzer -s4560 -t47 -i48 -v` -- passed - required clippy passes, nightly fmt, and `git diff --check` -- passed
This commit is contained in:
@@ -905,6 +905,44 @@ fn get_block_size(max_block_size: usize, window_log: u32) -> usize {
|
||||
max_block_size.min(1usize << window_log)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn override_cparams(
|
||||
cparams: &mut ZSTD_compressionParameters,
|
||||
overrides: &ZSTD_compressionParameters,
|
||||
) {
|
||||
if overrides.windowLog != 0 {
|
||||
cparams.windowLog = overrides.windowLog;
|
||||
}
|
||||
if overrides.hashLog != 0 {
|
||||
cparams.hashLog = overrides.hashLog;
|
||||
}
|
||||
if overrides.chainLog != 0 {
|
||||
cparams.chainLog = overrides.chainLog;
|
||||
}
|
||||
if overrides.searchLog != 0 {
|
||||
cparams.searchLog = overrides.searchLog;
|
||||
}
|
||||
if overrides.minMatch != 0 {
|
||||
cparams.minMatch = overrides.minMatch;
|
||||
}
|
||||
if overrides.targetLength != 0 {
|
||||
cparams.targetLength = overrides.targetLength;
|
||||
}
|
||||
if overrides.strategy != 0 {
|
||||
cparams.strategy = overrides.strategy;
|
||||
}
|
||||
}
|
||||
|
||||
/// Pure leaf for the private `ZSTD_overrideCParams()` helper.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ZSTD_rust_params_overrideCParams(
|
||||
cparams: *mut ZSTD_compressionParameters,
|
||||
overrides: *const ZSTD_compressionParameters,
|
||||
) {
|
||||
let (cparams, overrides) = unsafe { (&mut *cparams, &*overrides) };
|
||||
override_cparams(cparams, overrides);
|
||||
}
|
||||
|
||||
/// Pure leaf for the deprecated `ZSTD_getBlockSize()` path.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn ZSTD_rust_params_getBlockSize(maxBlockSize: usize, windowLog: u32) -> usize {
|
||||
@@ -1337,4 +1375,41 @@ mod tests {
|
||||
assert_eq!(ZSTD_rust_params_getBlockSize(128 * 1024, 16), 64 * 1024);
|
||||
assert_eq!(ZSTD_rust_params_getBlockSize(128 * 1024, 17), 128 * 1024);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cparam_overrides_replace_only_nonzero_fields() {
|
||||
let mut cparams = ZSTD_compressionParameters {
|
||||
windowLog: 10,
|
||||
chainLog: 11,
|
||||
hashLog: 12,
|
||||
searchLog: 13,
|
||||
minMatch: 14,
|
||||
targetLength: 15,
|
||||
strategy: 16,
|
||||
};
|
||||
let overrides = ZSTD_compressionParameters {
|
||||
windowLog: 20,
|
||||
chainLog: 0,
|
||||
hashLog: 22,
|
||||
searchLog: 0,
|
||||
minMatch: 24,
|
||||
targetLength: 0,
|
||||
strategy: 26,
|
||||
};
|
||||
|
||||
unsafe { ZSTD_rust_params_overrideCParams(&mut cparams, &overrides) };
|
||||
|
||||
assert_eq!(
|
||||
cparams,
|
||||
ZSTD_compressionParameters {
|
||||
windowLog: 20,
|
||||
chainLog: 11,
|
||||
hashLog: 22,
|
||||
searchLog: 13,
|
||||
minMatch: 24,
|
||||
targetLength: 15,
|
||||
strategy: 26,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user