1use size_of::SizeOf;
2use std::fmt::Display;
3
4#[derive(Copy, Clone, Debug, SizeOf)]
6pub struct BlockLocation {
7 pub offset: u64,
9
10 pub size: usize,
12}
13
14impl BlockLocation {
15 pub fn new(offset: u64, size: usize) -> Result<Self, InvalidBlockLocation> {
17 if !offset.is_multiple_of(512) || !size.is_multiple_of(512) {
18 Err(InvalidBlockLocation { offset, size })
19 } else {
20 Ok(Self { offset, size })
21 }
22 }
23
24 pub fn after(&self) -> u64 {
26 self.offset + self.size as u64
27 }
28}
29
30impl Display for BlockLocation {
31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32 write!(f, "{} bytes at offset {}", self.size, self.offset)
33 }
34}
35
36#[derive(Copy, Clone, Debug)]
39pub struct InvalidBlockLocation {
40 pub offset: u64,
42
43 pub size: usize,
45}
46
47impl Display for InvalidBlockLocation {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 write!(f, "{} bytes at offset {}", self.size, self.offset)
50 }
51}