@@ -44,12 +44,18 @@ void appendDirectCodeMethods(
44
44
final String methodName ,
45
45
final String representationType ,
46
46
final String nextCoderType ,
47
- final int numBytes ) throws IOException
47
+ final int numBytes ,
48
+ final int trailingBytes ) throws IOException
48
49
{
49
50
indent (appendable , 1 , "pub fn %s(mut self) -> CodecResult<(&%s %s, %s)> {\n " ,
50
51
methodName , DATA_LIFETIME , representationType , RustGenerator .withLifetime (nextCoderType ));
51
52
indent (appendable , 2 , "let v = self.%s.read_type::<%s>(%s)?;\n " ,
52
53
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
+ }
53
59
indent (appendable , 2 , "Ok((v, %s::wrap(self.%s)))\n " ,
54
60
nextCoderType , RustCodecType .Decoder .scratchProperty ());
55
61
indent (appendable ).append ("}\n " );
@@ -78,15 +84,20 @@ void appendDirectCodeMethods(
78
84
final String methodName ,
79
85
final String representationType ,
80
86
final String nextCoderType ,
81
- final int numBytes ) throws IOException
87
+ final int numBytes ,
88
+ final int trailingBytes ) throws IOException
82
89
{
83
90
indent (appendable , 1 , "\n /// Create a mutable struct reference overlaid atop the data buffer\n " );
84
91
indent (appendable , 1 , "/// such that changes to the struct directly edit the buffer. \n " );
85
92
indent (appendable , 1 , "/// Note that the initial content of the struct's fields may be garbage.\n " );
86
93
indent (appendable , 1 , "pub fn %s(mut self) -> CodecResult<(&%s mut %s, %s)> {\n " ,
87
94
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 );
90
101
indent (appendable , 2 , "Ok((v, %s::wrap(self.%s)))\n " ,
91
102
nextCoderType , RustCodecType .Encoder .scratchProperty ());
92
103
indent (appendable ).append ("}\n \n " );
@@ -97,6 +108,12 @@ void appendDirectCodeMethods(
97
108
indent (appendable , 2 )
98
109
.append (format ("self.%s.write_type::<%s>(t, %s)?;\n " ,
99
110
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
+ }
100
117
indent (appendable , 2 ).append (format ("Ok(%s::wrap(self.%s))\n " ,
101
118
nextCoderType , RustCodecType .Encoder .scratchProperty ()));
102
119
indent (appendable ).append ("}\n " );
@@ -125,7 +142,8 @@ abstract void appendDirectCodeMethods(
125
142
String methodName ,
126
143
String representationType ,
127
144
String nextCoderType ,
128
- int numBytes ) throws IOException ;
145
+ int numBytes ,
146
+ int trailingBytes ) throws IOException ;
129
147
130
148
abstract String gerund ();
131
149
@@ -175,7 +193,7 @@ String generateMessageHeaderCoder(
175
193
appendScratchWrappingStruct (writer , headerCoderType );
176
194
RustGenerator .appendImplWithLifetimeHeader (writer , headerCoderType );
177
195
appendWrapMethod (writer , headerCoderType );
178
- appendDirectCodeMethods (writer , "header" , messageHeaderRepresentation , topType , headerSize );
196
+ appendDirectCodeMethods (writer , "header" , messageHeaderRepresentation , topType , headerSize , 0 );
179
197
writer .append ("}\n " );
180
198
}
181
199
0 commit comments