use crate::{Ecc, Result};
use byteorder::{ByteOrder, ReadBytesExt, WriteBytesExt};
use std::{
fmt::Debug,
io::{Read, Write},
};
#[repr(C, align(16))]
#[derive(Copy, Clone, PartialEq, Hash)]
pub struct Chunk {
primary: Ecc,
secondary: Ecc,
length: u64,
offset: u64,
}
impl Debug for Chunk {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"({}:{}) - {}, {}",
self.primary.to_string(),
self.secondary.to_string(),
self.length,
self.offset
)
}
}
impl Chunk {
pub const SIZE: usize = std::mem::size_of::<Self>();
pub fn new(
primary: impl Into<Ecc>,
secondary: impl Into<Ecc>,
length: u64,
offset: u64,
) -> Self {
Self {
primary: primary.into(),
secondary: secondary.into(),
length,
offset,
}
}
pub fn primary(&self) -> Ecc {
self.primary
}
pub fn secondary(&self) -> Ecc {
self.secondary
}
pub fn length(&self) -> u64 {
self.length
}
pub fn length_mut(&mut self) -> &mut u64 {
&mut self.length
}
pub fn offset(&self) -> u64 {
self.offset
}
pub fn offset_mut(&mut self) -> &mut u64 {
&mut self.offset
}
pub fn read<E: ByteOrder>(reader: &mut dyn Read) -> Result<Self> {
Ok(Self {
primary: Ecc::read::<E>(reader)?,
secondary: Ecc::read::<E>(reader)?,
length: reader.read_u64::<E>()?,
offset: reader.read_u64::<E>()?,
})
}
pub fn write<E: ByteOrder>(self, writer: &mut dyn Write) -> Result<()> {
self.primary.write::<E>(writer)?;
self.secondary.write::<E>(writer)?;
writer.write_u64::<E>(self.length)?;
writer.write_u64::<E>(self.offset)?;
Ok(())
}
}