@@ -53,10 +53,10 @@ public RustGenerator(final Ir ir, final OutputManager outputManager)
53
53
54
54
public void generate () throws IOException
55
55
{
56
- generateSharedImports (ir , outputManager );
56
+ generateSharedImports (outputManager );
57
57
generateResultEnums (outputManager );
58
58
generateDecoderScratchStruct (outputManager );
59
- generateEncoderScratchStruct (ir , outputManager );
59
+ generateEncoderScratchStruct (outputManager );
60
60
generateEitherEnum (outputManager );
61
61
generateEnums (ir , outputManager );
62
62
generateComposites (ir , outputManager );
@@ -203,7 +203,7 @@ private static void generateSingleBitSet(final List<Token> tokens, final OutputM
203
203
indent (writer , 1 , "fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {\n " );
204
204
indent (writer , 2 , "write!(fmt, \" %s[" , setType );
205
205
206
- final StringBuilder string = new StringBuilder ();
206
+ final StringBuilder builder = new StringBuilder ();
207
207
final StringBuilder arguments = new StringBuilder ();
208
208
for (final Token token : tokens )
209
209
{
@@ -215,11 +215,11 @@ private static void generateSingleBitSet(final List<Token> tokens, final OutputM
215
215
final String choiceName = formatMethodName (token .name ());
216
216
final String choiceBitIndex = token .encoding ().constValue ().toString ();
217
217
218
- string .append (choiceName + "(" + choiceBitIndex + ")={}," );
219
- arguments .append ("self.get_" + choiceName + "()," );
218
+ builder .append (choiceName ). append ( "(" ). append ( choiceBitIndex ). append ( ")={}," );
219
+ arguments .append ("self.get_" ). append ( choiceName ). append ( "()," );
220
220
}
221
221
222
- writer .append (string .toString () + "]\" ,\n " );
222
+ writer .append (builder .toString ()). append ( "]\" ,\n " );
223
223
indent (writer , 3 , arguments .toString () + ")\n " );
224
224
indent (writer , 1 , "}\n " );
225
225
writer .append ("}\n " );
@@ -308,6 +308,7 @@ private static String generateFixedFieldCoder(
308
308
fieldStruct .name , topType , fieldStruct .sizeBytes (),
309
309
messageEncodedLength - fieldStruct .sizeBytes ());
310
310
writer .append ("}\n " );
311
+
311
312
return decoderName ;
312
313
}
313
314
}
@@ -535,6 +536,9 @@ private static void appendFixedSizeMemberGroupEncoderMethods(
535
536
final String fieldsType ,
536
537
final String scratchChain ) throws IOException
537
538
{
539
+ final String s = atEndOfParent ?
540
+ "self.parent" : format ("%s::wrap(self.%s)" , afterGroupCoderType , contentProperty );
541
+
538
542
indent (out ).append ("#[inline]\n " );
539
543
indent (out , 1 , "pub fn %s_as_slice(mut self, count: %s) -> CodecResult<(&%s mut [%s], %s)> {\n " ,
540
544
formatMethodName (node .originalName ), rustCountType , DATA_LIFETIME , fieldsType ,
@@ -548,8 +552,8 @@ scratchChain, rustTypeName(node.dimensionsBlockLengthType()),
548
552
indent (out , 2 , "let c = count as usize;\n " );
549
553
indent (out , 2 , "let group_slice = %s.writable_slice::<%s>(c, %s)?;\n " ,
550
554
scratchChain , fieldsType , node .blockLength );
551
- indent ( out , 2 , "Ok((group_slice, %s)) \n " , atEndOfParent ?
552
- "self.parent" : format ( "%s::wrap(self. %s)" , afterGroupCoderType , contentProperty ) );
555
+
556
+ indent ( out , 2 , "Ok((group_slice, %s)) \n " , s );
553
557
indent (out , 1 ).append ("}\n " );
554
558
555
559
indent (out ).append ("#[inline]\n " );
@@ -568,8 +572,7 @@ scratchChain, rustTypeName(node.dimensionsBlockLengthType()),
568
572
scratchChain , rustCountType , rustCountType , node .dimensionsNumInGroupType ().size ());
569
573
indent (out , 2 , "%s.write_slice_without_count::<%s>(s, %s)?;\n " ,
570
574
scratchChain , fieldsType , node .blockLength );
571
- indent (out , 2 , "Ok(%s)\n " , atEndOfParent ? "self.parent" :
572
- format ("%s::wrap(self.%s)" , afterGroupCoderType , contentProperty ));
575
+ indent (out , 2 , "Ok(%s)\n " , s );
573
576
indent (out , 1 ).append ("}\n " );
574
577
}
575
578
@@ -1129,7 +1132,7 @@ private static void generateEnums(final Ir ir, final OutputManager outputManager
1129
1132
}
1130
1133
}
1131
1134
1132
- static void generateSharedImports (final Ir ir , final OutputManager outputManager ) throws IOException
1135
+ static void generateSharedImports (final OutputManager outputManager ) throws IOException
1133
1136
{
1134
1137
try (Writer writer = outputManager .createOutput ("Imports core rather than std to broaden usable environments." ))
1135
1138
{
@@ -1155,7 +1158,7 @@ static void generateResultEnums(final OutputManager outputManager) throws IOExce
1155
1158
}
1156
1159
}
1157
1160
1158
- static void generateEncoderScratchStruct (final Ir ir , final OutputManager outputManager ) throws IOException
1161
+ static void generateEncoderScratchStruct (final OutputManager outputManager ) throws IOException
1159
1162
{
1160
1163
try (Writer writer = outputManager .createOutput ("Scratch Encoder Data Wrapper - codec internal use only" ))
1161
1164
{
@@ -1464,25 +1467,21 @@ private RustArrayType(final RustTypeDescriptor component, final int length)
1464
1467
this .length = length ;
1465
1468
}
1466
1469
1467
- @ Override
1468
1470
public String name ()
1469
1471
{
1470
1472
return getRustStaticArrayString (componentType .name (), length );
1471
1473
}
1472
1474
1473
- @ Override
1474
1475
public String literalValue (final String valueRep )
1475
1476
{
1476
1477
return getRustStaticArrayString (valueRep + componentType .name (), length );
1477
1478
}
1478
1479
1479
- @ Override
1480
1480
public int sizeBytes ()
1481
1481
{
1482
1482
return componentType .sizeBytes () * length ;
1483
1483
}
1484
1484
1485
- @ Override
1486
1485
public String defaultValue ()
1487
1486
{
1488
1487
final String defaultValue = RustTypeDescriptor .super .defaultValue ();
@@ -1504,6 +1503,7 @@ public String defaultValue()
1504
1503
}
1505
1504
}
1506
1505
result .append (']' );
1506
+
1507
1507
return result .toString ();
1508
1508
}
1509
1509
}
@@ -1520,19 +1520,16 @@ private RustPrimitiveType(final String name, final int sizeBytes)
1520
1520
this .sizeBytes = sizeBytes ;
1521
1521
}
1522
1522
1523
- @ Override
1524
1523
public String name ()
1525
1524
{
1526
1525
return name ;
1527
1526
}
1528
1527
1529
- @ Override
1530
1528
public String literalValue (final String valueRep )
1531
1529
{
1532
1530
return valueRep + name ;
1533
1531
}
1534
1532
1535
- @ Override
1536
1533
public int sizeBytes ()
1537
1534
{
1538
1535
return sizeBytes ;
@@ -1550,20 +1547,17 @@ private AnyRustType(final String name, final int sizeBytes)
1550
1547
this .sizeBytes = sizeBytes ;
1551
1548
}
1552
1549
1553
- @ Override
1554
1550
public String name ()
1555
1551
{
1556
1552
return name ;
1557
1553
}
1558
1554
1559
- @ Override
1560
1555
public String literalValue (final String valueRep )
1561
1556
{
1562
1557
final String msg = String .format ("Cannot produce a literal value %s of type %s!" , valueRep , name );
1563
1558
throw new UnsupportedOperationException (msg );
1564
1559
}
1565
1560
1566
- @ Override
1567
1561
public int sizeBytes ()
1568
1562
{
1569
1563
return sizeBytes ;
@@ -1583,6 +1577,7 @@ static RustTypeDescriptor ofPrimitiveToken(final Token token)
1583
1577
{
1584
1578
return new RustArrayType (type , token .arrayLength ());
1585
1579
}
1580
+
1586
1581
return type ;
1587
1582
}
1588
1583
@@ -1617,7 +1612,7 @@ private RustStruct(final String name, final List<RustStructField> fields, final
1617
1612
1618
1613
public int sizeBytes ()
1619
1614
{
1620
- return fields .stream ().mapToInt (v -> v .type .sizeBytes ()).sum ();
1615
+ return fields .stream ().mapToInt (( v ) -> v .type .sizeBytes ()).sum ();
1621
1616
}
1622
1617
1623
1618
static RustStruct fromHeader (final HeaderStructure header )
@@ -1626,13 +1621,15 @@ static RustStruct fromHeader(final HeaderStructure header)
1626
1621
final String originalTypeName = tokens .get (0 ).applicableTypeName ();
1627
1622
final String formattedTypeName = formatTypeName (originalTypeName );
1628
1623
final SplitCompositeTokens splitTokens = SplitCompositeTokens .splitInnerTokens (tokens );
1629
- return RustStruct .fromTokens (formattedTypeName ,
1630
- splitTokens .nonConstantEncodingTokens (),
1631
- EnumSet .of (Modifier .PACKED , Modifier .DEFAULT ));
1624
+
1625
+ return RustStruct .fromTokens (
1626
+ formattedTypeName ,
1627
+ splitTokens .nonConstantEncodingTokens (),
1628
+ EnumSet .of (Modifier .PACKED , Modifier .DEFAULT ));
1632
1629
}
1633
1630
1634
- static RustStruct fromTokens (final String name , final List < NamedToken > tokens ,
1635
- final EnumSet <Modifier > modifiers )
1631
+ static RustStruct fromTokens (
1632
+ final String name , final List < NamedToken > tokens , final EnumSet <Modifier > modifiers )
1636
1633
{
1637
1634
return new RustStruct (name , collectStructFields (tokens ), modifiers );
1638
1635
}
@@ -1656,7 +1653,7 @@ void appendDefinitionTo(final Appendable appendable) throws IOException
1656
1653
{
1657
1654
final boolean needsDefault = modifiers .contains (Modifier .DEFAULT );
1658
1655
final boolean canDeriveDefault = fields .stream ()
1659
- .allMatch (v -> v .type .defaultValue () == RustTypeDescriptor .DEFAULT_VALUE );
1656
+ .allMatch (( v ) -> v .type .defaultValue () == RustTypeDescriptor .DEFAULT_VALUE );
1660
1657
1661
1658
final Set <Modifier > modifiers = this .modifiers .clone ();
1662
1659
if (needsDefault && !canDeriveDefault )
@@ -1682,8 +1679,8 @@ void appendDefinitionTo(final Appendable appendable) throws IOException
1682
1679
}
1683
1680
}
1684
1681
1685
- void appendInstanceTo (final Appendable appendable , final int indent ,
1686
- final Map < String , String > values ) throws IOException
1682
+ void appendInstanceTo (final Appendable appendable , final int indent , final Map < String , String > values )
1683
+ throws IOException
1687
1684
{
1688
1685
indent (appendable , indent , "%s {\n " , name );
1689
1686
for (final RustStructField field : fields )
@@ -1702,7 +1699,6 @@ void appendInstanceTo(final Appendable appendable, final int indent,
1702
1699
}
1703
1700
indent (appendable , indent , "}\n " );
1704
1701
}
1705
-
1706
1702
}
1707
1703
1708
1704
private static final class RustStructField
@@ -1778,8 +1774,7 @@ private static List<RustStructField> collectStructFields(final List<NamedToken>
1778
1774
break ;
1779
1775
1780
1776
default :
1781
- throw new IllegalStateException (
1782
- format ("Unsupported struct property from %s" , typeToken .toString ()));
1777
+ throw new IllegalStateException (format ("Unsupported struct property from %s" , typeToken ));
1783
1778
}
1784
1779
}
1785
1780
return fields ;
@@ -1832,9 +1827,8 @@ private static void appendStructHeader(final Appendable appendable, final String
1832
1827
}
1833
1828
1834
1829
private static void appendStructHeader (
1835
- final Appendable appendable ,
1836
- final String structName ,
1837
- final Set <RustStruct .Modifier > modifiers ) throws IOException
1830
+ final Appendable appendable , final String structName , final Set <RustStruct .Modifier > modifiers )
1831
+ throws IOException
1838
1832
{
1839
1833
if (!modifiers .isEmpty ())
1840
1834
{
@@ -1873,9 +1867,7 @@ private static String getRustTypeForPrimitivePossiblyArray(
1873
1867
}
1874
1868
1875
1869
private static void generateConstantAccessorImpl (
1876
- final Appendable writer ,
1877
- final String formattedTypeName ,
1878
- final List <Token > unfilteredFields ) throws IOException
1870
+ final Appendable writer , final String formattedTypeName , final List <Token > unfilteredFields ) throws IOException
1879
1871
{
1880
1872
writer .append (format ("%nimpl %s {\n " , formattedTypeName ));
1881
1873
0 commit comments