MCPcopy Create free account
hub / github.com/RetypeOS/parcode / Compressor

Interface Compressor

src/compression.rs:170–241  ·  view source on GitHub ↗

Interface for compression algorithms. This trait defines the contract that all compression implementations must fulfill. Each compressor is identified by a unique ID (0-7) that is stored in the chunk's `MetaByte`, allowing the reader to select the appropriate decompressor. ## Design The trait provides three methods: 1. **[`id`](Self::id):** Returns the unique algorithm identifier 2. **[`compre

Source from the content-addressed store, hash-verified

168/// - Returning `Cow::Borrowed` from `compress` when compression doesn't help
169/// enables zero-copy optimization
170pub trait Compressor: Send + Sync + std::fmt::Debug {
171 /// Returns the unique ID stored in the `MetaByte` (Bits 1-3).
172 ///
173 /// This ID must be in the range 0-7 (3 bits) and should be unique across all
174 /// registered compressors. ID 0 is reserved for [`NoCompression`].
175 ///
176 /// ## ID Allocation
177 ///
178 /// - **0:** `NoCompression` (reserved)
179 /// - **1:** LZ4 (reserved)
180 /// - **2-7:** Available for custom algorithms
181 fn id(&self) -> u8;
182
183 /// Compresses the input data.
184 ///
185 /// Returns a `Cow<[u8]>` which may borrow the input if compression is not performed
186 /// or if the compressed size would be larger than the original (negative compression).
187 ///
188 /// ## Return Value
189 ///
190 /// - `Cow::Borrowed(data)`: Compression was not beneficial or not performed
191 /// - `Cow::Owned(compressed)`: Data was successfully compressed
192 ///
193 /// ## Errors
194 ///
195 /// Returns [`ParcodeError::Compression`] if the compression algorithm fails.
196 ///
197 /// ## Performance
198 ///
199 /// This method may allocate memory for the compressed output. For better performance
200 /// during serialization, use [`compress_append`](Self::compress_append) instead.
201 fn compress<'a>(&self, data: &'a [u8]) -> Result<Cow<'a, [u8]>>;
202
203 /// Decompresses the input data.
204 ///
205 /// Returns a `Cow<[u8]>` containing the original uncompressed data.
206 ///
207 /// ## Return Value
208 ///
209 /// - `Cow::Borrowed(data)`: No decompression needed (e.g., `NoCompression`)
210 /// - `Cow::Owned(decompressed)`: Data was successfully decompressed
211 ///
212 /// ## Errors
213 ///
214 /// Returns [`ParcodeError::Compression`] if:
215 /// - The compressed data is corrupted
216 /// - The decompression algorithm fails
217 /// - The decompressed size exceeds expected bounds
218 fn decompress<'a>(&self, data: &'a [u8]) -> Result<Cow<'a, [u8]>>;
219
220 /// Compresses data and appends it directly to the output vector.
221 ///
222 /// This method is more efficient than [`compress`](Self::compress) during serialization
223 /// because it avoids intermediate allocations by writing directly to the final buffer.
224 ///
225 /// ## Parameters
226 ///
227 /// - `data`: The input data to compress

Callers

nothing calls this directly

Implementers 1

compression.rssrc/compression.rs

Calls

no outgoing calls

Tested by

no test coverage detected