Struct Decoder

Source
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>

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn as_buf(&self) -> &[u8]

Get a reference to the inner compressed data buffer.

Source

pub fn into_buf(self) -> Cow<'a, [u8]>

Get the inner compressed data buffer.

Source

pub fn nbytes(&self) -> usize

Get the number of bytes in the uncompressed data.

Source

pub fn typesize(&self) -> usize

Get the size of each item in the decoder’s buffer.

Source

pub fn items_num(&self) -> usize

Get the number of items in the decoder’s buffer.

Source

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.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

§

impl<'a> Freeze for Decoder<'a>

§

impl<'a> RefUnwindSafe for Decoder<'a>

§

impl<'a> Send for Decoder<'a>

§

impl<'a> Sync for Decoder<'a>

§

impl<'a> Unpin for Decoder<'a>

§

impl<'a> UnwindSafe for Decoder<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.