use std::ops::{Add, Sub};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct Block(pub u64);
impl Block {
pub fn to_bytes(&self, sector_size: u16) -> u64 {
let sector_size = sector_size as u64;
self.0 * sector_size
}
pub fn from_bytes(bytes: u64, sector_size: u16) -> Option<Block> {
let sector_size = sector_size as u64;
if bytes % sector_size == 0 {
Some(Block(bytes / 512))
} else {
None
}
}
pub fn from_bytes_offset(bytes: u64, sector_size: u16) -> (Block, u16) {
let sector_size = sector_size as u64;
(Block(bytes / sector_size), (bytes % sector_size) as u16)
}
}
impl Add for Block {
type Output = Block;
fn add(self, other: Block) -> Block {
Block(self.0 + other.0)
}
}
impl Sub for Block {
type Output = Block;
fn sub(self, other: Block) -> Block {
Block(self.0 - other.0)
}
}