From b9e829a6c384ce5b2385259804bd2dad1061ac51 Mon Sep 17 00:00:00 2001 From: Grzegorz Gierlach Date: Tue, 25 Feb 2025 14:56:08 +0100 Subject: [PATCH] [Rust] Elide explicit ActingVersion lifetime. --- .../sbe/generation/rust/MessageCoderDef.java | 10 ++++---- .../sbe/generation/rust/RustGenerator.java | 25 +++++++++++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/MessageCoderDef.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/MessageCoderDef.java index da9060b9a2..e4e19f4c52 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/MessageCoderDef.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/MessageCoderDef.java @@ -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 @@ -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"); @@ -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()); @@ -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"); } diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java index 32f1633178..99aad88b46 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java @@ -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 @@ -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( @@ -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"); @@ -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");