Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions crates/bindings-csharp/BSATN.Codegen/BSATN.Codegen.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>SpacetimeDB.BSATN.Codegen</AssemblyName>
<Version>1.11.0</Version>
Expand Down Expand Up @@ -27,5 +26,4 @@
<!-- dev-dependency to add internal C# classes and attributes not included in .NET Standard -->
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions crates/bindings-csharp/BSATN.Codegen/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public abstract record TypeUse(string Name, string BSATNName)
/// </summary>
public static string BsatnFieldSuffix => $"{BSATN_FIELD_SUFFIX}";


/// <summary>
/// Parse a type use for a member.
/// </summary>
Expand All @@ -53,6 +54,12 @@ public abstract record TypeUse(string Name, string BSATNName)
/// <returns></returns>
public static TypeUse Parse(ISymbol member, ITypeSymbol typeSymbol, DiagReporter diag)
{
if (typeSymbol.SpecialType == SpecialType.System_Void)
{
// Treat void as equivalent to Unit type
return new ReferenceUse("SpacetimeDB.Unit", "SpacetimeDB.Unit.BSATN");
}

var type = SymbolToName(typeSymbol);
string typeInfo;

Expand Down Expand Up @@ -194,6 +201,7 @@ public override string GetHashCodeStatement(string inVar, string outVar, int lev
$"var {outVar} = {inVar} == null ? 0 : {inVar}.GetHashCode();";
}


/// <summary>
/// A use of an array type.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
Expand All @@ -20,7 +19,10 @@

<ItemGroup>
<ProjectReference Include="../BSATN.Runtime/BSATN.Runtime.csproj" />
<ProjectReference Include="../BSATN.Codegen/BSATN.Codegen.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference
Include="../BSATN.Codegen/BSATN.Codegen.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"
/>
</ItemGroup>

</Project>
33 changes: 10 additions & 23 deletions crates/bindings-csharp/BSATN.Runtime.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,12 @@ public BasicDataClass((int x, string y, int? z, string? w) data)
}

[Type]
public partial struct BasicDataStruct
public partial struct BasicDataStruct((int x, string y, int? z, string? w) data)
{
public int X;
public string Y;
public int? Z;
public string? W;

public BasicDataStruct((int x, string y, int? z, string? w) data)
{
X = data.x;
Y = data.y;
Z = data.z;
W = data.w;
}
public int X = data.x;
public string Y = data.y;
public int? Z = data.z;
public string? W = data.w;
}

[Type]
Expand Down Expand Up @@ -315,12 +307,12 @@ public void Add(bool collides)
}
}

public double CollisionFraction
public readonly double CollisionFraction
{
get => (double)Collisions / (double)Comparisons;
}

public void AssertCollisionsLessThan(double fraction)
public readonly void AssertCollisionsLessThan(double fraction)
{
Assert.True(
CollisionFraction < fraction,
Expand Down Expand Up @@ -626,18 +618,13 @@ public static void GeneratedNestedListRoundTrip()
.Select(list => new ContainsNestedList(list));
#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.


static readonly Gen<(ContainsNestedList e1, ContainsNestedList e2)> GenTwoContainsNestedList =
Gen.Select(GenContainsNestedList, GenContainsNestedList, (e1, e2) => (e1, e2));

class EnumerableEqualityComparer<T> : EqualityComparer<IEnumerable<T>>
class EnumerableEqualityComparer<T>(EqualityComparer<T> equalityComparer)
: EqualityComparer<IEnumerable<T>>
{
private readonly EqualityComparer<T> EqualityComparer;

public EnumerableEqualityComparer(EqualityComparer<T> equalityComparer)
{
EqualityComparer = equalityComparer;
}
private readonly EqualityComparer<T> EqualityComparer = equalityComparer;

public override bool Equals(IEnumerable<T>? x, IEnumerable<T>? y) =>
x == null ? y == null : (y == null ? false : x.SequenceEqual(y, EqualityComparer));
Expand Down
14 changes: 10 additions & 4 deletions crates/bindings-csharp/BSATN.Runtime/BSATN.Runtime.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>SpacetimeDB.BSATN.Runtime</AssemblyName>
<Version>1.11.0</Version>
Expand All @@ -18,18 +17,25 @@

<ItemGroup>
<!-- We want to build BSATN.Codegen both to include it in our NuGet package but also we want it to transform [SpacetimeDB.Type] usages in BSATN.Runtime code itself. -->
<ProjectReference Include="../BSATN.Codegen/BSATN.Codegen.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference
Include="../BSATN.Codegen/BSATN.Codegen.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"
/>
</ItemGroup>

<ItemGroup>
<None Include="../Runtime/README.md" Pack="true" PackagePath="" />
<!-- We want all users who depends on BSATN.Runtime to automatically get the Roslyn codegen component as well. -->
<None Include="../BSATN.Codegen/bin/$(Configuration)/netstandard2.0/SpacetimeDB.BSATN.Codegen.dll" Pack="true" PackagePath="analyzers/dotnet/cs" />
<None
Include="../BSATN.Codegen/bin/$(Configuration)/netstandard2.0/SpacetimeDB.BSATN.Codegen.dll"
Pack="true"
PackagePath="analyzers/dotnet/cs"
/>
</ItemGroup>

<ItemGroup>
<!-- dev-dependency to add internal C# classes and attributes not included in .NET Standard -->
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />
</ItemGroup>

</Project>
12 changes: 3 additions & 9 deletions crates/bindings-csharp/BSATN.Runtime/BSATN/AlgebraicType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ public interface ITypeRegistrar
}

[SpacetimeDB.Type]
public partial struct AggregateElement
public partial struct AggregateElement(string name, AlgebraicType algebraicType)
{
public string? Name;
public string? Name = name;

public AlgebraicType AlgebraicType;

public AggregateElement(string name, AlgebraicType algebraicType)
{
Name = name;
AlgebraicType = algebraicType;
}
Comment on lines -9 to -19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why'd this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C# benefits from property initializers and explicit constructors for better tooling support. Basically this pattern matches .NET's design guidelines and enables better encapsulation.

public AlgebraicType AlgebraicType = algebraicType;
}

[SpacetimeDB.Type]
Expand Down
1 change: 0 additions & 1 deletion crates/bindings-csharp/BSATN.Runtime/BSATN/I256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public int CompareTo(I256 value)
}

/// <inheritdoc cref="INumberBase{TSelf}.IsNegative(TSelf)" />

public static bool IsNegative(I256 value) => (long)value._upper.Upper < 0;

private BigInteger AsBigInt() =>
Expand Down
2 changes: 1 addition & 1 deletion crates/bindings-csharp/BSATN.Runtime/Builtins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public readonly TimeDuration TimeDurationSince(Timestamp earlier) =>
public static Timestamp operator -(Timestamp point, TimeDuration interval) =>
new Timestamp(checked(point.MicrosecondsSinceUnixEpoch - interval.Microseconds));

public int CompareTo(Timestamp that)
public readonly int CompareTo(Timestamp that)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why'd this change? Is it related to this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Na, this was just an IDE recommendation that made sense to me when I was making changes. It's not required. It just helps the compiler optimize the method.

{
return this.MicrosecondsSinceUnixEpoch.CompareTo(that.MicrosecondsSinceUnixEpoch);
}
Expand Down
2 changes: 0 additions & 2 deletions crates/bindings-csharp/Codegen.Tests/Codegen.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
Expand Down Expand Up @@ -32,5 +31,4 @@
<ItemGroup>
<ProjectReference Include="../Codegen/Codegen.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!-- We want to ensure that BSATN.Codegen on its own produces code compatible with Unity. -->
<!-- This means limiting project to .NET Standard 2.1 and C# 9. -->
Expand All @@ -10,5 +9,4 @@
<ItemGroup>
<ProjectReference Include="../../../BSATN.Runtime/BSATN.Runtime.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -9,5 +8,4 @@
<ProjectReference Include="../../../BSATN.Runtime/BSATN.Runtime.csproj" />
<ProjectReference Include="../../../Runtime/Runtime.csproj" />
</ItemGroup>

</Project>
Loading
Loading