use crate::codec::{Codec, StdError, WithOffset, WithSize};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RequestTag {
pub eop: bool,
pub id: u8,
}
impl std::fmt::Display for RequestTag {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "[{}]({})", if self.eop { "E" } else { "-" }, self.id)
}
}
impl Codec for RequestTag {
type Error = StdError;
fn encoded_size(&self) -> usize {
1 + 1
}
unsafe fn encode_in(&self, out: &mut [u8]) -> usize {
out[0] |= (self.eop as u8) << 7;
out[1] = self.id;
1 + 1
}
fn decode(out: &[u8]) -> Result<WithSize<Self>, WithOffset<Self::Error>> {
let min_size = 1 + 1;
if out.len() < min_size {
return Err(WithOffset::new(
0,
Self::Error::MissingBytes(min_size - out.len()),
));
}
Ok(WithSize {
value: Self {
eop: out[0] & 0x80 != 0,
id: out[1],
},
size: 2,
})
}
}