[][src]Struct brotli2::raw::Compress

pub struct Compress { /* fields omitted */ }

In-memory state for compressing/encoding data with brotli

This stream is at the heart of the I/O encoders and is used to compress data.

Methods

impl Compress[src]

pub fn new() -> Compress[src]

Creates a new compressor ready to encode data into brotli

pub fn compress(
    &mut self,
    op: CompressOp,
    input: &mut &[u8],
    output: &mut &mut [u8]
) -> Result<CoStatus, Error>
[src]

Pass some input data to the compressor and write it to a buffer of output data, compressing or otherwise handling it as instructed by the specified operation.

This function will handle the data in input and place the output in output, returning the Result. Possible statuses are that the operation is complete or incomplete.

The input slice is updated to point to the remaining data that was not consumed, and the output slice is updated to point to the portion of the output slice that still needs to be filled in.

If the result of a compress operation is Unfinished (which it may be for any operation except Process), you must call the operation again with the same operation and input buffer and more space to output to. Process will never return Unfinished, but it is a logic error to end a buffer without calling either Flush or Finish as some output data may not have been written.

Errors

Returns an error if brotli encountered an error while processing the stream.

Examples

use brotli2::raw::{Error, Compress, CompressOp, CoStatus, decompress_buf};

// An example of compressing `input` into the destination vector
// `output`, expanding as necessary
fn compress_vec(mut input: &[u8],
                output: &mut Vec<u8>) -> Result<(), Error> {
    let mut compress = Compress::new();
    let nilbuf = &mut &mut [][..];
    loop {
        // Compressing to a buffer is easiest when the slice is already
        // available - since we need to grow, extend from compressor
        // internal buffer.
        let status = try!(compress.compress(CompressOp::Finish, &mut input, nilbuf));
        while let Some(buf) = compress.take_output(None) {
            output.extend_from_slice(buf)
        }
        match status {
            CoStatus::Finished => break,
            CoStatus::Unfinished => (),
        }
    }
    Ok(())
}

fn assert_roundtrip(data: &[u8]) {
    let mut compressed = Vec::new();
    compress_vec(data, &mut compressed).unwrap();

    let mut decompressed = [0; 2048];
    let mut decompressed = &mut decompressed[..];
    decompress_buf(&compressed, &mut decompressed).unwrap();
    assert_eq!(decompressed, data);
}

assert_roundtrip(b"Hello, World!");
assert_roundtrip(b"");
assert_roundtrip(&[6; 1024]);

pub fn take_output(&mut self, size_limit: Option<usize>) -> Option<&[u8]>[src]

Retrieve a slice of the internal compressor buffer up to size_limit in length (unlimited length if None), consuming it. As the internal buffer may not be contiguous, consecutive calls may return more output until this function returns None.

pub fn set_params(&mut self, params: &CompressParams)[src]

Configure the parameters of this compression session.

Note that this is likely to only successful if called before compression starts.

Trait Implementations

impl Drop for Compress[src]

impl Sync for Compress[src]

impl Send for Compress[src]

Auto Trait Implementations

impl Unpin for Compress

impl UnwindSafe for Compress

impl RefUnwindSafe for Compress

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]