Skip to content

Commit 68a0a5b

Browse files
gierlachgmarciorasf-ab
authored andcommitted
[Rust] Elide explicit ActingVersion lifetime. (aeron-io#1053)
1 parent df2224e commit 68a0a5b

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

Lines changed: 5 additions & 5 deletions
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
@@ -171,8 +171,8 @@ void appendMessageStruct(final Appendable out, final String structName) throws I
171171
indent(out, 1, "#[derive(Debug, Default)]\n");
172172
}
173173

174-
indent(out, 1, "pub struct %s {\n", withLifetime(structName));
175-
indent(out, 2, "buf: %s,\n", withLifetime(this.codecType.bufType()));
174+
indent(out, 1, "pub struct %s {\n", withBufLifetime(structName));
175+
indent(out, 2, "buf: %s,\n", withBufLifetime(this.codecType.bufType()));
176176
indent(out, 2, "initial_offset: usize,\n");
177177
indent(out, 2, "offset: usize,\n");
178178
indent(out, 2, "limit: usize,\n");
@@ -190,7 +190,7 @@ void appendWrapFn(final Appendable out) throws IOException
190190
{
191191
indent(out, 2, "pub fn wrap(\n");
192192
indent(out, 3, "mut self,\n");
193-
indent(out, 3, "buf: %s,\n", withLifetime(this.codecType.bufType()));
193+
indent(out, 3, "buf: %s,\n", withBufLifetime(this.codecType.bufType()));
194194
indent(out, 3, "offset: usize,\n");
195195
indent(out, 3, "acting_block_length: %s,\n", blockLengthType());
196196
indent(out, 3, "acting_version: %s,\n", schemaVersionType());
@@ -200,7 +200,7 @@ void appendWrapFn(final Appendable out) throws IOException
200200
else
201201
{
202202
indent(out, 2, "pub fn wrap(mut self, buf: %s, offset: usize) -> Self {\n",
203-
withLifetime(this.codecType.bufType()));
203+
withBufLifetime(this.codecType.bufType()));
204204
indent(out, 3, "let limit = offset + SBE_BLOCK_LENGTH as usize;\n");
205205
}
206206

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class RustGenerator implements CodeGenerator
5252
{
5353
static final String WRITE_BUF_TYPE = "WriteBuf";
5454
static final String READ_BUF_TYPE = "ReadBuf";
55+
static final String ANONYMOUS_LIFETIME = "'_";
5556
static final String BUF_LIFETIME = "'a";
5657

5758
enum CodecType
@@ -189,9 +190,19 @@ String schemaVersionType()
189190
return rustTypeName(ir.headerStructure().schemaVersionType());
190191
}
191192

192-
static String withLifetime(final String typeName)
193+
static String withAnonymousLifetime(final String typeName)
193194
{
194-
return format("%s<%s>", typeName, BUF_LIFETIME);
195+
return withLifetime(typeName, ANONYMOUS_LIFETIME);
196+
}
197+
198+
static String withBufLifetime(final String typeName)
199+
{
200+
return withLifetime(typeName, BUF_LIFETIME);
201+
}
202+
203+
private static String withLifetime(final String typeName, final String lifetime)
204+
{
205+
return format("%s<%s>", typeName, lifetime);
195206
}
196207

197208
static void appendImplWithLifetimeHeader(
@@ -1281,14 +1292,14 @@ static void appendImplEncoderTrait(
12811292
final Appendable out,
12821293
final String typeName) throws IOException
12831294
{
1284-
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withLifetime("Writer"), withLifetime(typeName));
1295+
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withBufLifetime("Writer"), withBufLifetime(typeName));
12851296
indent(out, 2, "#[inline]\n");
12861297
indent(out, 2, "fn get_buf_mut(&mut self) -> &mut WriteBuf<'a> {\n");
12871298
indent(out, 3, "&mut self.buf\n");
12881299
indent(out, 2, "}\n");
12891300
indent(out, 1, "}\n\n");
12901301

1291-
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withLifetime("Encoder"), withLifetime(typeName));
1302+
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withBufLifetime("Encoder"), withBufLifetime(typeName));
12921303
indent(out, 2, "#[inline]\n");
12931304
indent(out, 2, "fn get_limit(&self) -> usize {\n");
12941305
indent(out, 3, "self.limit\n");
@@ -1306,21 +1317,21 @@ static void appendImplDecoderTrait(
13061317
final Appendable out,
13071318
final String typeName) throws IOException
13081319
{
1309-
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, "ActingVersion", withLifetime(typeName));
1320+
indent(out, 1, "impl %s for %s {\n", "ActingVersion", withAnonymousLifetime(typeName));
13101321
indent(out, 2, "#[inline]\n");
13111322
indent(out, 2, "fn acting_version(&self) -> %s {\n", schemaVersionType);
13121323
indent(out, 3, "self.acting_version\n");
13131324
indent(out, 2, "}\n");
13141325
indent(out, 1, "}\n\n");
13151326

1316-
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withLifetime("Reader"), withLifetime(typeName));
1327+
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withBufLifetime("Reader"), withBufLifetime(typeName));
13171328
indent(out, 2, "#[inline]\n");
13181329
indent(out, 2, "fn get_buf(&self) -> &ReadBuf<'a> {\n");
13191330
indent(out, 3, "&self.buf\n");
13201331
indent(out, 2, "}\n");
13211332
indent(out, 1, "}\n\n");
13221333

1323-
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withLifetime("Decoder"), withLifetime(typeName));
1334+
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withBufLifetime("Decoder"), withBufLifetime(typeName));
13241335
indent(out, 2, "#[inline]\n");
13251336
indent(out, 2, "fn get_limit(&self) -> usize {\n");
13261337
indent(out, 3, "self.limit\n");

0 commit comments

Comments
 (0)