hff_core/read/
chunk_view.rs

1use super::Hff;
2use crate::{ContentInfo, Identifier};
3use std::fmt::Debug;
4
5/// A view to a chunk.
6#[derive(Copy, Clone)]
7pub struct ChunkView<'a, T: Debug> {
8    hff: &'a Hff<T>,
9    index: usize,
10}
11
12impl<'a, T: Debug> Debug for ChunkView<'a, T> {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        write!(f, "{:?}", self.hff.chunks_array()[self.index])
15    }
16}
17
18impl<'a, T: Debug> ChunkView<'a, T> {
19    /// Create a new view.
20    pub fn new(hff: &'a Hff<T>, index: usize) -> Self {
21        Self { hff, index }
22    }
23
24    /// Get the hff this was built from.
25    pub fn hff(&self) -> &Hff<T> {
26        self.hff
27    }
28
29    /// Get the current index.
30    pub fn index(&self) -> usize {
31        self.index
32    }
33
34    /// Get the identifier.
35    pub fn identifier(&self) -> Identifier {
36        self.hff.chunks_array()[self.index].identifier()
37    }
38
39    /// Get the size of the data in the chunk.
40    pub fn size(&self) -> usize {
41        self.hff.chunks_array()[self.index].length() as usize
42    }
43}
44
45impl<'a, T: Debug> ContentInfo for ChunkView<'a, T> {
46    fn len(&self) -> u64 {
47        self.hff.chunks_array()[self.index].length()
48    }
49
50    fn offset(&self) -> u64 {
51        self.hff.chunks_array()[self.index].offset()
52    }
53}