Skip to content

Commit e22ff5d

Browse files
authored
Merge pull request aeron-io#685 from ian-p-cooke/streaming_rust
enhancements to Rust generator
2 parents a6ef907 + 471c7a8 commit e22ff5d

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# Build tools
1414
build-local.properties
1515
deps
16-
/bin
16+
*/bin
1717
build
1818
.gradle
1919
target
@@ -23,6 +23,7 @@ GPATH
2323
prop
2424
out
2525
.vs/
26+
.vscode
2627

2728
# cmake
2829

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,8 @@ def cargo_exists() {
569569
try {
570570
def result = project.exec {
571571
executable = 'cargo'
572-
args = '-v'
572+
args = [ '-V' ]
573+
standardOutput = new ByteArrayOutputStream()
573574
}
574575
return result.exitValue == 0
575576
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ void appendWrapMethod(final Appendable appendable, final String structName)
173173
throws IOException
174174
{
175175
appendable.append("\n").append(INDENT).append(String.format(
176-
"fn wrap(%s: %s) -> %s {%n", scratchProperty(), RustGenerator.withLifetime(scratchType()),
176+
"pub fn wrap(%s: %s) -> %s {%n", scratchProperty(), RustGenerator.withLifetime(scratchType()),
177177
RustGenerator.withLifetime(structName)));
178178
indent(appendable, 2, "%s { %s: %s }\n",
179179
structName, scratchProperty(), scratchProperty());

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public RustGenerator(final Ir ir, final OutputManager outputManager)
5353

5454
public void generate() throws IOException
5555
{
56+
final int headerSize = totalByteSize(ir.headerStructure());
57+
5658
generateSharedImports(outputManager);
5759
generateResultEnums(outputManager);
5860
generateDecoderScratchStruct(outputManager);
@@ -61,7 +63,7 @@ public void generate() throws IOException
6163
generateEnums(ir, outputManager);
6264
generateComposites(ir, outputManager);
6365
generateBitSets(ir, outputManager);
64-
final int headerSize = totalByteSize(ir.headerStructure());
66+
generateMessageHeaderDecoder(outputManager, headerSize);
6567

6668
for (final List<Token> tokens : ir.messages())
6769
{
@@ -285,6 +287,27 @@ private static void generateEntryPoint(
285287
}
286288
}
287289

290+
static void generateMessageHeaderDecoder(
291+
final OutputManager outputManager,
292+
final int headerSize) throws IOException
293+
{
294+
final String messageTypeName = "MessageHeader";
295+
final RustCodecType codecType = RustCodecType.Decoder;
296+
try (Writer writer = outputManager.createOutput(messageTypeName + format(" %s entry point", codecType.name())))
297+
{
298+
final String gerund = codecType.gerund();
299+
writer.append(format("pub fn start_%s_%s<%s>(data: &%s%s [u8]) -> CodecResult<(&%s %s, %s)> {\n", gerund,
300+
formatMethodName(messageTypeName), DATA_LIFETIME, DATA_LIFETIME,
301+
codecType == RustCodecType.Encoder ? " mut" : "",
302+
DATA_LIFETIME, messageTypeName,
303+
withLifetime(codecType.scratchType())));
304+
indent(writer, 1, format("let mut scratch = %s { data: data, pos: 0 };\n", codecType.scratchType()));
305+
indent(writer, 1, format("let v = scratch.read_type::<%s>(%s)?;\n", messageTypeName, headerSize));
306+
indent(writer, 1, "Ok((v, scratch))\n");
307+
writer.append("}\n");
308+
}
309+
}
310+
288311
static String withLifetime(final String typeName)
289312
{
290313
return format("%s<%s>", typeName, DATA_LIFETIME);
@@ -1163,7 +1186,7 @@ static void generateEncoderScratchStruct(final OutputManager outputManager) thro
11631186
try (Writer writer = outputManager.createOutput("Scratch Encoder Data Wrapper - codec internal use only"))
11641187
{
11651188
writer.append("#[derive(Debug)]\n");
1166-
writer.append(format("struct %s<%s> {\n", SCRATCH_ENCODER_TYPE, DATA_LIFETIME));
1189+
writer.append(format("pub struct %s<%s> {\n", SCRATCH_ENCODER_TYPE, DATA_LIFETIME));
11671190
indent(writer, 1, "data: &%s mut [u8],\n", DATA_LIFETIME);
11681191
indent(writer).append("pos: usize,\n");
11691192
writer.append("}\n");
@@ -1291,7 +1314,7 @@ private static void generateDecoderScratchStruct(final OutputManager outputManag
12911314
try (Writer writer = outputManager.createOutput("Scratch Decoder Data Wrapper - codec internal use only"))
12921315
{
12931316
writer.append("#[derive(Debug)]\n");
1294-
writer.append(format("struct %s<%s> {\n", SCRATCH_DECODER_TYPE, DATA_LIFETIME));
1317+
writer.append(format("pub struct %s<%s> {\n", SCRATCH_DECODER_TYPE, DATA_LIFETIME));
12951318
indent(writer, 1, "data: &%s [u8],\n", DATA_LIFETIME);
12961319
indent(writer).append("pos: usize,\n");
12971320
writer.append("}\n");
@@ -1798,6 +1821,18 @@ private void generateMessageHeaderDefault(
17981821
indent(writer, 1, "pub message_header: MessageHeader\n");
17991822
writer.append("}\n");
18001823

1824+
final String blockLength = Integer.toString(messageToken.encodedLength());
1825+
final String templateId = Integer.toString(messageToken.id());
1826+
final String schemaId = Integer.toString(ir.id());
1827+
final String version = Integer.toString(ir.version());
1828+
1829+
indent(writer, 0, "impl %s {\n", wrapperName);
1830+
indent(writer, 1, "pub const BLOCK_LENGTH : u16 = " + blockLength + ";\n");
1831+
indent(writer, 1, "pub const TEMPLATE_ID : u16 = " + templateId + ";\n");
1832+
indent(writer, 1, "pub const SCHEMA_ID : u16 = " + schemaId + ";\n");
1833+
indent(writer, 1, "pub const VERSION : u16 = " + version + ";\n");
1834+
indent(writer, 0, "}\n");
1835+
18011836
indent(writer, 0, "impl Default for %s {\n", wrapperName);
18021837
indent(writer, 1, "fn default() -> %s {\n", wrapperName);
18031838
indent(writer, 2, "%s {\n", wrapperName);
@@ -1807,10 +1842,10 @@ private void generateMessageHeaderDefault(
18071842
new HashMap<String, String>()
18081843
{
18091844
{
1810-
put("block_length", Integer.toString(messageToken.encodedLength()));
1811-
put("template_id", Integer.toString(messageToken.id()));
1812-
put("schema_id", Integer.toString(ir.id()));
1813-
put("version", Integer.toString(ir.version()));
1845+
put("block_length", blockLength);
1846+
put("template_id", templateId);
1847+
put("schema_id", schemaId);
1848+
put("version", version);
18141849
}
18151850
});
18161851

sbe-tool/src/test/java/uk/co/real_logic/sbe/generation/rust/RustGeneratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ private static boolean cargoExists()
237237
{
238238
try
239239
{
240-
final ProcessBuilder builder = new ProcessBuilder("cargo", "-v");
240+
final ProcessBuilder builder = new ProcessBuilder("cargo", "-V");
241241
final Process process = builder.start();
242242
process.waitFor(5, TimeUnit.SECONDS);
243243
return process.exitValue() == 0;

0 commit comments

Comments
 (0)