pub struct Decoder<'a> { /* private fields */ }
Expand description
A decoder for Blosc compressed data.
This struct is not the usual stream-like decoder that commonly exists in Rust compression libraries, but rather an array-like object that allows random access to items in the compressed data or decoding the entire buffer. This is because blosc is not a streaming library, and it operates on the entire data buffer at once.
The compressed data is held in memory within the decoder, and decoding is done either by decompressing the entire buffer, or by accessing individual items or ranges of items. In both cases, the decoder remains unchanged and only the compressed data is held by it.
Implementations§
Source§impl<'a> Decoder<'a>
impl<'a> Decoder<'a>
Sourcepub fn from_reader(reader: &mut impl Read) -> Result<Self, DecompressError>
pub fn from_reader(reader: &mut impl Read) -> Result<Self, DecompressError>
Create a new decoder from a reader that contains Blosc compressed data.
First a header of a fixed size is read from the reader, which contains metadata about the length of the compressed data. Then the rest of the compressed data is read into an in-memory internal buffer held by the decoder, and the reader is not used after this point.
Sourcepub fn new(src: impl Into<Cow<'a, [u8]>>) -> Result<Self, DecompressError>
pub fn new(src: impl Into<Cow<'a, [u8]>>) -> Result<Self, DecompressError>
Create a new decoder from a slice of Blosc compressed data.
No decompression is performed at this point. Decompression is done on demand either by decompressing the entire buffer or by accessing individual items or ranges of items.
Sourcepub fn decompress(
&self,
numinternalthreads: u32,
) -> Result<Vec<u8>, DecompressError>
pub fn decompress( &self, numinternalthreads: u32, ) -> Result<Vec<u8>, DecompressError>
Decompress the entire buffer and return the decompressed data as a Vec<u8>
.
Note that the returned vector may not be aligned to the original data type’s alignment, and the caller should
ensure that the alignment is correct before transmuting it to original type. If the alignment does not match
the original data type, the bytes should be copied to a new aligned allocation before transmuting, otherwise
undefined behavior may occur. Alternatively, the caller can use Self::decompress_into
and provide an already
aligned destination buffer.
§Arguments
numinternalthreads
: The number of threads to use internally.
§Returns
A Result
containing the decompressed data as a Vec<u8>
, or a DecompressError
if an error occurs.
Sourcepub fn decompress_into(
&self,
dst: &mut [MaybeUninit<u8>],
numinternalthreads: u32,
) -> Result<usize, DecompressError>
pub fn decompress_into( &self, dst: &mut [MaybeUninit<u8>], numinternalthreads: u32, ) -> Result<usize, DecompressError>
Decompress the entire buffer and write the decompressed data into the provided destination buffer.
Note that if the destination buffer is not aligned to the original data type’s alignment, the caller should not transmute the decompressed data to the original type, as this may lead to undefined behavior.
§Arguments
dst
: The destination buffer where the decompressed data will be written.numinternalthreads
: The number of threads to use internally.
§Returns
The number of bytes copied into the destination buffer.
Sourcepub fn item(&self, idx: usize) -> Result<Vec<u8>, DecompressError>
pub fn item(&self, idx: usize) -> Result<Vec<u8>, DecompressError>
Get an item at the specified index.
Each item is typesize
(as provided during encoding) bytes long, and the index is zero-based.
Note that the returned vector may not be aligned to the original data type’s alignment, and the caller should
ensure that the alignment is correct before transmuting it to original type. If the alignment does not match
the original data type, the bytes should be copied to a new aligned allocation before transmuting, otherwise
undefined behavior may occur. Alternatively, the caller can use Self::item_into
and provide an already
aligned destination buffer.
Sourcepub fn item_into(
&self,
idx: usize,
dst: &mut [MaybeUninit<u8>],
) -> Result<usize, DecompressError>
pub fn item_into( &self, idx: usize, dst: &mut [MaybeUninit<u8>], ) -> Result<usize, DecompressError>
Get an item at the specified index and copy it into the provided destination buffer.
Each item is typesize
(as provided during encoding) bytes long, and the index is zero-based.
Note that if the destination buffer is not aligned to the original data type’s alignment, the caller should not transmute the decompressed data to original type, as this may lead to undefined behavior.
§Returns
The number of bytes copied into the destination buffer.
Sourcepub fn items(&self, idx: Range<usize>) -> Result<Vec<u8>, DecompressError>
pub fn items(&self, idx: Range<usize>) -> Result<Vec<u8>, DecompressError>
Get a range of items specified by the index range.
Each item is typesize
(as provided during encoding) bytes long, and the index is zero-based.
Note that the returned vector may not be aligned to the original data type’s alignment, and the caller should
ensure that the alignment is correct before transmuting it to original type. If the alignment does not match
the original data type, the bytes should be copied to a new aligned allocation before transmuting, otherwise
undefined behavior may occur. Alternatively, the caller can use Self::items_into
and provide an already
aligned destination buffer.
Sourcepub fn items_into(
&self,
idx: Range<usize>,
dst: &mut [MaybeUninit<u8>],
) -> Result<usize, DecompressError>
pub fn items_into( &self, idx: Range<usize>, dst: &mut [MaybeUninit<u8>], ) -> Result<usize, DecompressError>
Get a range of items specified by the index range and copy them into the provided destination buffer.
Each item is typesize
(as provided during encoding) bytes long, and the index is zero-based.
Note that if the destination buffer is not aligned to the original data type’s alignment, the caller should not transmute the decompressed data to original type, as this may lead to undefined behavior.
§Returns
The number of bytes copied into the destination buffer.