1use 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Archive, Serialize, Deserialize)]
17pub struct BlockId(pub u32);
18
19#[derive(Debug, Clone, PartialEq, Archive, Serialize, Deserialize)]
25pub struct ChunkAddress {
26 pub block_id: BlockId,
28 pub offset: u32,
30 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}