Struct q_compress::Compressor
source · [−]pub struct Compressor<T> where
T: NumberLike, { /* private fields */ }
Expand description
Converts vectors of numbers into compressed bytes.
You can use the compressor very easily:
use q_compress::Compressor;
let my_nums = vec![1, 2, 3];
let compressor = Compressor::<i32>::default();
let bytes = compressor.simple_compress(&my_nums);
You can also get full control over the compression process:
use q_compress::{BitWriter, Compressor, CompressorConfig};
let compressor = Compressor::<i32>::from_config(CompressorConfig {
compression_level: 5,
..Default::default()
});
let mut writer = BitWriter::default();
compressor.header(&mut writer).expect("header failure");
let chunk_0 = vec![1, 2, 3];
compressor.chunk(&chunk_0, &mut writer).expect("chunk failure");
let chunk_1 = vec![4, 5];
compressor.chunk(&chunk_1, &mut writer).expect("chunk failure");
compressor.footer(&mut writer).expect("footer failure");
let bytes = writer.pop();
Note that in practice we would need larger chunks than this to achieve good compression, preferably containing 10k-10M numbers.
Implementations
sourceimpl<T> Compressor<T> where
T: NumberLike,
impl<T> Compressor<T> where
T: NumberLike,
sourcepub fn from_config(config: CompressorConfig) -> Self
pub fn from_config(config: CompressorConfig) -> Self
Creates a new compressor, given a CompressorConfig
.
Internally, the compressor builds Flags
as well as an internal
configuration that doesn’t show up in the output file.
You can inspect the flags it chooses with .flags()
.
sourcepub fn header(&self, writer: &mut BitWriter) -> QCompressResult<()>
pub fn header(&self, writer: &mut BitWriter) -> QCompressResult<()>
Writes out a header using the compressor’s data type and flags. Will return an error if the writer is not at a byte-aligned position.
Each .qco file must start with such a header, which contains:
- a 4-byte magic header for “qco!” in ascii,
- a byte for the data type (e.g.
i64
has byte 1 andf64
has byte 5), and - bytes for the flags used to compress.
sourcepub fn chunk(
&self,
nums: &[T],
writer: &mut BitWriter
) -> QCompressResult<ChunkMetadata<T>>
pub fn chunk(
&self,
nums: &[T],
writer: &mut BitWriter
) -> QCompressResult<ChunkMetadata<T>>
Writes out a chunk of data representing the provided numbers. Will return an error if the writer is not at a byte-aligned position or the slice of numbers is empty.
Each chunk contains a ChunkMetadata
section followed by the chunk body.
The chunk body encodes the numbers passed in here.
Writes out a single footer byte indicating that the .qco file has ended. Will return an error if the writer is not byte-aligned.
sourcepub fn simple_compress(&self, nums: &[T]) -> Vec<u8>
pub fn simple_compress(&self, nums: &[T]) -> Vec<u8>
Takes in a slice of numbers and returns compressed bytes.
Trait Implementations
sourceimpl<T: Clone> Clone for Compressor<T> where
T: NumberLike,
impl<T: Clone> Clone for Compressor<T> where
T: NumberLike,
sourcefn clone(&self) -> Compressor<T>
fn clone(&self) -> Compressor<T>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<T: Debug> Debug for Compressor<T> where
T: NumberLike,
impl<T: Debug> Debug for Compressor<T> where
T: NumberLike,
sourceimpl<T: NumberLike> Default for Compressor<T>
impl<T: NumberLike> Default for Compressor<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Compressor<T> where
T: RefUnwindSafe,
impl<T> Send for Compressor<T> where
T: Send,
impl<T> Sync for Compressor<T> where
T: Sync,
impl<T> Unpin for Compressor<T> where
T: Unpin,
impl<T> UnwindSafe for Compressor<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more