Skip to content

Commit 47e4914

Browse files
authored
[Rust] Elide explicit ActingVersion lifetime. (#1053)
1 parent 048e387 commit 47e4914

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/MessageCoderDef.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
import static uk.co.real_logic.sbe.generation.rust.RustGenerator.CodecType.Encoder;
2828
import static uk.co.real_logic.sbe.generation.rust.RustGenerator.CodecType.Decoder;
29-
import static uk.co.real_logic.sbe.generation.rust.RustGenerator.withLifetime;
29+
import static uk.co.real_logic.sbe.generation.rust.RustGenerator.withBufLifetime;
3030
import static uk.co.real_logic.sbe.generation.rust.RustUtil.*;
3131

3232
class MessageCoderDef implements RustGenerator.ParentDef
@@ -165,8 +165,8 @@ void appendMessageStruct(final Appendable out, final String structName) throws I
165165
indent(out, 1, "#[derive(Debug, Default)]\n");
166166
}
167167

168-
indent(out, 1, "pub struct %s {\n", withLifetime(structName));
169-
indent(out, 2, "buf: %s,\n", withLifetime(this.codecType.bufType()));
168+
indent(out, 1, "pub struct %s {\n", withBufLifetime(structName));
169+
indent(out, 2, "buf: %s,\n", withBufLifetime(this.codecType.bufType()));
170170
indent(out, 2, "initial_offset: usize,\n");
171171
indent(out, 2, "offset: usize,\n");
172172
indent(out, 2, "limit: usize,\n");
@@ -184,7 +184,7 @@ void appendWrapFn(final Appendable out) throws IOException
184184
{
185185
indent(out, 2, "pub fn wrap(\n");
186186
indent(out, 3, "mut self,\n");
187-
indent(out, 3, "buf: %s,\n", withLifetime(this.codecType.bufType()));
187+
indent(out, 3, "buf: %s,\n", withBufLifetime(this.codecType.bufType()));
188188
indent(out, 3, "offset: usize,\n");
189189
indent(out, 3, "acting_block_length: %s,\n", blockLengthType());
190190
indent(out, 3, "acting_version: %s,\n", schemaVersionType());
@@ -194,7 +194,7 @@ void appendWrapFn(final Appendable out) throws IOException
194194
else
195195
{
196196
indent(out, 2, "pub fn wrap(mut self, buf: %s, offset: usize) -> Self {\n",
197-
withLifetime(this.codecType.bufType()));
197+
withBufLifetime(this.codecType.bufType()));
198198
indent(out, 3, "let limit = offset + SBE_BLOCK_LENGTH as usize;\n");
199199
}
200200

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class RustGenerator implements CodeGenerator
5050
{
5151
static final String WRITE_BUF_TYPE = "WriteBuf";
5252
static final String READ_BUF_TYPE = "ReadBuf";
53+
static final String ANONYMOUS_LIFETIME = "'_";
5354
static final String BUF_LIFETIME = "'a";
5455

5556
enum CodecType
@@ -187,9 +188,19 @@ String schemaVersionType()
187188
return rustTypeName(ir.headerStructure().schemaVersionType());
188189
}
189190

190-
static String withLifetime(final String typeName)
191+
static String withAnonymousLifetime(final String typeName)
191192
{
192-
return format("%s<%s>", typeName, BUF_LIFETIME);
193+
return withLifetime(typeName, ANONYMOUS_LIFETIME);
194+
}
195+
196+
static String withBufLifetime(final String typeName)
197+
{
198+
return withLifetime(typeName, BUF_LIFETIME);
199+
}
200+
201+
private static String withLifetime(final String typeName, final String lifetime)
202+
{
203+
return format("%s<%s>", typeName, lifetime);
193204
}
194205

195206
static void appendImplWithLifetimeHeader(
@@ -1243,14 +1254,14 @@ static void appendImplEncoderTrait(
12431254
final Appendable out,
12441255
final String typeName) throws IOException
12451256
{
1246-
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withLifetime("Writer"), withLifetime(typeName));
1257+
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withBufLifetime("Writer"), withBufLifetime(typeName));
12471258
indent(out, 2, "#[inline]\n");
12481259
indent(out, 2, "fn get_buf_mut(&mut self) -> &mut WriteBuf<'a> {\n");
12491260
indent(out, 3, "&mut self.buf\n");
12501261
indent(out, 2, "}\n");
12511262
indent(out, 1, "}\n\n");
12521263

1253-
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withLifetime("Encoder"), withLifetime(typeName));
1264+
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withBufLifetime("Encoder"), withBufLifetime(typeName));
12541265
indent(out, 2, "#[inline]\n");
12551266
indent(out, 2, "fn get_limit(&self) -> usize {\n");
12561267
indent(out, 3, "self.limit\n");
@@ -1268,21 +1279,21 @@ static void appendImplDecoderTrait(
12681279
final Appendable out,
12691280
final String typeName) throws IOException
12701281
{
1271-
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, "ActingVersion", withLifetime(typeName));
1282+
indent(out, 1, "impl %s for %s {\n", "ActingVersion", withAnonymousLifetime(typeName));
12721283
indent(out, 2, "#[inline]\n");
12731284
indent(out, 2, "fn acting_version(&self) -> %s {\n", schemaVersionType);
12741285
indent(out, 3, "self.acting_version\n");
12751286
indent(out, 2, "}\n");
12761287
indent(out, 1, "}\n\n");
12771288

1278-
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withLifetime("Reader"), withLifetime(typeName));
1289+
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withBufLifetime("Reader"), withBufLifetime(typeName));
12791290
indent(out, 2, "#[inline]\n");
12801291
indent(out, 2, "fn get_buf(&self) -> &ReadBuf<'a> {\n");
12811292
indent(out, 3, "&self.buf\n");
12821293
indent(out, 2, "}\n");
12831294
indent(out, 1, "}\n\n");
12841295

1285-
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withLifetime("Decoder"), withLifetime(typeName));
1296+
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withBufLifetime("Decoder"), withBufLifetime(typeName));
12861297
indent(out, 2, "#[inline]\n");
12871298
indent(out, 2, "fn get_limit(&self) -> usize {\n");
12881299
indent(out, 3, "self.limit\n");

0 commit comments

Comments
 (0)