Skip to content

Commit ae41175

Browse files
committed
Improve Rust code gen
1 parent 5ed2dbe commit ae41175

14 files changed

Lines changed: 109 additions & 110 deletions

Src/FastData.Generator.Rust.Tests/Features/ObjectSupportTest-ArrayStructure_Int32_3.verified.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ impl Person {
1818
pub const fn new(age: i32, name: &'static str, other: Option<&'static Person>) -> Self { Self { age, name, other } }
1919
}
2020

21-
static VALUES: [&'static Person; 3] = [
21+
impl ArrayStructure_Int32_3 {
22+
const VALUES: [&'static Person; 3] = [
2223
&Person::new(1, "Bob", Some(&Person::new(4, "Anna", None))), &Person::new(2, "Billy", None), &Person::new(3, "Bibi", None)
2324
];
24-
25-
impl ArrayStructure_Int32_3 {
2625
const KEYS: [i32; 3] = [
2726
1, 2, 3
2827
];
@@ -48,7 +47,7 @@ impl ArrayStructure_Int32_3 {
4847

4948
for entry in Self::KEYS.iter() {
5049
if *entry == key {
51-
return Some(VALUES[(key - 1) as usize])
50+
return Some(Self::VALUES[(key - 1) as usize])
5251
}
5352
}
5453
None

Src/FastData.Generator.Rust.Tests/Features/ObjectSupportTest-BinarySearchStructure_Int32_3.verified.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ impl Person {
1818
pub const fn new(age: i32, name: &'static str, other: Option<&'static Person>) -> Self { Self { age, name, other } }
1919
}
2020

21-
static VALUES: [&'static Person; 3] = [
21+
impl BinarySearchStructure_Int32_3 {
22+
const VALUES: [&'static Person; 3] = [
2223
&Person::new(1, "Bob", Some(&Person::new(4, "Anna", None))), &Person::new(2, "Billy", None), &Person::new(3, "Bibi", None)
2324
];
24-
25-
impl BinarySearchStructure_Int32_3 {
2625
const KEYS: [i32; 3] = [
2726
1, 2, 3
2827
];
@@ -64,7 +63,7 @@ impl BinarySearchStructure_Int32_3 {
6463
let entry = Self::KEYS[i];
6564

6665
if entry == key {
67-
return Some(VALUES[i]);
66+
return Some(Self::VALUES[i]);
6867
}
6968
if entry < key {
7069
lo = i + 1;

Src/FastData.Generator.Rust.Tests/Features/ObjectSupportTest-ConditionalStructure_Int32_3.verified.txt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ impl Person {
1818
pub const fn new(age: i32, name: &'static str, other: Option<&'static Person>) -> Self { Self { age, name, other } }
1919
}
2020

21-
static VALUES: [&'static Person; 3] = [
22-
&Person::new(1, "Bob", Some(&Person::new(4, "Anna", None))), &Person::new(2, "Billy", None), &Person::new(3, "Bibi", None)
23-
];
24-
2521
impl ConditionalStructure_Int32_3 {
26-
#[must_use]
22+
23+
const VALUES: [&'static Person; 3] = [
24+
&Person::new(1, "Bob", Some(&Person::new(4, "Anna", None))), &Person::new(2, "Billy", None), &Person::new(3, "Bibi", None)
25+
]; #[must_use]
2726
pub fn contains(key: i32) -> bool {
2827

2928

@@ -36,17 +35,14 @@ impl ConditionalStructure_Int32_3 {
3635
#[must_use]
3736
pub fn try_lookup(key: i32) -> Option<&'static Person> {
3837

39-
if (key == 1)
40-
{
41-
return Some(VALUES[0]);
38+
if (key == 1) {
39+
return Some(Self::VALUES[0]);
4240
}
43-
if (key == 2)
44-
{
45-
return Some(VALUES[1]);
41+
if (key == 2) {
42+
return Some(Self::VALUES[1]);
4643
}
47-
if (key == 3)
48-
{
49-
return Some(VALUES[2]);
44+
if (key == 3) {
45+
return Some(Self::VALUES[2]);
5046
}
5147

5248
None

Src/FastData.Generator.Rust.Tests/Features/ObjectSupportTest-KeyLengthStructure_String_3.verified.txt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
use std::ptr;
88

99
pub struct KeyLengthStructure_String_3;
10-
11-
static KEYS: [&'static str; 3] = [
12-
"a", "aa", "aaa"
13-
];
1410
pub struct Person {
1511
pub age: i32,
1612
pub name: &'static str,
@@ -22,22 +18,25 @@ impl Person {
2218
pub const fn new(age: i32, name: &'static str, other: Option<&'static Person>) -> Self { Self { age, name, other } }
2319
}
2420

25-
static VALUES: [&'static Person; 3] = [
21+
impl KeyLengthStructure_String_3 {
22+
const VALUES: [&'static Person; 3] = [
2623
&Person::new(1, "Bob", Some(&Person::new(4, "Anna", None))), &Person::new(2, "Billy", None), &Person::new(3, "Bibi", None)
2724
];
2825

29-
static OFFSETS: [i32; 3] = [
26+
const OFFSETS: [i32; 3] = [
3027
0, 1, 2
3128
];
29+
const KEYS: [&'static str; 3] = [
30+
"a", "aa", "aaa"
31+
];
3232

33-
impl KeyLengthStructure_String_3 {
3433
#[must_use]
3534
pub fn contains(key: &'static str) -> bool {
3635
if key.len() < 1 as usize || key.len() > 3 as usize {
3736
return false;
3837
}
3938

40-
return key == KEYS[key.len() - 1];
39+
return key == Self::KEYS[key.len() - 1];
4140
}
4241
#[must_use]
4342
pub fn try_lookup(key: &'static str) -> Option<&'static Person> {
@@ -46,8 +45,8 @@ pub fn try_lookup(key: &'static str) -> Option<&'static Person> {
4645
}
4746

4847
let idx = (key.len() - 1) as usize;
49-
if (key == KEYS[idx]) {
50-
return Some(VALUES[OFFSETS[idx] as usize]);
48+
if (key == Self::KEYS[idx]) {
49+
return Some(Self::VALUES[Self::OFFSETS[idx] as usize]);
5150
}
5251
None
5352
}

Src/FastData.Generator.Rust.Tests/Features/ObjectSupportTest-SingleValueStructure_Int32_1.verified.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ pub struct Person {
1717
impl Person {
1818
pub const fn new(age: i32, name: &'static str, other: Option<&'static Person>) -> Self { Self { age, name, other } }
1919
}
20-
pub static STORED_VALUE: &'static Person = &Person::new(1, "Bob", Some(&Person::new(4, "Anna", None)));
2120

2221
impl SingleValueStructure_Int32_1 {
23-
#[must_use]
22+
pub const STORED_VALUE: &'static Person = &Person::new(1, "Bob", Some(&Person::new(4, "Anna", None))); #[must_use]
2423
pub fn contains(key: i32) -> bool {
2524
key == 1
2625
} #[must_use]
2726
pub fn try_lookup(key: i32) -> Option<&'static Person> {
2827
if (key == 1) {
29-
return Some(STORED_VALUE);
28+
return Some(Self::STORED_VALUE);
3029
}
3130
None
3231
}

Src/FastData.Generator.Rust.Tests/Vectors/KeyLengthStructure_String_7.verified.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ use std::ptr;
88

99
pub struct KeyLengthStructure_String_7;
1010

11-
static KEYS: [&'static str; 8] = [
11+
impl KeyLengthStructure_String_7 {
12+
const KEYS: [&'static str; 8] = [
1213
"aaa", "", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa"
1314
];
1415

15-
impl KeyLengthStructure_String_7 {
1616
#[must_use]
1717
pub fn contains(key: &'static str) -> bool {
1818
if 1012u64 & (1u64 << (key.len() - 1)) == 0 {
1919
return false;
2020
}
2121

22-
return key == KEYS[key.len() - 3];
22+
return key == Self::KEYS[key.len() - 3];
2323
}
2424

2525
pub const ITEM_COUNT: usize = 7;

Src/FastData.Generator.Rust/Internal/Framework/RustOutputWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ internal abstract class RustOutputWriter<T> : OutputWriter<T>
66
{
77
protected string MethodModifier => "pub ";
88
protected string MethodAttribute => "#[must_use]";
9-
protected string FieldModifier => string.Empty;
9+
protected string FieldModifier => "const ";
1010
protected string GetKeyTypeName(bool customType) => customType ? $"&'static {KeyTypeName}" : KeyTypeName;
1111
protected string GetValueTypeName(bool customType) => customType ? $"&'static {ValueTypeName}" : ValueTypeName;
1212
}

Src/FastData.Generator.Rust/Internal/Generators/ArrayCode.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,23 @@ internal sealed class ArrayCode<TKey, TValue>(ArrayContext<TKey, TValue> ctx, Sh
1010
public override string Generate()
1111
{
1212
bool customKey = !typeof(TKey).IsPrimitive;
13+
bool customValue = !typeof(TValue).IsPrimitive;
1314
StringBuilder sb = new StringBuilder();
1415

16+
if (ctx.Values != null)
17+
{
18+
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
19+
20+
sb.Append($"""
21+
{FieldModifier}VALUES: [{GetValueTypeName(customValue)}; {ctx.Values.Length.ToStringInvariant()}] = [
22+
{FormatColumns(ctx.Values, ToValueLabel)}
23+
];
24+
25+
""");
26+
}
27+
1528
sb.Append($$"""
16-
{{FieldModifier}}const KEYS: [{{GetKeyTypeName(customKey)}}; {{ctx.Keys.Length.ToStringInvariant()}}] = [
29+
{{FieldModifier}}KEYS: [{{GetKeyTypeName(customKey)}}; {{ctx.Keys.Length.ToStringInvariant()}}] = [
1730
{{FormatColumns(ctx.Keys, ToValueLabel)}}
1831
];
1932
@@ -32,17 +45,6 @@ public override string Generate()
3245

3346
if (ctx.Values != null)
3447
{
35-
bool customValue = !typeof(TValue).IsPrimitive;
36-
37-
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
38-
39-
shared.Add(CodePlacement.Before, $"""
40-
41-
{FieldModifier} static VALUES: [{GetValueTypeName(customValue)}; {ctx.Values.Length.ToStringInvariant()}] = [
42-
{FormatColumns(ctx.Values, ToValueLabel)}
43-
];
44-
""");
45-
4648
sb.Append($$"""
4749
4850
{{MethodAttribute}}
@@ -51,7 +53,7 @@ public override string Generate()
5153
5254
for entry in Self::KEYS.iter() {
5355
if {{GetEqualFunction("*entry", "key")}} {
54-
return Some(VALUES[(key - 1) as usize])
56+
return Some(Self::VALUES[(key - 1) as usize])
5557
}
5658
}
5759
None

Src/FastData.Generator.Rust/Internal/Generators/BinarySearchCode.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,23 @@ internal sealed class BinarySearchCode<TKey, TValue>(BinarySearchContext<TKey, T
1010
public override string Generate()
1111
{
1212
bool customKey = !typeof(TKey).IsPrimitive;
13+
bool customValue = !typeof(TValue).IsPrimitive;
1314
StringBuilder sb = new StringBuilder();
1415

16+
if (ctx.Values != null)
17+
{
18+
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
19+
20+
sb.Append($"""
21+
{FieldModifier}VALUES: [{GetValueTypeName(customValue)}; {ctx.Values.Length.ToStringInvariant()}] = [
22+
{FormatColumns(ctx.Values, ToValueLabel)}
23+
];
24+
25+
""");
26+
}
27+
1528
sb.Append($$"""
16-
{{FieldModifier}}const KEYS: [{{GetKeyTypeName(customKey)}}; {{ctx.Keys.Length.ToStringInvariant()}}] = [
29+
{{FieldModifier}}KEYS: [{{GetKeyTypeName(customKey)}}; {{ctx.Keys.Length.ToStringInvariant()}}] = [
1730
{{FormatColumns(ctx.Keys, ToValueLabel)}}
1831
];
1932
@@ -43,16 +56,6 @@ public override string Generate()
4356

4457
if (ctx.Values != null)
4558
{
46-
bool customValue = !typeof(TValue).IsPrimitive;
47-
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
48-
49-
shared.Add(CodePlacement.Before, $"""
50-
51-
{FieldModifier} static VALUES: [{GetValueTypeName(customValue)}; {ctx.Values.Length.ToStringInvariant()}] = [
52-
{FormatColumns(ctx.Values, ToValueLabel)}
53-
];
54-
""");
55-
5659
sb.Append($$"""
5760
5861
{{MethodAttribute}}
@@ -66,7 +69,7 @@ public override string Generate()
6669
let entry = Self::KEYS[i];
6770
6871
if entry == key {
69-
return Some(VALUES[i]);
72+
return Some(Self::VALUES[i]);
7073
}
7174
if entry < key {
7275
lo = i + 1;

Src/FastData.Generator.Rust/Internal/Generators/ConditionalCode.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,21 @@ internal sealed class ConditionalCode<TKey, TValue>(ConditionalContext<TKey, TVa
1010
public override string Generate()
1111
{
1212
bool customKey = !typeof(TKey).IsPrimitive;
13+
bool customType = !typeof(TValue).IsPrimitive;
1314
StringBuilder sb = new StringBuilder();
1415

16+
if (ctx.Values != null)
17+
{
18+
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
19+
20+
sb.Append($"""
21+
22+
{FieldModifier}VALUES: [{GetValueTypeName(customType)}; {ctx.Values.Length.ToStringInvariant()}] = [
23+
{FormatColumns(ctx.Values, ToValueLabel)}
24+
];
25+
""");
26+
}
27+
1528
sb.Append($$"""
1629
{{MethodAttribute}}
1730
{{MethodModifier}}fn contains(key: {{GetKeyTypeName(customKey)}}) -> bool {
@@ -27,16 +40,6 @@ public override string Generate()
2740

2841
if (ctx.Values != null)
2942
{
30-
bool customType = !typeof(TValue).IsPrimitive;
31-
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
32-
33-
shared.Add(CodePlacement.Before, $"""
34-
35-
{FieldModifier} static VALUES: [{GetValueTypeName(customType)}; {ctx.Values.Length.ToStringInvariant()}] = [
36-
{FormatColumns(ctx.Values, ToValueLabel)}
37-
];
38-
""");
39-
4043
sb.Append($$"""
4144
4245
{{MethodAttribute}}
@@ -54,9 +57,8 @@ string GenerateBranches()
5457
for (int i = 0; i < ctx.Keys.Length; i++)
5558
{
5659
temp.AppendLine($$"""
57-
if (key == {{ToValueLabel(ctx.Keys[i])}})
58-
{
59-
return Some(VALUES[{{i.ToStringInvariant()}}]);
60+
if (key == {{ToValueLabel(ctx.Keys[i])}}) {
61+
return Some(Self::VALUES[{{i.ToStringInvariant()}}]);
6062
}
6163
""");
6264
}

0 commit comments

Comments
 (0)