hff_core/write/
chunk_array.rs

1use crate::{ByteOrder, Chunk, Result};
2use std::ops::{Index, IndexMut};
3
4/// After flattening a table, this is where the chunks will
5/// exist.
6#[derive(Debug)]
7pub struct ChunkArray {
8    chunks: Vec<Chunk>,
9}
10
11impl ChunkArray {
12    /// Create a new empty ChunkArray.
13    pub fn new() -> Self {
14        Self { chunks: vec![] }
15    }
16
17    /// Get the length of the chunk array.
18    pub fn len(&self) -> usize {
19        self.chunks.len()
20    }
21
22    /// Push a new chunk onto the array.
23    pub fn push(&mut self, chunk: Chunk) {
24        self.chunks.push(chunk);
25    }
26
27    /// Convert the chunk array to a byte vector.
28    pub fn to_bytes<E: ByteOrder>(self) -> Result<Vec<u8>> {
29        let mut buffer = vec![];
30        for chunk in self.chunks {
31            chunk.write::<E>(&mut buffer)?;
32        }
33        Ok(buffer)
34    }
35}
36
37impl Index<usize> for ChunkArray {
38    type Output = Chunk;
39
40    fn index(&self, index: usize) -> &Self::Output {
41        &self.chunks[index]
42    }
43}
44
45impl IndexMut<usize> for ChunkArray {
46    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
47        &mut self.chunks[index]
48    }
49}