tpm2_protocol/macro/
integer.rs1#[macro_export]
6macro_rules! tpm_integer {
7 ($ty:ty, $variant:ident) => {
8 impl TpmParse for $ty {
9 fn parse(buf: &[u8]) -> TpmResult<(Self, &[u8])> {
10 let size = size_of::<$ty>();
11 if buf.len() < size {
12 return Err(TpmErrorKind::Boundary);
13 }
14 let (bytes, buf) = buf.split_at(size);
15 let array = bytes.try_into().map_err(|_| TpmErrorKind::Unreachable)?;
16 let val = <$ty>::from_be_bytes(array);
17 Ok((val, buf))
18 }
19 }
20
21 impl TpmBuild for $ty {
22 fn build(&self, writer: &mut TpmWriter) -> TpmResult<()> {
23 writer.write_bytes(&self.to_be_bytes())
24 }
25 }
26
27 impl TpmSized for $ty {
28 const SIZE: usize = size_of::<$ty>();
29 fn len(&self) -> usize {
30 Self::SIZE
31 }
32 }
33
34 impl core::convert::From<$ty> for TpmNotDiscriminant {
35 fn from(value: $ty) -> Self {
36 Self::$variant(value.into())
37 }
38 }
39 };
40}