#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(C)]
pub struct DiskLoc {
pub offset: u32,
pub len: u32,
pub file_id: u32,
pub shard_id: u8,
_pad: [u8; 3],
}
impl DiskLoc {
#[inline]
pub fn new(shard_id: u8, file_id: u32, offset: u32, len: u32) -> Self {
Self {
offset,
len,
file_id,
shard_id,
_pad: [0; 3],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::mem::{align_of, size_of};
#[test]
fn disk_loc_size_is_16() {
assert_eq!(size_of::<DiskLoc>(), 16);
}
#[test]
fn disk_loc_alignment_is_4() {
assert_eq!(align_of::<DiskLoc>(), 4);
}
#[test]
fn disk_loc_new_zeroes_padding() {
let loc = DiskLoc::new(1, 0xDEAD_BEEF, 0x1234_5678, 0x9ABC_DEF0);
let bytes: [u8; 16] = unsafe { std::mem::transmute(loc) };
assert_eq!(&bytes[13..16], &[0u8, 0u8, 0u8]);
}
#[test]
fn disk_loc_roundtrip_large_file_id() {
for &fid in &[0u32, 1, 65535, 65536, 65537, 1_000_000, u32::MAX] {
let loc = DiskLoc::new(7, fid, 4096, 1024);
assert_eq!(loc.file_id, fid);
assert_eq!(loc.shard_id, 7);
assert_eq!(loc.offset, 4096);
assert_eq!(loc.len, 1024);
}
}
}