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
impl Chunk
Sourcepub fn new(chunk: *mut u8, needs_free: bool) -> Self
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.
Sourcepub fn from_vec(v: Vec<u8>) -> Result<Self>
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());Sourcepub fn into_vec(self) -> Result<Vec<u8>>
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
Sourcepub fn len<T>(&self) -> Result<usize>
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);pub fn getitems<T>(&self, offset: usize, n_items: usize) -> Result<Vec<T>>
Sourcepub fn uninit<T>(cparams: CParams, len: usize) -> Result<Self>
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);Sourcepub fn repeatval<T>(cparams: CParams, value: T, len: usize) -> Result<Self>
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]);Sourcepub fn zeros<T>(cparams: CParams, len: usize) -> Result<Self>
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 eachSourcepub fn compression_ratio(&self) -> Result<f32>
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);Sourcepub fn compress<T: 'static>(
src: &[T],
typesize: Option<usize>,
clevel: Option<CLevel>,
filter: Option<Filter>,
codec: Option<Codec>,
) -> Result<Self>
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);Sourcepub fn decompress<T>(&self) -> Result<Vec<T>>
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]);Sourcepub fn from_schunk(schunk: &mut SChunk, nchunk: usize) -> Result<Self>
pub fn from_schunk(schunk: &mut SChunk, nchunk: usize) -> Result<Self>
Create a new Chunk from a SChunk
Sourcepub fn info(&self) -> Result<CompressedBufferInfo>
pub fn info(&self) -> Result<CompressedBufferInfo>
Get CompressedBufferInfo for this chunk.