Skip to content

Add a helper for compressing a slice into a vec #86

Open
@casey

Description

@casey

A common use case is compressing a &[u8] into a Vec<u8> without doing any I/O. The current docs don't make it clear how to do this.

A google search turns up this forum post with this code snippet:

use std::io::Write;

pub fn compress(input: &[u8]) -> Vec<u8> {
    let mut writer = brotli::CompressorWriter::new(
        Vec::new(),
        4096,
        11,
        22);
    writer.write_all(input).unwrap();
    writer.into_inner()
}

Which is nontrivial to figure out. A helper functions in the root of the crate to compress a &[u8] into a Vec<u8> and to decompress a Vec<u8> into a &[u8] would help a lot with this. (Also, if these functions don't do I/O then perhaps they could be be infallible.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions