Skip to main content

BlockEncoder

Struct BlockEncoder 

Source
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

Source

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);
Source

pub const fn block_size(&self) -> BlockSize

Return the block size this block encoder was built for.

Source

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);
Source

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§

Source§

impl Debug for BlockEncoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.