use crate::MsgDescHot;
#[derive(Debug, Clone)]
pub struct Frame {
pub desc: MsgDescHot,
pub payload: Option<Vec<u8>>,
}
impl Frame {
pub fn new(desc: MsgDescHot) -> Self {
Self {
desc,
payload: None,
}
}
pub fn with_inline_payload(mut desc: MsgDescHot, payload: &[u8]) -> Option<Self> {
if payload.len() > crate::INLINE_PAYLOAD_SIZE {
return None;
}
desc.payload_slot = crate::INLINE_PAYLOAD_SLOT;
desc.payload_generation = 0;
desc.payload_offset = 0;
desc.payload_len = payload.len() as u32;
desc.inline_payload[..payload.len()].copy_from_slice(payload);
Some(Self {
desc,
payload: None,
})
}
pub fn with_payload(mut desc: MsgDescHot, payload: Vec<u8>) -> Self {
desc.payload_slot = 0;
desc.payload_len = payload.len() as u32;
Self {
desc,
payload: Some(payload),
}
}
pub fn payload(&self) -> &[u8] {
if self.desc.is_inline() {
self.desc.inline_payload()
} else {
self.payload.as_deref().unwrap_or(&[])
}
}
}
#[derive(Debug)]
pub struct FrameView<'a> {
pub desc: &'a MsgDescHot,
pub payload: &'a [u8],
}
impl<'a> FrameView<'a> {
pub fn new(desc: &'a MsgDescHot, payload: &'a [u8]) -> Self {
Self { desc, payload }
}
pub fn to_owned(&self) -> Frame {
if self.desc.is_inline() {
Frame {
desc: *self.desc,
payload: None,
}
} else {
Frame {
desc: *self.desc,
payload: Some(self.payload.to_vec()),
}
}
}
}