@@ -44,12 +44,18 @@ void appendDirectCodeMethods(
4444 final String methodName ,
4545 final String representationType ,
4646 final String nextCoderType ,
47- final int numBytes ) throws IOException
47+ final int numBytes ,
48+ final int trailingBytes ) throws IOException
4849 {
4950 indent (appendable , 1 , "pub fn %s(mut self) -> CodecResult<(&%s %s, %s)> {\n " ,
5051 methodName , DATA_LIFETIME , representationType , RustGenerator .withLifetime (nextCoderType ));
5152 indent (appendable , 2 , "let v = self.%s.read_type::<%s>(%s)?;\n " ,
5253 RustCodecType .Decoder .scratchProperty (), representationType , numBytes );
54+ if (trailingBytes > 0 )
55+ {
56+ indent (appendable , 2 , "self.%s.skip_bytes(%s)?;\n " ,
57+ RustCodecType .Decoder .scratchProperty (), trailingBytes );
58+ }
5359 indent (appendable , 2 , "Ok((v, %s::wrap(self.%s)))\n " ,
5460 nextCoderType , RustCodecType .Decoder .scratchProperty ());
5561 indent (appendable ).append ("}\n " );
@@ -78,15 +84,20 @@ void appendDirectCodeMethods(
7884 final String methodName ,
7985 final String representationType ,
8086 final String nextCoderType ,
81- final int numBytes ) throws IOException
87+ final int numBytes ,
88+ final int trailingBytes ) throws IOException
8289 {
8390 indent (appendable , 1 , "\n /// Create a mutable struct reference overlaid atop the data buffer\n " );
8491 indent (appendable , 1 , "/// such that changes to the struct directly edit the buffer. \n " );
8592 indent (appendable , 1 , "/// Note that the initial content of the struct's fields may be garbage.\n " );
8693 indent (appendable , 1 , "pub fn %s(mut self) -> CodecResult<(&%s mut %s, %s)> {\n " ,
8794 methodName , DATA_LIFETIME , representationType , RustGenerator .withLifetime (nextCoderType ));
88- indent (appendable , 2 , "let v = self.%s.writable_overlay::<%s>(%s)?;\n " ,
89- RustCodecType .Encoder .scratchProperty (), representationType , numBytes );
95+ if (trailingBytes > 0 )
96+ {
97+ indent (appendable , 2 , "// add trailing bytes to extend the end position of the scratch buffer\n " );
98+ }
99+ indent (appendable , 2 , "let v = self.%s.writable_overlay::<%s>(%s+%s)?;\n " ,
100+ RustCodecType .Encoder .scratchProperty (), representationType , numBytes , trailingBytes );
90101 indent (appendable , 2 , "Ok((v, %s::wrap(self.%s)))\n " ,
91102 nextCoderType , RustCodecType .Encoder .scratchProperty ());
92103 indent (appendable ).append ("}\n \n " );
@@ -97,6 +108,12 @@ void appendDirectCodeMethods(
97108 indent (appendable , 2 )
98109 .append (format ("self.%s.write_type::<%s>(t, %s)?;\n " ,
99110 RustCodecType .Encoder .scratchProperty (), representationType , numBytes ));
111+ if (trailingBytes > 0 )
112+ {
113+ indent (appendable , 2 , "// fixed message length > sum of field lengths\n " );
114+ indent (appendable , 2 , "self.%s.skip_bytes(%s)?;\n " ,
115+ RustCodecType .Decoder .scratchProperty (), trailingBytes );
116+ }
100117 indent (appendable , 2 ).append (format ("Ok(%s::wrap(self.%s))\n " ,
101118 nextCoderType , RustCodecType .Encoder .scratchProperty ()));
102119 indent (appendable ).append ("}\n " );
@@ -125,7 +142,8 @@ abstract void appendDirectCodeMethods(
125142 String methodName ,
126143 String representationType ,
127144 String nextCoderType ,
128- int numBytes ) throws IOException ;
145+ int numBytes ,
146+ int trailingBytes ) throws IOException ;
129147
130148 abstract String gerund ();
131149
@@ -175,7 +193,7 @@ String generateMessageHeaderCoder(
175193 appendScratchWrappingStruct (writer , headerCoderType );
176194 RustGenerator .appendImplWithLifetimeHeader (writer , headerCoderType );
177195 appendWrapMethod (writer , headerCoderType );
178- appendDirectCodeMethods (writer , "header" , messageHeaderRepresentation , topType , headerSize );
196+ appendDirectCodeMethods (writer , "header" , messageHeaderRepresentation , topType , headerSize , 0 );
179197 writer .append ("}\n " );
180198 }
181199
0 commit comments