Chunk

Struct Chunk 

Source
pub struct Chunk { /* private fields */ }
Expand description

Wraps a single chunk of a super-chunk.

Normally constructed via Chunk::from_schunk

§Example

use std::io::Write;
use blosc2::{CParams, DParams, Blosc2Guard};
use blosc2::schunk::{Storage, SChunk, Chunk};

let _guard = Blosc2Guard::get_or_init();

let input = b"some data";
let storage = Storage::default()
    .set_contiguous(true)
    .set_cparams(CParams::from(&input[0]))
    .set_dparams(DParams::default());
let mut schunk = SChunk::new(storage);

let n = schunk.write(input).unwrap();  // same as schunk.append_buffer(input)?;
assert_eq!(n as usize, input.len());
assert_eq!(schunk.n_chunks(), 1);

let chunk = Chunk::from_schunk(&mut schunk, 0).unwrap();  // Get first (and only) chunk
assert_eq!(chunk.info().unwrap().nbytes() as usize, input.len());

Implementations§

Source§

impl Chunk

Source

pub fn new(chunk: *mut u8, needs_free: bool) -> Self

Create a new Chunk directly from a pointer, you probably want Chunk::from_schunk instead.

Source

pub fn from_vec(v: Vec<u8>) -> Result<Self>

Construct Chunk from vector of bytes, this Vec is assumed to be the result of a valid chunk de/compression or other initialization method like uinit/zeros etc.

§Example
use blosc2::{schunk::Chunk, compress};

let buf: Vec<u8> = compress(&vec![0i32, 1, 2, 3, 4], None, None, None, None).unwrap();
assert!(Chunk::from_vec(buf).is_ok());
Source

pub fn into_vec(self) -> Result<Vec<u8>>

Export this Chunk into a Vec<u8> Maybe clone underlying vec if it’s managed by blosc2

Source

pub fn as_slice(&self) -> Result<&[u8]>

Get the raw buffer of this chunk

Source

pub fn len<T>(&self) -> Result<usize>

Return the number of elements in the compressed Chunk

§Example
use blosc2::{schunk::Chunk, compress};

let chunk: Chunk = compress(&vec![0i32, 1, 2, 3, 4], None, None, None, None)
   .unwrap()
   .try_into()
   .unwrap();
assert_eq!(chunk.len::<i32>().unwrap(), 5);
Source

pub fn getitems<T>(&self, offset: usize, n_items: usize) -> Result<Vec<T>>

Source

pub fn uninit<T>(cparams: CParams, len: usize) -> Result<Self>

Create a chunk made of uninitialized values

§Examplek
use blosc2::CParams;
use blosc2::schunk::Chunk;

let chunk = Chunk::uninit::<u8>(CParams::default(), 10).unwrap();
assert_eq!(chunk.info().unwrap().nbytes(), 10);
Source

pub fn repeatval<T>(cparams: CParams, value: T, len: usize) -> Result<Self>

Create a chunk made of repeating a value

§Examplek
use blosc2::CParams;
use blosc2::schunk::Chunk;

let mut chunk = Chunk::repeatval::<f32>(CParams::default().set_typesize::<f32>(), 0.123, 4).unwrap();
assert_eq!(chunk.info().unwrap().nbytes(), 16);  // 4 elements * 4 bytes each
assert_eq!(chunk.decompress::<f32>().unwrap(), vec![0.123_f32, 0.123, 0.123, 0.123]);
Source

pub fn zeros<T>(cparams: CParams, len: usize) -> Result<Self>

Create a chunk made of zeros

§Example
use blosc2::CParams;
use blosc2::schunk::Chunk;

let cparams = CParams::default().set_typesize::<u32>();
let chunk = Chunk::zeros::<u32>(cparams, 10).unwrap();
assert_eq!(chunk.info().unwrap().nbytes(), 40);  // 10 elements * 4 bytes each
Source

pub fn compression_ratio(&self) -> Result<f32>

Compression ratio of the Chunk

§Example
use blosc2::schunk::Chunk;

let chunk = Chunk::compress(&vec![0i32; 1_000], None, None, None, None).unwrap();
let ratio = chunk.compression_ratio().unwrap();
assert_eq!(ratio, 125.0);
Source

pub fn compress<T: 'static>( src: &[T], typesize: Option<usize>, clevel: Option<CLevel>, filter: Option<Filter>, codec: Option<Codec>, ) -> Result<Self>

Helper method to construct a Chunk directly from compression. This is equivelent to:

use blosc2::{compress, schunk::Chunk};

let compressed = compress(&vec![0u8, 1, 2, 3], None, None, None, None).unwrap();
let chunk = Chunk::from_vec(compressed).unwrap();
assert_eq!(chunk.len::<u8>().unwrap(), 4);
Source

pub fn decompress<T>(&self) -> Result<Vec<T>>

Decompress the current chunk

§Example
use blosc2::CParams;
use blosc2::schunk::Chunk;

let cparams = CParams::default().set_typesize::<i64>();
let mut chunk = Chunk::zeros::<i64>(cparams, 5).unwrap();

assert_eq!(chunk.info().unwrap().nbytes(), 40);  // 5 elements * 64bit == 40bytes
let decompressed = chunk.decompress::<i64>().unwrap();
dbg!(decompressed.len());
assert_eq!(decompressed, vec![0i64; 5]);
Source

pub fn from_schunk(schunk: &mut SChunk, nchunk: usize) -> Result<Self>

Create a new Chunk from a SChunk

Source

pub fn nbytes(&self) -> Result<usize>

Uncompressed bytes in this Chunk

Source

pub fn cbytes(&self) -> Result<usize>

Compressed bytes in this Chunk

Source

pub fn info(&self) -> Result<CompressedBufferInfo>

Get CompressedBufferInfo for this chunk.

Trait Implementations§

Source§

impl Clone for Chunk

Source§

fn clone(&self) -> Chunk

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Drop for Chunk

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl TryFrom<Vec<u8>> for Chunk

Source§

type Error = Error

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

fn try_from(v: Vec<u8>) -> Result<Self>

Performs the conversion.
Source§

impl Send for Chunk

Source§

impl Sync for Chunk

Auto Trait Implementations§

§

impl Freeze for Chunk

§

impl !RefUnwindSafe for Chunk

§

impl Unpin for Chunk

§

impl !UnwindSafe for Chunk

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.