Skip to content

Commit 24d781d

Browse files
author
Vadim Platonov
committed
[Rust] Generate impl Default for field structs
Without a default value, structs with paddings become tedious to construct.
1 parent 09b3155 commit 24d781d

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

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

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,11 @@ private static void generateEnum(final List<Token> enumTokens, final OutputManag
13181318
.append(",\n");
13191319

13201320
writer.append("}\n");
1321+
1322+
// Default implementation to support Default in other structs
1323+
indent(writer, 0, "impl Default for %s {\n", enumRustName);
1324+
indent(writer, 1, "fn default() -> Self { %s::%s }\n", enumRustName, "NullVal");
1325+
indent(writer, 0, "}\n");
13211326
}
13221327
}
13231328

@@ -1387,9 +1392,35 @@ public String literalValue(String valueRep)
13871392
}
13881393

13891394
@Override
1390-
public int sizeBytes() {
1395+
public int sizeBytes()
1396+
{
13911397
return componentType.sizeBytes() * length;
13921398
}
1399+
1400+
@Override
1401+
public String defaultValue()
1402+
{
1403+
final String defaultValue = RustTypeDescriptor.super.defaultValue();
1404+
if (length <= 32)
1405+
{
1406+
return defaultValue;
1407+
}
1408+
else
1409+
{
1410+
StringBuilder result = new StringBuilder();
1411+
result.append('[');
1412+
for (int i = 0; i < length; i++)
1413+
{
1414+
result.append(defaultValue);
1415+
result.append(", ");
1416+
if (i % 4 == 0) { // ~80 char lines
1417+
result.append('\n');
1418+
}
1419+
}
1420+
result.append(']');
1421+
return result.toString();
1422+
}
1423+
}
13931424
}
13941425

13951426
private static final class RustPrimitiveType implements RustTypeDescriptor
@@ -1515,6 +1546,20 @@ static RustStruct fromTokens(String name, List<NamedToken> tokens, EnumSet<Modif
15151546
return new RustStruct(name, collectStructFields(tokens), modifiers);
15161547
}
15171548

1549+
// No way to create struct with default values.
1550+
// Rust RFC: https://github.com/Centril/rfcs/pull/19
1551+
// TODO: #[derive(Default)] ?
1552+
void appendDefaultConstructorTo(final Appendable appendable) throws IOException {
1553+
indent(appendable, 0, "impl Default for %s {\n", name);
1554+
indent(appendable, 1, "fn default() -> Self {\n");
1555+
1556+
appendInstanceTo(appendable, 2);
1557+
1558+
indent(appendable, 1, "}\n");
1559+
1560+
appendable.append("}\n");
1561+
}
1562+
15181563
void appendDefinitionTo(final Appendable appendable) throws IOException
15191564
{
15201565
appendStructHeader(appendable, name, modifiers.contains(Modifier.PACKED));
@@ -1524,12 +1569,18 @@ void appendDefinitionTo(final Appendable appendable) throws IOException
15241569
if (field.modifiers.contains(RustStructField.Modifier.PUBLIC)) appendable.append("pub ");
15251570
appendable.append(field.name).append(":").append(field.type.name()).append(",\n");
15261571
}
1527-
appendable.append("}");
1572+
appendable.append("}\n");
1573+
appendDefaultConstructorTo(appendable);
1574+
}
1575+
1576+
void appendInstanceTo(final Appendable appendable, int indent) throws IOException
1577+
{
1578+
appendInstanceTo(appendable, indent, Collections.emptyMap());
15281579
}
15291580

15301581
void appendInstanceTo(final Appendable appendable, int indent, Map<String, String> values) throws IOException
15311582
{
1532-
appendable.append("MessageHeader {\n");
1583+
indent(appendable, indent, "%s {\n", name);
15331584
for (RustStructField field: fields) {
15341585
final String value;
15351586
if (values.containsKey(field.name))
@@ -1635,7 +1686,7 @@ private void generateMessageHeaderDefault(
16351686
indent(writer, 1, "pub message_header: MessageHeader\n");
16361687
writer.append("}\n");
16371688

1638-
indent(writer, 1, "impl Default for %s {\n", wrapperName);
1689+
indent(writer, 0, "impl Default for %s {\n", wrapperName);
16391690
indent(writer, 1, "fn default() -> %s {\n", wrapperName);
16401691
indent(writer, 2, "%s {\n", wrapperName);
16411692

0 commit comments

Comments
 (0)