Skip to content

Commit

Permalink
[Rust] Elide explicit ActingVersion lifetime.
Browse files Browse the repository at this point in the history
  • Loading branch information
gierlachg committed Feb 25, 2025
1 parent ee0888f commit b9e829a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import static uk.co.real_logic.sbe.generation.rust.RustGenerator.CodecType.Encoder;
import static uk.co.real_logic.sbe.generation.rust.RustGenerator.CodecType.Decoder;
import static uk.co.real_logic.sbe.generation.rust.RustGenerator.withLifetime;
import static uk.co.real_logic.sbe.generation.rust.RustGenerator.withBufLifetime;
import static uk.co.real_logic.sbe.generation.rust.RustUtil.*;

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

indent(out, 1, "pub struct %s {\n", withLifetime(structName));
indent(out, 2, "buf: %s,\n", withLifetime(this.codecType.bufType()));
indent(out, 1, "pub struct %s {\n", withBufLifetime(structName));
indent(out, 2, "buf: %s,\n", withBufLifetime(this.codecType.bufType()));
indent(out, 2, "initial_offset: usize,\n");
indent(out, 2, "offset: usize,\n");
indent(out, 2, "limit: usize,\n");
Expand All @@ -184,7 +184,7 @@ void appendWrapFn(final Appendable out) throws IOException
{
indent(out, 2, "pub fn wrap(\n");
indent(out, 3, "mut self,\n");
indent(out, 3, "buf: %s,\n", withLifetime(this.codecType.bufType()));
indent(out, 3, "buf: %s,\n", withBufLifetime(this.codecType.bufType()));
indent(out, 3, "offset: usize,\n");
indent(out, 3, "acting_block_length: %s,\n", blockLengthType());
indent(out, 3, "acting_version: %s,\n", schemaVersionType());
Expand All @@ -194,7 +194,7 @@ void appendWrapFn(final Appendable out) throws IOException
else
{
indent(out, 2, "pub fn wrap(mut self, buf: %s, offset: usize) -> Self {\n",
withLifetime(this.codecType.bufType()));
withBufLifetime(this.codecType.bufType()));
indent(out, 3, "let limit = offset + SBE_BLOCK_LENGTH as usize;\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class RustGenerator implements CodeGenerator
{
static final String WRITE_BUF_TYPE = "WriteBuf";
static final String READ_BUF_TYPE = "ReadBuf";
static final String ANONYMOUS_LIFETIME = "'_";
static final String BUF_LIFETIME = "'a";

enum CodecType
Expand Down Expand Up @@ -187,9 +188,19 @@ String schemaVersionType()
return rustTypeName(ir.headerStructure().schemaVersionType());
}

static String withLifetime(final String typeName)
static String withAnonymousLifetime(final String typeName)
{
return format("%s<%s>", typeName, BUF_LIFETIME);
return withLifetime(typeName, ANONYMOUS_LIFETIME);
}

static String withBufLifetime(final String typeName)
{
return withLifetime(typeName, BUF_LIFETIME);
}

private static String withLifetime(final String typeName, final String lifetime)
{
return format("%s<%s>", typeName, lifetime);
}

static void appendImplWithLifetimeHeader(
Expand Down Expand Up @@ -1243,14 +1254,14 @@ static void appendImplEncoderTrait(
final Appendable out,
final String typeName) throws IOException
{
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withLifetime("Writer"), withLifetime(typeName));
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withBufLifetime("Writer"), withBufLifetime(typeName));
indent(out, 2, "#[inline]\n");
indent(out, 2, "fn get_buf_mut(&mut self) -> &mut WriteBuf<'a> {\n");
indent(out, 3, "&mut self.buf\n");
indent(out, 2, "}\n");
indent(out, 1, "}\n\n");

indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withLifetime("Encoder"), withLifetime(typeName));
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, withBufLifetime("Encoder"), withBufLifetime(typeName));
indent(out, 2, "#[inline]\n");
indent(out, 2, "fn get_limit(&self) -> usize {\n");
indent(out, 3, "self.limit\n");
Expand All @@ -1268,21 +1279,21 @@ static void appendImplDecoderTrait(
final Appendable out,
final String typeName) throws IOException
{
indent(out, 1, "impl<%s> %s for %s {\n", BUF_LIFETIME, "ActingVersion", withLifetime(typeName));
indent(out, 1, "impl %s for %s {\n", "ActingVersion", withAnonymousLifetime(typeName));
indent(out, 2, "#[inline]\n");
indent(out, 2, "fn acting_version(&self) -> %s {\n", schemaVersionType);
indent(out, 3, "self.acting_version\n");
indent(out, 2, "}\n");
indent(out, 1, "}\n\n");

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

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

0 comments on commit b9e829a

Please sign in to comment.