pub struct BlockEncoder { /* private fields */ }Expand description
Block encoder with constructor-owned working memory.
The two swap buffers each hold compress_bound of the block
size, so every intermediate stage result fits. The hash table and
the entropy coder state persist across calls but reset per block,
exactly like the reference. Slice-based calls borrow the input and
return an owned payload.
§Examples
Build a streaming frame with one block, then decode it with
stream::decompress.
use bzip3_rs::{BlockEncoder, BlockHeader, BlockSize, Jobs, StreamHeader, stream};
let input = b"hello world ".repeat(1000);
let size = BlockSize::MIN;
let mut encoder = BlockEncoder::new(size)?;
let payload = encoder.encode_block(&input)?;
let mut frame = StreamHeader::new(size).to_bytes().to_vec();
frame.extend_from_slice(&BlockHeader::for_block(payload.len(), input.len())?.to_bytes());
frame.extend_from_slice(&payload);
let mut plain = Vec::new();
stream::decompress(frame.as_slice(), &mut plain, Jobs::SERIAL)?;
assert_eq!(plain, input);Implementations§
Source§impl BlockEncoder
impl BlockEncoder
Sourcepub fn new(block_size: BlockSize) -> Result<Self>
pub fn new(block_size: BlockSize) -> Result<Self>
Build a block encoder for block_size, allocating working memory once.
§Errors
Returns Error::OutOfMemory when any working buffer allocation fails.
§Examples
use bzip3_rs::{BlockEncoder, BlockSize};
let encoder = BlockEncoder::new(BlockSize::MIN)?;
assert_eq!(encoder.block_size(), BlockSize::MIN);Sourcepub const fn block_size(&self) -> BlockSize
pub const fn block_size(&self) -> BlockSize
Return the block size this block encoder was built for.
Sourcepub fn encode_block(&mut self, data: &[u8]) -> Result<Vec<u8>>
pub fn encode_block(&mut self, data: &[u8]) -> Result<Vec<u8>>
Encode one block into a new vector.
Same output as BlockEncoder::encode_block_into. Use that call
to reuse one output vector across blocks.
§Errors
Returns the same errors as BlockEncoder::encode_block_into.
§Examples
use bzip3_rs::{BlockDecoder, BlockEncoder, BlockSize};
let data = b"hello world ".repeat(100);
let mut encoder = BlockEncoder::new(BlockSize::MIN)?;
let payload = encoder.encode_block(&data)?;
let mut decoder = BlockDecoder::new(BlockSize::MIN)?;
assert_eq!(decoder.decode_block(&payload, u32::try_from(data.len())?)?, data);Sourcepub fn encode_block_into(
&mut self,
data: &[u8],
out: &mut Vec<u8>,
) -> Result<()>
pub fn encode_block_into( &mut self, data: &[u8], out: &mut Vec<u8>, ) -> Result<()>
Encode one block through the full pipeline.
Append the block to out. On an error, out keeps its length.
Inputs below 64 bytes take the stored-raw layout.
Larger inputs run RLE, then LZP, then BWT, then entropy coding,
with each of the first two stages kept only when it shrinks the
block. The header matches the reference byte for byte: CRC32,
BWT index, model byte, then the kept stage sizes in LZP, RLE
order, then the coded stream.
§Errors
Returns Error::BlockTooLarge when data exceeds the block size,
Error::OutOfMemory when the RLE stage cannot grow its output,
Error::BwtFailed when the transform fails.
§Examples
use bzip3_rs::{BlockEncoder, BlockSize};
let mut encoder = BlockEncoder::new(BlockSize::MIN)?;
let mut out = Vec::new();
for chunk in [b"first block ".repeat(50), b"second block ".repeat(50)] {
out.clear(); // Reuse one vector for every block.
encoder.encode_block_into(&chunk, &mut out)?;
assert!(!out.is_empty());
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for BlockEncoder
impl RefUnwindSafe for BlockEncoder
impl Send for BlockEncoder
impl Sync for BlockEncoder
impl Unpin for BlockEncoder
impl UnsafeUnpin for BlockEncoder
impl UnwindSafe for BlockEncoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more