use size_of::SizeOf;
use std::fmt::Display;
#[derive(Copy, Clone, Debug, SizeOf)]
pub struct BlockLocation {
pub offset: u64,
pub size: usize,
}
impl BlockLocation {
pub fn new(offset: u64, size: usize) -> Result<Self, InvalidBlockLocation> {
if !offset.is_multiple_of(512) || !size.is_multiple_of(512) {
Err(InvalidBlockLocation { offset, size })
} else {
Ok(Self { offset, size })
}
}
pub fn after(&self) -> u64 {
self.offset + self.size as u64
}
}
impl Display for BlockLocation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} bytes at offset {}", self.size, self.offset)
}
}
#[derive(Copy, Clone, Debug)]
pub struct InvalidBlockLocation {
pub offset: u64,
pub size: usize,
}
impl Display for InvalidBlockLocation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} bytes at offset {}", self.size, self.offset)
}
}