structured_zstd/encoding/mod.rs
1//! Zstandard encoder — frame compression, streaming, dictionary support.
2//!
3//! Four entry points cover the common use cases:
4//!
5//! * [`compress`] — one-shot helper that builds a self-contained
6//! Zstandard frame from a `Read` source to a `Write` sink. The
7//! input is consumed incrementally from `Read`, so input buffering
8//! stays bounded; however, the compressed output is buffered in
9//! memory until the frame is complete so the Frame Content Size
10//! field can be filled in the header — peak memory is
11//! `O(compressed_size)` (worst-case `O(input_size)` for
12//! incompressible payloads, plus a small frame overhead). The
13//! savings vs [`compress_to_vec`] come from not materialising the
14//! input alongside the output.
15//! * [`compress_to_vec`] — same one-shot path as [`compress`] but
16//! the input is eagerly drained into an internal `Vec` first
17//! (`read_to_end`) so the encoder can be handed a `&[u8]` and a
18//! precise source-size hint. Peak memory is therefore ≈
19//! `input_size + output_size`; prefer [`compress`] or
20//! [`StreamingEncoder`] when the input is large or unbounded.
21//! * [`StreamingEncoder`] — implements [`crate::io::Write`], which
22//! re-exports [`std::io::Write`] under the `std` feature and falls
23//! back to a `no_std`-friendly trait otherwise. Accepts bytes
24//! incrementally and flushes compressed output as blocks fill.
25//! Requires `set_pledged_content_size` before the first write if
26//! the Frame Content Size field is to be populated.
27//! * [`FrameCompressor`] — lower-level builder that owns the matcher and
28//! the per-frame configuration; the streaming and one-shot helpers are
29//! thin wrappers over it. Reach for it when you need to swap in a custom
30//! [`Matcher`] implementation or share the matcher across frames.
31//!
32//! Compression intensity is selected via [`CompressionLevel`], which
33//! provides both named presets (`Fastest`, `Default`, `Better`, `Best`) and
34//! numeric levels (`from_level(n)`) that mirror C zstd's level numbering
35//! (negative for ultra-fast, `0` = default, `1..=22` for the standard
36//! range).
37//!
38//! All produced frames are valid RFC 8878 Zstandard streams and decode
39//! through both this crate's [`crate::decoding`] module and upstream C zstd.
40//!
41//! For memory budgeting, [`estimated_compression_workspace_bytes`] reports
42//! the approximate steady-state heap footprint of a one-shot compression at
43//! a given level (window + match-finder tables + block staging).
44
45pub(crate) mod block_header;
46pub(crate) mod blocks;
47pub(crate) mod cparams;
48pub(crate) mod dict_attach;
49pub(crate) mod fastpath;
50pub(crate) mod frame_header;
51pub(crate) mod incompressible;
52pub(crate) mod match_generator;
53pub(crate) mod util;
54
55// `#111` encoder architecture rewrite. `cost_model`, `opt`,
56// `strategy`, `dfast`, `row`, and `simple` host the relocated
57// cost-model types, the optimal-parser plain-data types, the
58// const-generic [`strategy::Strategy`] trait + per-level [`strategy::
59// StrategyTag`] dispatcher, and the Dfast / Row / Simple matchers
60// respectively. `match_table::helpers` hosts the shared match-finder
61// primitives. The rewrite plan is tracked in
62// <https://github.com/structured-world/structured-zstd/issues/111>;
63// per-phase boundaries are `perf/post-pr-110-baseline` (start),
64// `perf/post-pr-121-baseline` (post-Phase-2).
65pub(crate) mod bt;
66pub(crate) mod cost_model;
67pub(crate) mod dfast;
68pub(crate) mod hc;
69// LDM uses `twox_hash::XxHash64` (per-window XXH64 over the
70// `min_match_length` byte slice, upstream zstd `zstd_ldm.c:315`). The
71// `twox-hash` dependency is gated behind the `hash` feature so
72// `default-features = false` builds (no_std, embedded) don't pull
73// it in. `BtMatcher::ldm_producer` and the `cfg(feature = "hash")`
74// blocks inside `BtMatcher::prepare_ldm_candidates` /
75// `BtMatcher::reset` carry the same gate; the call site in
76// `match_generator.rs::start_matching_optimal` invokes
77// `prepare_ldm_candidates` unconditionally because the
78// gating is internal to the method body (under
79// `not(feature = "hash")` the method shrinks to the legacy
80// `ldm_sequences.clear()` stub).
81#[cfg(feature = "hash")]
82pub(crate) mod ldm;
83pub(crate) mod match_table;
84pub(crate) mod opt;
85pub(crate) mod row;
86pub(crate) mod simple;
87pub(crate) mod strategy;
88
89pub(crate) mod frame_compressor;
90#[cfg(feature = "lsm")]
91pub mod frame_emit_info;
92mod levels;
93pub(crate) mod parameters;
94#[cfg(feature = "bench_internals")]
95pub mod sequence_capture;
96mod streaming_encoder;
97pub use frame_compressor::{EncoderDictionary, FrameCompressor};
98#[cfg(feature = "lsm")]
99pub use frame_emit_info::{BlockType, FrameBlock, FrameEmitInfo};
100pub use levels::config::{
101 estimated_bt_strategy_extra_bytes, estimated_compression_workspace_bytes,
102};
103pub use match_generator::MatchGeneratorDriver;
104pub use parameters::{
105 Bounds, CParameter, CompressionParameters, CompressionParametersBuilder, ParameterError,
106 Strategy,
107};
108pub use streaming_encoder::StreamingEncoder;
109
110use crate::io::{Read, Write};
111use alloc::vec::Vec;
112
113/// Convenience function to compress some source into a target without reusing any resources of the compressor
114/// ```rust
115/// use structured_zstd::encoding::{compress, CompressionLevel};
116/// let data: &[u8] = &[0,0,0,0,0,0,0,0,0,0,0,0];
117/// let mut target = Vec::new();
118/// compress(data, &mut target, CompressionLevel::Fastest);
119/// ```
120pub fn compress<R: Read, W: Write>(source: R, target: W, level: CompressionLevel) {
121 let mut frame_enc = FrameCompressor::new(level);
122 frame_enc.set_source(source);
123 frame_enc.set_drain(target);
124 frame_enc.compress();
125}
126
127/// Convenience function to compress some source into a Vec without reusing any resources of the compressor.
128///
129/// This helper eagerly buffers the full input (`Read`) before compression so it
130/// can provide a source-size hint to the one-shot encoder path. Peak memory can
131/// therefore be roughly `input_size + output_size`. For very large payloads or
132/// tighter memory budgets, prefer streaming APIs such as [`StreamingEncoder`].
133///
134/// **This is NOT a streaming API.** The source is fully buffered
135/// into a `Vec<u8>` before any compression work begins, so peak input
136/// memory is bounded by `source.len()` (not "constant regardless of
137/// payload size" as a stream-shaped encoder would offer). If the
138/// source is large enough that holding it in memory is not acceptable,
139/// use [`StreamingEncoder`] which consumes chunks incrementally
140/// without the up-front Vec build.
141///
142/// This helper drives `read_to_end` to materialize the full source
143/// into a `Vec<u8>` before forwarding the slice to
144/// [`compress_slice_to_vec`]. For a `Read` whose size is unknown ahead
145/// of time, `read_to_end` grows that input `Vec` via power-of-two
146/// doubling: peak input allocation can be up to 2× the final source
147/// length transiently. The live working set on this entry point is
148/// roughly `input.capacity()` plus the block-accumulation buffer and
149/// per-block scratch carried by [`compress_slice_to_vec`], plus the
150/// exactly-sized output `Vec`. [`StreamingEncoder`] avoids the input
151/// materialization step entirely and is the right entry point when
152/// the source is large or unbounded.
153///
154/// ```rust
155/// use structured_zstd::encoding::{compress_to_vec, CompressionLevel};
156/// let data: &[u8] = &[0,0,0,0,0,0,0,0,0,0,0,0];
157/// let compressed = compress_to_vec(data, CompressionLevel::Fastest);
158/// ```
159pub fn compress_to_vec<R: Read>(source: R, level: CompressionLevel) -> Vec<u8> {
160 let mut source = source;
161 let mut input = Vec::new();
162 source.read_to_end(&mut input).unwrap();
163 compress_slice_to_vec(input.as_slice(), level)
164}
165
166/// Compress a contiguous byte slice into a fresh `Vec<u8>` without the
167/// input-buffering step that [`compress_to_vec`] performs to adapt a
168/// `Read` source.
169///
170/// One-shot wrapper over
171/// [`FrameCompressor::compress_independent_frame`]: the input is read by
172/// reference (the eligible Fast path scans it in place, no per-block
173/// history copy), and the returned `Vec` is allocated exactly once at the
174/// final frame size after compression. Peak transient memory is the
175/// block-accumulation buffer (grown via amortized doubling, ≈ 2× current
176/// compressed size at the last realloc) plus the exactly-sized output. The
177/// worst-case compressed-size bound is never pinned upfront, so a highly
178/// compressible 100 MiB input does not charge ~100 MiB of worst-case
179/// expansion against peak.
180///
181/// To compress many slices, construct one [`FrameCompressor`] and call
182/// [`compress_independent_frame_into`](FrameCompressor::compress_independent_frame_into)
183/// in a loop instead, which reuses the matcher tables, scratch, and output
184/// buffer across frames (this function allocates and primes from scratch
185/// each call).
186///
187/// # Panics
188///
189/// Panics on encoder error (matches the failure surface of
190/// [`compress_to_vec`], which this function backs). Out-of-memory during
191/// the output / per-block scratch allocations is handled by the global
192/// allocator's abort policy. The slice/Vec entry points mirror the upstream zstd
193/// `ZSTD_compress` shape (no error return on the bulk path).
194///
195/// ```rust
196/// use structured_zstd::encoding::{compress_slice_to_vec, CompressionLevel};
197/// let data: &[u8] = &[0,0,0,0,0,0,0,0,0,0,0,0];
198/// let compressed = compress_slice_to_vec(data, CompressionLevel::Fastest);
199/// ```
200pub fn compress_slice_to_vec(source: &[u8], level: CompressionLevel) -> Vec<u8> {
201 // Bare `FrameCompressor` resolves all three type params to their
202 // defaults (`&'static [u8]` reader, `Vec<u8>` drain, MatchGeneratorDriver);
203 // neither the reader nor the drain is used by the in-place
204 // `compress_independent_frame` path.
205 let mut enc: FrameCompressor = FrameCompressor::new(level);
206 enc.compress_independent_frame(source)
207}
208
209/// Worst-case compressed-frame size for an input of `src_size` bytes.
210///
211/// A destination buffer of this size is always large enough to hold the
212/// output of [`compress_slice_to_vec`] (or any single-frame compression) for
213/// an input of `src_size` bytes, so a caller sizing a fixed buffer once (the
214/// shape the C `ZSTD_compress` entry point needs) never has to grow it.
215///
216/// Mirrors the upstream `ZSTD_COMPRESSBOUND` formula exactly:
217/// `src_size + (src_size >> 8) + margin`, where `margin` is
218/// `(128 KiB - src_size) >> 11` for inputs below 128 KiB and `0` otherwise.
219/// The margin guarantees `bound(a) + bound(b) <= bound(a + b)` for blocks of
220/// at least 128 KiB, which keeps multi-frame concatenation sizing sound.
221///
222/// Saturates at [`usize::MAX`] if the formula would overflow on a
223/// pathologically large `src_size` — no allocation that large can exist, so
224/// the saturated value is the correct "cannot fit" sentinel rather than a
225/// masked wrap.
226///
227/// ```rust
228/// use structured_zstd::encoding::{compress_bound, compress_slice_to_vec, CompressionLevel};
229/// let data = [7u8; 4096];
230/// assert!(compress_slice_to_vec(&data, CompressionLevel::Default).len() <= compress_bound(data.len()));
231/// ```
232pub const fn compress_bound(src_size: usize) -> usize {
233 const LOWER: usize = 128 * 1024;
234 let margin = if src_size < LOWER {
235 (LOWER - src_size) >> 11
236 } else {
237 0
238 };
239 // Saturating is the correct UPPER-BOUND semantic here, not a masked bug:
240 // this is a public API over an arbitrary `usize`, and the largest meaningful
241 // bound is `usize::MAX`. A real slice is at most `isize::MAX` bytes, so the
242 // `* 1.004 + margin` cannot overflow for genuine inputs; the saturation only
243 // caps a pathological caller-supplied size at the representable ceiling.
244 src_size
245 .saturating_add(src_size >> 8)
246 .saturating_add(margin)
247}
248
249/// Compress a byte slice into a fresh `Vec<u8>` using fine-grained
250/// [`CompressionParameters`] (#27) instead of a bare
251/// [`CompressionLevel`].
252///
253/// One-shot wrapper over [`FrameCompressor::set_parameters`] +
254/// [`FrameCompressor::compress_independent_frame`]. The produced frame is
255/// a valid RFC 8878 stream regardless of the knobs chosen.
256///
257/// ```rust
258/// use structured_zstd::encoding::{
259/// compress_with_parameters, CompressionLevel, CompressionParameters, Strategy,
260/// };
261/// let data: &[u8] = b"the quick brown fox jumps over the lazy dog";
262/// let params = CompressionParameters::builder(CompressionLevel::Level(5))
263/// .strategy(Strategy::Greedy)
264/// .build()
265/// .unwrap();
266/// let compressed = compress_with_parameters(data, ¶ms);
267/// assert!(!compressed.is_empty());
268/// ```
269pub fn compress_with_parameters(source: &[u8], params: &CompressionParameters) -> Vec<u8> {
270 let mut enc: FrameCompressor = FrameCompressor::new(params.level());
271 enc.set_parameters(params);
272 enc.compress_independent_frame(source)
273}
274
275/// The compression mode used impacts the speed of compression,
276/// and resulting compression ratios. Faster compression will result
277/// in worse compression ratios, and vice versa.
278#[derive(Copy, Clone, Debug, PartialEq, Eq)]
279pub enum CompressionLevel {
280 /// This level does not compress the data at all, and simply wraps
281 /// it in a Zstandard frame.
282 Uncompressed,
283 /// This level is roughly equivalent to Zstd compression level 1
284 Fastest,
285 /// This level uses the crate's dedicated `dfast`-style matcher to
286 /// target a better speed/ratio tradeoff than [`CompressionLevel::Fastest`].
287 ///
288 /// It represents this crate's "default" compression setting and may
289 /// evolve in future versions as the implementation moves closer to
290 /// reference zstd level 3 behavior.
291 Default,
292 /// This level is roughly equivalent to Zstd level 7.
293 ///
294 /// Uses the hash-chain matcher with a lazy2 matching strategy: the encoder
295 /// evaluates up to two positions ahead before committing to a match,
296 /// trading speed for a better compression ratio than [`CompressionLevel::Default`].
297 Better,
298 /// This level is roughly equivalent to Zstd level 11.
299 ///
300 /// Uses the hash-chain matcher with a deep lazy2 matching strategy and
301 /// a 16 MiB window. Compared to [`CompressionLevel::Better`], this level
302 /// uses larger hash and chain tables (2 M / 1 M entries vs 1 M / 512 K),
303 /// a deeper search (32 candidates vs 16), and a higher target match
304 /// length (128 vs 48), trading speed for the best compression ratio
305 /// available in this crate.
306 Best,
307 /// Numeric compression level.
308 ///
309 /// Levels 1–22 correspond to the C zstd level numbering. Higher values
310 /// produce smaller output at the cost of more CPU time. Negative values
311 /// select ultra-fast modes that trade ratio for speed. Level 0 is
312 /// treated as [`DEFAULT_LEVEL`](Self::DEFAULT_LEVEL), matching C zstd
313 /// semantics.
314 ///
315 /// Named variants map to specific numeric levels:
316 /// [`Fastest`](Self::Fastest) = 1, [`Default`](Self::Default) = 3,
317 /// [`Better`](Self::Better) = 7, [`Best`](Self::Best) = 11.
318 /// [`Best`](Self::Best) remains the highest-ratio named preset, but
319 /// [`Level`](Self::Level) values above 11 can target stronger (slower)
320 /// tuning than the named hierarchy.
321 ///
322 /// Levels above 11 use progressively larger windows and deeper search.
323 /// Levels 16–17 use a `btopt`-style price parser, 18–19 use `btultra`,
324 /// and 20–22 use a `btultra2`-style two-pass selection profile.
325 ///
326 /// Semver note: this variant was added after the initial enum shape and
327 /// is a breaking API change for downstream crates that exhaustively
328 /// `match` on [`CompressionLevel`] without a wildcard arm.
329 Level(i32),
330}
331
332impl CompressionLevel {
333 /// The minimum supported numeric compression level (ultra-fast mode).
334 pub const MIN_LEVEL: i32 = -131072;
335 /// The maximum supported numeric compression level.
336 pub const MAX_LEVEL: i32 = 22;
337 /// The default numeric compression level (equivalent to [`Default`](Self::Default)).
338 pub const DEFAULT_LEVEL: i32 = 3;
339
340 /// Create a compression level from a numeric value.
341 ///
342 /// Returns named variants for canonical levels (`0`/`3`, `1`, `7`, `11`)
343 /// and [`Level`](Self::Level) for all other values.
344 ///
345 /// With the default matcher backend (`MatchGeneratorDriver`), values
346 /// outside [`MIN_LEVEL`](Self::MIN_LEVEL)..=[`MAX_LEVEL`](Self::MAX_LEVEL)
347 /// are silently clamped during built-in level parameter resolution.
348 pub const fn from_level(level: i32) -> Self {
349 match level {
350 0 | Self::DEFAULT_LEVEL => Self::Default,
351 1 => Self::Fastest,
352 7 => Self::Better,
353 11 => Self::Best,
354 _ => Self::Level(level),
355 }
356 }
357}
358
359/// Trait used by the encoder that users can use to extend the matching facilities with their own algorithm
360/// making their own tradeoffs between runtime, memory usage and compression ratio
361///
362/// This trait operates on buffers that represent the chunks of data the matching algorithm wants to work on.
363/// Each one of these buffers is referred to as a *space*. One or more of these buffers represent the window
364/// the decoder will need to decode the data again.
365///
366/// This library asks the Matcher for a new buffer using `get_next_space` to allow reusing of allocated buffers when they are no longer part of the
367/// window of data that is being used for matching.
368///
369/// The library fills the buffer with data that is to be compressed and commits them back to the matcher using `commit_space`.
370///
371/// Then it will either call `start_matching` or, if the space is deemed not worth compressing, `skip_matching` is called.
372///
373/// This is repeated until no more data is left to be compressed.
374pub trait Matcher {
375 /// Get a space where we can put data to be matched on. Will be encoded as one block. The maximum allowed size is 128 kB.
376 fn get_next_space(&mut self) -> alloc::vec::Vec<u8>;
377 /// Get a reference to the last committed space
378 fn get_last_space(&mut self) -> &[u8];
379 /// Commit a space to the matcher so it can be matched against
380 fn commit_space(&mut self, space: alloc::vec::Vec<u8>);
381 /// Just process the data in the last committed space for future matching.
382 fn skip_matching(&mut self);
383 /// Hint-aware skip path used internally to thread a precomputed block
384 /// incompressibility verdict to matcher backends.
385 ///
386 /// Default implementation preserves backwards compatibility for external
387 /// custom matchers by delegating to [`skip_matching`](Self::skip_matching).
388 fn skip_matching_with_hint(&mut self, _incompressible_hint: Option<bool>) {
389 self.skip_matching();
390 }
391 /// Process the data in the last committed space for future matching AND generate matches for the data
392 fn start_matching(&mut self, handle_sequence: impl for<'a> FnMut(Sequence<'a>));
393 /// Reset this matcher so it can be used for the next new frame
394 fn reset(&mut self, level: CompressionLevel);
395 /// Provide a hint about the total uncompressed size for the next frame.
396 ///
397 /// Implementations may use this to select smaller hash tables and windows
398 /// for small inputs, matching the C zstd source-size-class behavior.
399 /// Called before [`reset`](Self::reset) when the caller knows the input
400 /// size (e.g. from pledged content size or file metadata).
401 ///
402 /// The default implementation is a no-op for custom matchers and
403 /// test stubs. The built-in runtime matcher (`MatchGeneratorDriver`)
404 /// overrides this hook and applies the hint during level resolution.
405 fn set_source_size_hint(&mut self, _size: u64) {}
406 /// Hint the byte size of the dictionary that will be primed into the next
407 /// frame. The built-in runtime matcher uses it to size the binary-tree /
408 /// hash-chain match-finder tables from the dictionary's cParams tier rather
409 /// than the source window (upstream zstd CDict economics), while keeping the
410 /// eviction window source-sized. Default no-op for custom matchers and test
411 /// stubs; consumed at the next [`reset`](Self::reset).
412 fn set_dictionary_size_hint(&mut self, _size: usize) {}
413 /// Drop any per-frame fine-grained parameter overrides installed via
414 /// the public parameter API, reverting to plain level-based geometry
415 /// at the next [`reset`](Self::reset). Called by
416 /// [`FrameCompressor::set_compression_level`](crate::encoding::FrameCompressor::set_compression_level)
417 /// so switching back to a bare level after a customized frame does not
418 /// keep the old overrides sticky. Default no-op for custom matchers.
419 fn clear_param_overrides(&mut self) {}
420 /// Prime matcher state with dictionary history before compressing the next frame.
421 /// Default implementation is a no-op for custom matchers that do not support this.
422 fn prime_with_dictionary(&mut self, _dict_content: &[u8], _offset_hist: [u32; 3]) {}
423 /// Whether the most recent [`reset`](Self::reset) re-borrowed a resident
424 /// attach-mode dictionary (kept the dict bytes + cached index in place).
425 /// When `true` the caller MUST skip [`Self::prime_with_dictionary`] and only
426 /// reapply the offset history via [`Self::reapply_resident_dictionary`].
427 fn dictionary_is_resident(&self) -> bool {
428 false
429 }
430 /// Reapply the dictionary's offset history to a re-borrowed frame — the cheap
431 /// tail of priming, without the dict commit / re-index. Default no-op.
432 fn reapply_resident_dictionary(&mut self, _offset_hist: [u32; 3]) {}
433 /// CDict-equivalent fast path for repeated frames sharing one dictionary.
434 /// Restore the matcher state captured by [`Self::capture_primed_dictionary`]
435 /// at the SAME level (a table copy) instead of re-running
436 /// [`Self::prime_with_dictionary`] (which re-hashes every dictionary
437 /// position). Returns `true` when a matching snapshot was restored;
438 /// `false` (the default) means the caller must prime then capture.
439 fn restore_primed_dictionary(&mut self, _level: CompressionLevel) -> bool {
440 false
441 }
442 /// Snapshot the post-prime matcher state for the given level so later
443 /// frames can [`Self::restore_primed_dictionary`] it. Default no-op.
444 fn capture_primed_dictionary(&mut self, _level: CompressionLevel) {}
445 /// Drop any captured prime snapshot (dictionary or level changed).
446 /// Default no-op.
447 fn invalidate_primed_dictionary(&mut self) {}
448 /// Seed matcher cost model with dictionary entropy tables before the next frame.
449 /// Default implementation is a no-op for custom matchers.
450 fn seed_dictionary_entropy(
451 &mut self,
452 _huff: Option<&crate::huff0::huff0_encoder::HuffmanTable>,
453 _ll: Option<&crate::fse::fse_encoder::FSETable>,
454 _ml: Option<&crate::fse::fse_encoder::FSETable>,
455 _of: Option<&crate::fse::fse_encoder::FSETable>,
456 ) {
457 }
458 /// Returns whether this matcher can consume dictionary priming state and produce
459 /// dictionary-dependent sequences. Defaults to `false` for custom matchers.
460 fn supports_dictionary_priming(&self) -> bool {
461 false
462 }
463 /// Whether a sample of `block` hashes to a match in an attached dictionary.
464 /// The raw-fast-path uses this to avoid skipping the scan on a block that
465 /// looks incompressible but compresses against the dictionary (an external
466 /// match the block's own content cannot reveal). Defaults to `false` for
467 /// custom matchers (and the no-dict case), leaving the content-only verdict.
468 fn block_samples_match_dict(&self, _block: &[u8]) -> bool {
469 false
470 }
471 /// Heap bytes this matcher's allocations hold (tables, history, scratch),
472 /// excluding the inline struct itself. Lets a context report its true
473 /// footprint via `ZSTD_sizeof_CCtx`. Defaults to `0` for custom matchers.
474 fn heap_size(&self) -> usize {
475 0
476 }
477 /// The size of the window the decoder will need to execute all sequences produced by this matcher.
478 ///
479 /// Must return a positive (non-zero) value; returning 0 causes
480 /// [`StreamingEncoder`] to reject the first write with an invalid-input error
481 /// (`InvalidInput` with `std`, `Other` with `no_std`).
482 ///
483 /// Must remain stable for the lifetime of a frame.
484 /// It may change only after `reset()` is called for the next frame
485 /// (for example because the compression level changed).
486 fn window_size(&self) -> u64;
487}
488
489#[derive(PartialEq, Eq, Debug)]
490/// Sequences that a [`Matcher`] can produce
491pub enum Sequence<'data> {
492 /// Is encoded as a sequence for the decoder sequence execution.
493 ///
494 /// First the literals will be copied to the decoded data,
495 /// then `match_len` bytes are copied from `offset` bytes back in the decoded data
496 Triple {
497 literals: &'data [u8],
498 offset: usize,
499 match_len: usize,
500 },
501 /// This is returned as the last sequence in a block
502 ///
503 /// These literals will just be copied at the end of the sequence execution by the decoder
504 Literals { literals: &'data [u8] },
505}
506
507#[cfg(test)]
508mod compress_bound_tests;