[][src]Function rscache::codec::encode

pub fn encode(
    compression: Compression,
    data: &[u8],
    revision: Option<i16>
) -> Result<Vec<u8>>

Encodes a buffer, with the selected Compression format. Revision is an optional argument that encodes the version of this buffer into it, if no revision should be encoded pass None.

The following process takes place when encoding:

  1. Compress the buffer with the selected compression format.
  2. Allocate a new buffer.
  3. Push the compression type as a byte into the new buffer.
  4. Push the length (u32) into the buffer of the compressed data from step 1.
  5. If a compression type was selected (and not Compression::None) insert the uncompressed length as u32.
  6. Extend the buffer with the compressed data.
  7. Add the revision as i16 if present.
  8. Encode complete.

Supported compression types:

  • Gzip
  • Bzip2

NOTE: When compressing with gzip the header is removed before the compressed data is returned. The encoded buffer will not contain the gzip header.

Errors

Returns an error if the data couldn't be compressed or is invalid.

Examples

use rscache::codec::Compression;
 
let encoded_buffer = rscache::codec::encode(Compression::Bzip2, &buffer, None)?;
 
assert_eq!(Compression::Bzip2 as u8, encoded_buffer[0]);