refactor(compress): move advanced one-shot order to Rust

ZSTD_compress_advanced_internal() still encoded the one-shot begin-then-end
sequence in C after public advanced parameter validation had moved behind Rust.
That left the public compression boundary split between Rust policy and a C
orchestration body, and made the begin-error ordering implicit in the C path.

Add an explicit C-layout state with opaque begin and end callbacks. Rust now
owns the ordering, forwards the source size as the pledged size, propagates a
begin error before invoking the end callback, and returns the end result. The
callbacks retain the private ZSTD_CCtx and ZSTD_CCtx_params layouts, so the
change moves policy and sequencing without exposing window, workspace,
matchfinder, or context internals to Rust. The existing advanced and
using-dictionary wrappers continue to use the same C private operations.

Test Plan:
- `git diff --cached --check` -- passed.
- `rustfmt --check --edition 2021 rust/src/zstd_compress.rs` -- passed.
- Not run: Cargo, make, native tests, builds, or heavy verification per request.
This commit is contained in:
2026-07-21 09:13:50 +02:00
parent cb770812fd
commit 19c3414095
2 changed files with 264 additions and 4 deletions
+58 -4
View File
@@ -678,6 +678,32 @@ typedef char ZSTD_rust_compress_advanced_state_layout[
== 2 * sizeof(void*)
&& sizeof(ZSTD_rust_compressAdvancedState) == 3 * sizeof(void*))
? 1 : -1];
typedef size_t (*ZSTD_rust_compressAdvancedInternalBegin_f)(
void* context, const void* dict, size_t dictSize,
const void* params, U64 pledgedSrcSize);
typedef size_t (*ZSTD_rust_compressAdvancedInternalEnd_f)(
void* context, void* dst, size_t dstCapacity,
const void* src, size_t srcSize);
typedef struct {
void* callbackContext;
ZSTD_rust_compressAdvancedInternalBegin_f begin;
ZSTD_rust_compressAdvancedInternalEnd_f end;
} ZSTD_rust_compressAdvancedInternalState;
size_t ZSTD_rust_compressAdvancedInternal(
const ZSTD_rust_compressAdvancedInternalState* state,
void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
const void* dict, size_t dictSize,
const void* params);
typedef char ZSTD_rust_compress_advanced_internal_state_layout[
(offsetof(ZSTD_rust_compressAdvancedInternalState, callbackContext) == 0
&& offsetof(ZSTD_rust_compressAdvancedInternalState, begin)
== sizeof(void*)
&& offsetof(ZSTD_rust_compressAdvancedInternalState, end)
== 2 * sizeof(void*)
&& sizeof(ZSTD_rust_compressAdvancedInternalState)
== 3 * sizeof(void*))
? 1 : -1];
typedef void (*ZSTD_rust_compressUsingDictInitParams_f)(
void* context, const ZSTD_parameters* params, int compressionLevel);
typedef struct {
@@ -6950,6 +6976,12 @@ static size_t ZSTD_rust_compressAdvanced_compressInternal(
void* context, void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
const void* dict, size_t dictSize);
static size_t ZSTD_rust_compressAdvancedInternal_begin(
void* context, const void* dict, size_t dictSize,
const void* params, U64 pledgedSrcSize);
static size_t ZSTD_rust_compressAdvancedInternal_end(
void* context, void* dst, size_t dstCapacity,
const void* src, size_t srcSize);
size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx,
void* dst, size_t dstCapacity,
@@ -6974,11 +7006,33 @@ size_t ZSTD_compress_advanced_internal(
const void* dict,size_t dictSize,
const ZSTD_CCtx_params* params)
{
ZSTD_rust_compressAdvancedInternalState state;
DEBUGLOG(4, "ZSTD_compress_advanced_internal (srcSize:%u)", (unsigned)srcSize);
FORWARD_IF_ERROR( ZSTD_compressBegin_internal(cctx,
dict, dictSize, ZSTD_dct_auto, ZSTD_dtlm_fast, NULL,
params, srcSize, ZSTDb_not_buffered) , "");
return ZSTD_compressEnd_public(cctx, dst, dstCapacity, src, srcSize);
state.callbackContext = cctx;
state.begin = ZSTD_rust_compressAdvancedInternal_begin;
state.end = ZSTD_rust_compressAdvancedInternal_end;
return ZSTD_rust_compressAdvancedInternal(
&state, dst, dstCapacity, src, srcSize,
dict, dictSize, params);
}
static size_t ZSTD_rust_compressAdvancedInternal_begin(
void* context, const void* dict, size_t dictSize,
const void* params, U64 pledgedSrcSize)
{
return ZSTD_compressBegin_internal(
(ZSTD_CCtx*)context, dict, dictSize,
ZSTD_dct_auto, ZSTD_dtlm_fast, NULL,
(const ZSTD_CCtx_params*)params,
pledgedSrcSize, ZSTDb_not_buffered);
}
static size_t ZSTD_rust_compressAdvancedInternal_end(
void* context, void* dst, size_t dstCapacity,
const void* src, size_t srcSize)
{
return ZSTD_compressEnd_public(
(ZSTD_CCtx*)context, dst, dstCapacity, src, srcSize);
}
static void ZSTD_rust_compressAdvanced_initParams(