Skip to main content

array_format/
address.rs

1//! Block identifiers and chunk address types.
2//!
3//! A [`ChunkAddress`] locates a slice of array data within a specific
4//! block by `(block_id, offset, size)`.
5
6use rkyv::{Archive, Deserialize, Serialize};
7
8#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, Archive, Serialize, Deserialize)]
9pub struct BlockAllocAddress {
10    id: BlockId,
11    offset: u64,
12    size: u64,
13}
14
15/// A block identifier within the file.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Archive, Serialize, Deserialize)]
17pub struct BlockId(pub u32);
18
19/// Locates a contiguous slice of bytes within a block.
20///
21/// To read the data: find the block identified by [`block_id`](ChunkAddress::block_id),
22/// seek to [`offset`](ChunkAddress::offset) bytes into the decompressed block,
23/// and read [`size`](ChunkAddress::size) bytes.
24#[derive(Debug, Clone, PartialEq, Archive, Serialize, Deserialize)]
25pub struct ChunkAddress {
26    /// Which block the data resides in.
27    pub block_id: BlockId,
28    /// Byte offset within the decompressed block.
29    pub offset: u32,
30    /// Number of bytes to read from the block.
31    pub size: u32,
32}
33
34impl BlockAllocAddress {
35    pub fn new(id: BlockId, offset: u64, size: u64) -> Self {
36        Self { id, offset, size }
37    }
38
39    pub fn id(&self) -> BlockId {
40        self.id
41    }
42
43    pub fn offset(&self) -> u64 {
44        self.offset
45    }
46
47    pub fn size(&self) -> u64 {
48        self.size
49    }
50}
51
52impl From<BlockAllocAddress> for ChunkAddress {
53    fn from(a: BlockAllocAddress) -> Self {
54        ChunkAddress {
55            block_id: a.id,
56            offset: a.offset as u32,
57            size: a.size as u32,
58        }
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn block_id_copy() {
68        let a = BlockId(7);
69        let b = a;
70        assert_eq!(a, b);
71    }
72
73    #[test]
74    fn chunk_address_fields() {
75        let addr = ChunkAddress {
76            block_id: BlockId(3),
77            offset: 1024,
78            size: 4096,
79        };
80        assert_eq!(addr.block_id, BlockId(3));
81        assert_eq!(addr.offset, 1024);
82        assert_eq!(addr.size, 4096);
83    }
84}