Skip to contentSkip to conversation

refactor!(message): decouple TpmHeader from the build trait

PATCH In review Jarkko Sakkinen 1 participant 2 messages Aug 31, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 refactor!(message): decouple TpmHeader from the build trait

In order to make the command build trait more symmetrical with the parse trait, decouple TpmHeader from the trait. Rename the trait as TpmCommandBuild.

---
I think I'll backport this too to the 0.10.x as it by pracical means does not affect the consumed API and user volumes are still low.

 src/macro/struct.rs  | 6 +++---
 src/message/build.rs | 4 ++--
 src/message/mod.rs   | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)
src/macro/struct.rs +3 -3
index 01e2cfb..ad5303f 100644
@@ -36,12 +36,12 @@ macro_rules! tpm_struct {
         impl $crate::TpmBuild for $name {
             #[allow(unused_variables)]
             fn build(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
-                <Self as $crate::message::TpmHeaderCommand>::build_handles(self, writer)?;
-                <Self as $crate::message::TpmHeaderCommand>::build_parameters(self, writer)
+                <Self as $crate::message::TpmCommandBuild>::build_handles(self, writer)?;
+                <Self as $crate::message::TpmCommandBuild>::build_parameters(self, writer)
             }
         }
 
-        impl $crate::message::TpmHeaderCommand for $name {
+        impl $crate::message::TpmCommandBuild for $name {
             #[allow(unused_variables)]
             fn build_handles(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
                 $($crate::TpmBuild::build(&self.$handle_field, writer)?;)*
src/message/build.rs +2 -2
index 4170513..41a7fee 100644
@@ -4,7 +4,7 @@
 
 use crate::{
     data::{TpmRc, TpmSt, TpmsAuthCommand, TpmsAuthResponse},
-    message::{TpmHeader, TpmHeaderCommand, TPM_HEADER_SIZE},
+    message::{TpmCommandBuild, TpmHeader, TPM_HEADER_SIZE},
     TpmBuild, TpmErrorKind, TpmResult, TpmSized,
 };
 use core::mem::size_of;
@@ -21,7 +21,7 @@ pub fn tpm_build_command<C>(
     writer: &mut crate::TpmWriter,
 ) -> TpmResult<()>
 where
-    C: TpmHeaderCommand,
+    C: TpmHeader + TpmCommandBuild,
 {
     match tag {
         TpmSt::NoSessions => {
src/message/mod.rs +1 -1
index eb0d3f0..2d02361 100644
@@ -60,7 +60,7 @@ pub trait TpmHeader: TpmBuild + Debug {
 }
 
 /// A trait for building command bodies in separate handle and parameter sections.
-pub trait TpmHeaderCommand: TpmHeader {
+pub trait TpmCommandBuild {
     /// Builds the handle area of the command.
     ///
     /// # Errors
Signature · 1 line
2.39.5
#1 lore raw
Quoted 71 lines · Jarkko Sakkinen

In order to make the command build trait more symmetrical with the parse trait, decouple TpmHeader from the trait. Rename the trait as TpmCommandBuild.

---
I think I'll backport this too to the 0.10.x as it by pracical means does not affect the consumed API and user volumes are still low.

 src/macro/struct.rs  | 6 +++---
 src/message/build.rs | 4 ++--
 src/message/mod.rs   | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)
src/macro/struct.rs +3 -3
index 01e2cfb..ad5303f 100644
@@ -36,12 +36,12 @@ macro_rules! tpm_struct {
         impl $crate::TpmBuild for $name {
             #[allow(unused_variables)]
             fn build(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
-                <Self as $crate::message::TpmHeaderCommand>::build_handles(self, writer)?;
-                <Self as $crate::message::TpmHeaderCommand>::build_parameters(self, writer)
+                <Self as $crate::message::TpmCommandBuild>::build_handles(self, writer)?;
+                <Self as $crate::message::TpmCommandBuild>::build_parameters(self, writer)
             }
         }
 
-        impl $crate::message::TpmHeaderCommand for $name {
+        impl $crate::message::TpmCommandBuild for $name {
             #[allow(unused_variables)]
             fn build_handles(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
                 $($crate::TpmBuild::build(&self.$handle_field, writer)?;)*
src/message/build.rs +2 -2
index 4170513..41a7fee 100644
@@ -4,7 +4,7 @@
 
 use crate::{
     data::{TpmRc, TpmSt, TpmsAuthCommand, TpmsAuthResponse},
-    message::{TpmHeader, TpmHeaderCommand, TPM_HEADER_SIZE},
+    message::{TpmCommandBuild, TpmHeader, TPM_HEADER_SIZE},
     TpmBuild, TpmErrorKind, TpmResult, TpmSized,
 };
 use core::mem::size_of;
@@ -21,7 +21,7 @@ pub fn tpm_build_command<C>(
     writer: &mut crate::TpmWriter,
 ) -> TpmResult<()>
 where
-    C: TpmHeaderCommand,
+    C: TpmHeader + TpmCommandBuild,
 {
     match tag {
         TpmSt::NoSessions => {
src/message/mod.rs +1 -1
index eb0d3f0..2d02361 100644
@@ -60,7 +60,7 @@ pub trait TpmHeader: TpmBuild + Debug {
 }
 
 /// A trait for building command bodies in separate handle and parameter sections.
-pub trait TpmHeaderCommand: TpmHeader {
+pub trait TpmCommandBuild {
     /// Builds the handle area of the command.
     ///
     /// # Errors
Signature · 1 line
2.39.5

This also applied (last remaiming low-hanging fruit item in my backlog). It was also backported to 0.10.x given low incompatibility risk factor.

Where main branch API wise now diverges is TpmRc (i.e. no longer is wrapped into a result structure).

BR, Jarkko