tpm2_protocol/macro/
integer.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// Copyright (c) 2025 Opinsys Oy
3// Copyright (c) 2024-2025 Jarkko Sakkinen
4
5#[macro_export]
6macro_rules! tpm_integer {
7    ($ty:ty) => {
8        impl TpmUnmarshal for $ty {
9            fn unmarshal(buf: &[u8]) -> TpmResult<(Self, &[u8])> {
10                let size = size_of::<$ty>();
11                let bytes = buf.get(..size).ok_or(TpmProtocolError::UnexpectedEnd)?;
12                let array = bytes
13                    .try_into()
14                    .map_err(|_| TpmProtocolError::OperationFailed)?;
15                let val = <$ty>::from_be_bytes(array);
16                Ok((val, &buf[size..]))
17            }
18        }
19
20        impl TpmMarshal for $ty {
21            fn marshal(&self, writer: &mut TpmWriter) -> TpmResult<()> {
22                writer.write_bytes(&self.to_be_bytes())
23            }
24        }
25
26        impl TpmSized for $ty {
27            const SIZE: usize = size_of::<$ty>();
28            fn len(&self) -> usize {
29                Self::SIZE
30            }
31        }
32    };
33}