use rkyv::{Archive, Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, Archive, Serialize, Deserialize)]
pub(crate) struct BlockAllocAddress {
id: BlockId,
offset: u64,
size: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Archive, Serialize, Deserialize)]
pub struct BlockId(pub u32);
#[derive(Debug, Clone, PartialEq, Archive, Serialize, Deserialize)]
pub struct ChunkAddress {
pub block_id: BlockId,
pub offset: u32,
pub size: u32,
}
impl BlockAllocAddress {
pub(crate) fn new(id: BlockId, offset: u64, size: u64) -> Self {
Self { id, offset, size }
}
pub(crate) fn id(&self) -> BlockId {
self.id
}
pub(crate) fn offset(&self) -> u64 {
self.offset
}
pub(crate) fn size(&self) -> u64 {
self.size
}
}
impl From<BlockAllocAddress> for ChunkAddress {
fn from(a: BlockAllocAddress) -> Self {
ChunkAddress {
block_id: a.id,
offset: a.offset as u32,
size: a.size as u32,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn block_id_copy() {
let a = BlockId(7);
let b = a;
assert_eq!(a, b);
}
#[test]
fn chunk_address_fields() {
let addr = ChunkAddress {
block_id: BlockId(3),
offset: 1024,
size: 4096,
};
assert_eq!(addr.block_id, BlockId(3));
assert_eq!(addr.offset, 1024);
assert_eq!(addr.size, 4096);
}
}