use crate::{
core::{
debug::Annotations,
layout::{Layout, macros::gen_bitrange_const},
view::ViewConversionError,
},
dataplane_path::standard::layout::{HopFieldLayout, InfoFieldLayout},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OneHopPathLayout;
impl OneHopPathLayout {
pub const SIZE_BYTES: usize = InfoFieldLayout::SIZE_BYTES + 2 * HopFieldLayout::SIZE_BYTES;
gen_bitrange_const!(INFO_FIELD, 0, InfoFieldLayout::SIZE_BYTES * 8);
gen_bitrange_const!(
HOP_FIELD_1,
Self::INFO_FIELD.end,
HopFieldLayout::SIZE_BYTES * 8
);
gen_bitrange_const!(
HOP_FIELD_2,
Self::HOP_FIELD_1.end,
HopFieldLayout::SIZE_BYTES * 8
);
gen_bitrange_const!(TOTAL, 0, Self::SIZE_BYTES * 8);
}
impl OneHopPathLayout {
#[inline]
pub fn annotations(&self) -> Annotations {
let mut annotations = Annotations::new();
annotations.extend(InfoFieldLayout.annotations());
annotations.extend(HopFieldLayout.annotations());
annotations.extend(HopFieldLayout.annotations());
annotations
}
}
impl Layout for OneHopPathLayout {
#[inline]
fn size_bytes(&self) -> usize {
Self::SIZE_BYTES
}
}
impl TryFrom<&[u8]> for OneHopPathLayout {
type Error = ViewConversionError;
#[inline]
fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
if buf.len() < Self::SIZE_BYTES {
return Err(ViewConversionError::BufferTooSmall {
at: "OneHopPath",
required: Self::SIZE_BYTES,
actual: buf.len(),
});
}
Ok(Self)
}
}