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,
15}
16
17impl BlockLocation {
18 pub fn new(offset: u64, size: usize) -> Result<Self, InvalidBlockLocation> {
20 if !offset.is_multiple_of(512)
21 || !(512..1 << 31).contains(&size)
22 || !size.is_multiple_of(512)
23 {
24 Err(InvalidBlockLocation { offset, size })
25 } else {
26 Ok(Self { offset, size })
27 }
28 }
29
30 pub fn after(&self) -> u64 {
32 self.offset + self.size as u64
33 }
34}
35
36impl Display for BlockLocation {
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 write!(f, "{} bytes at offset {}", self.size, self.offset)
39 }
40}
41
42#[derive(Copy, Clone, Debug)]
45pub struct InvalidBlockLocation {
46 pub offset: u64,
48
49 pub size: usize,
51}
52
53impl Display for InvalidBlockLocation {
54 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 write!(f, "{} bytes at offset {}", self.size, self.offset)
56 }
57}