use crate::Stripe;
use static_assertions::const_assert_eq;
use zerocopy::{
CastError, FromBytes as _,
little_endian::{U16 as U16LE, U32 as U32LE, U64 as U64LE},
};
use zerocopy_derive::*;
#[derive(Copy, Clone, Debug, Hash, IntoBytes, FromBytes, Unaligned, KnownLayout, Immutable)]
#[repr(C, packed)]
pub struct Chunk {
pub length: U64LE,
pub owner: U64LE,
pub stripe_len: U64LE,
pub chunk_type: U64LE,
pub io_align: U32LE,
pub io_width: U32LE,
pub sector_size: U32LE,
pub num_stripes: U16LE,
pub sub_stripes: U16LE,
}
const_assert_eq!(core::mem::size_of::<Chunk>(), 48);
impl Chunk {
pub unsafe fn into_dynamic(
&self,
num_stripes: usize,
) -> Result<&ChunkDynamic, CastError<&[u8], ChunkDynamic>> {
let expected_size =
core::mem::size_of::<Chunk>() + num_stripes * core::mem::size_of::<Stripe>();
let bytes = unsafe {
core::slice::from_raw_parts(self as *const Chunk as *const u8, expected_size)
};
ChunkDynamic::ref_from_prefix_with_elems(bytes, num_stripes).map(|(chunk, _)| chunk)
}
}
#[derive(IntoBytes, FromBytes, Unaligned, KnownLayout, Immutable)]
#[repr(C, packed)]
pub struct ChunkDynamic {
pub length: U64LE,
pub owner: U64LE,
pub stripe_len: U64LE,
pub chunk_type: U64LE,
pub io_align: U32LE,
pub io_width: U32LE,
pub sector_size: U32LE,
pub num_stripes: U16LE,
pub sub_stripes: U16LE,
pub stripe: [Stripe],
}