Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions crates/bindings-csharp/BSATN.Runtime.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,26 @@ public static void TimestampConversionChecks()

var newIntervalUs = 333L;
var newInterval = new TimeDuration(newIntervalUs);
var laterInterval = new TimeDuration(newIntervalUs + 1);
var laterStamp = stamp + newInterval;
Assert.Equal(laterStamp.MicrosecondsSinceUnixEpoch, us + newIntervalUs);
Assert.Equal(laterStamp.TimeDurationSince(stamp), newInterval);

#pragma warning disable CS1718
Assert.True(stamp == stamp);
Assert.True(newInterval == newInterval);
#pragma warning restore CS1718
Assert.False(stamp == laterStamp);
Assert.True(stamp < laterStamp);
Assert.False(laterStamp < stamp);
Assert.Equal(-1, stamp.CompareTo(laterStamp));
Assert.Equal(+1, laterStamp.CompareTo(stamp));

Assert.False(newInterval == laterInterval);
Assert.True(newInterval < laterInterval);
Assert.False(laterInterval < newInterval);
Assert.Equal(-1, newInterval.CompareTo(laterInterval));
Assert.Equal(+1, laterInterval.CompareTo(newInterval));
}

[Fact]
Expand Down
22 changes: 20 additions & 2 deletions 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)
{
return this.MicrosecondsSinceUnixEpoch.CompareTo(that.MicrosecondsSinceUnixEpoch);
}
Expand Down Expand Up @@ -428,7 +428,9 @@ public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
/// This type has less precision than TimeSpan (units of microseconds rather than units of 100ns).
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public record struct TimeDuration(long Microseconds) : IStructuralReadWrite
public record struct TimeDuration(long Microseconds)
: IStructuralReadWrite,
IComparable<TimeDuration>
{
public static readonly TimeDuration ZERO = new(0);

Expand Down Expand Up @@ -457,6 +459,22 @@ public override readonly string ToString()
return $"{sign}{secs}.{microsRemaining:D6}";
}

/// <inheritdoc cref="IComparable{T}.CompareTo(T)" />
public readonly int CompareTo(TimeDuration that)
{
return this.Microseconds.CompareTo(that.Microseconds);
}

public static bool operator <(TimeDuration l, TimeDuration r)
{
return l.CompareTo(r) == -1;
}

public static bool operator >(TimeDuration l, TimeDuration r)
{
return l.CompareTo(r) == 1;
}

// --- auto-generated ---
public void ReadFields(BinaryReader reader)
{
Expand Down
8 changes: 4 additions & 4 deletions crates/bindings/tests/ui/tables.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ error[E0277]: `&'a Alpha` cannot appear as an argument to an index filtering ope
&RawMiscModuleExportV9
&TableAccess
&TableType
&bool
&ethnum::int::I256
&TimeDuration
&[u8]
and $N others
note: required by a bound in `UniqueColumn::<Tbl, <Col as Column>::ColType, Col>::find`
--> src/table.rs
Expand All @@ -159,8 +159,8 @@ error[E0277]: the trait bound `Alpha: IndexScanRangeBounds<(Alpha,), SingleBound
&RawMiscModuleExportV9
&TableAccess
&TableType
&bool
&ethnum::int::I256
&TimeDuration
&[u8]
and $N others
= note: required for `Alpha` to implement `IndexScanRangeBounds<(Alpha,), SingleBound>`
note: required by a bound in `RangedIndex::<Tbl, IndexType, Idx>::filter`
Expand Down
162 changes: 162 additions & 0 deletions crates/codegen/tests/snapshots/codegen__codegen_csharp.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,7 @@ namespace SpacetimeDB
{
public RemoteTables(DbConnection conn)
{
AddTable(Filterable = new(conn));
AddTable(HasSpecialStuff = new(conn));
AddTable(LoggedOutPlayer = new(conn));
AddTable(Person = new(conn));
Expand Down Expand Up @@ -1538,6 +1539,107 @@ namespace SpacetimeDB
}
}
'''
"Tables/Filterable.g.cs" = '''
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.

VERSION_COMMENT

#nullable enable

using System;
using SpacetimeDB.BSATN;
using SpacetimeDB.ClientApi;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace SpacetimeDB
{
public sealed partial class RemoteTables
{
public sealed class FilterableHandle : RemoteTableHandle<EventContext, Filterable>
{
protected override string RemoteTableName => "filterable";

public sealed class BlobIndex : BTreeIndexBase<System.Collections.Generic.List<byte>>
{
protected override System.Collections.Generic.List<byte> GetKey(Filterable row) => row.Blob;

public BlobIndex(FilterableHandle table) : base(table) { }
}

public readonly BlobIndex Blob;

public sealed class ConnectionIdIndex : BTreeIndexBase<SpacetimeDB.ConnectionId>
{
protected override SpacetimeDB.ConnectionId GetKey(Filterable row) => row.ConnectionId;

public ConnectionIdIndex(FilterableHandle table) : base(table) { }
}

public readonly ConnectionIdIndex ConnectionId;

public sealed class IdentityIndex : BTreeIndexBase<SpacetimeDB.Identity>
{
protected override SpacetimeDB.Identity GetKey(Filterable row) => row.Identity;

public IdentityIndex(FilterableHandle table) : base(table) { }
}

public readonly IdentityIndex Identity;

public sealed class StrIndex : BTreeIndexBase<string>
{
protected override string GetKey(Filterable row) => row.Str;

public StrIndex(FilterableHandle table) : base(table) { }
}

public readonly StrIndex Str;

public sealed class TestCIndex : BTreeIndexBase<NamespaceTestC>
{
protected override NamespaceTestC GetKey(Filterable row) => row.TestC;

public TestCIndex(FilterableHandle table) : base(table) { }
}

public readonly TestCIndex TestC;

public sealed class TimeDurationIndex : BTreeIndexBase<SpacetimeDB.TimeDuration>
{
protected override SpacetimeDB.TimeDuration GetKey(Filterable row) => row.TimeDuration;

public TimeDurationIndex(FilterableHandle table) : base(table) { }
}

public readonly TimeDurationIndex TimeDuration;

public sealed class TimestampIndex : BTreeIndexBase<SpacetimeDB.Timestamp>
{
protected override SpacetimeDB.Timestamp GetKey(Filterable row) => row.Timestamp;

public TimestampIndex(FilterableHandle table) : base(table) { }
}

public readonly TimestampIndex Timestamp;

internal FilterableHandle(DbConnection conn) : base(conn)
{
Blob = new(this);
ConnectionId = new(this);
Identity = new(this);
Str = new(this);
TestC = new(this);
TimeDuration = new(this);
Timestamp = new(this);
}
}

public readonly FilterableHandle Filterable;
}
}
'''
"Tables/HasSpecialStuff.g.cs" = '''
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
Expand Down Expand Up @@ -2105,6 +2207,66 @@ namespace SpacetimeDB
}
}
'''
"Types/Filterable.g.cs" = '''
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.

VERSION_COMMENT

#nullable enable

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace SpacetimeDB
{
[SpacetimeDB.Type]
[DataContract]
public sealed partial class Filterable
{
[DataMember(Name = "str")]
public string Str;
[DataMember(Name = "identity")]
public SpacetimeDB.Identity Identity;
[DataMember(Name = "connection_id")]
public SpacetimeDB.ConnectionId ConnectionId;
[DataMember(Name = "test_c")]
public NamespaceTestC TestC;
[DataMember(Name = "timestamp")]
public SpacetimeDB.Timestamp Timestamp;
[DataMember(Name = "time_duration")]
public SpacetimeDB.TimeDuration TimeDuration;
[DataMember(Name = "blob")]
public System.Collections.Generic.List<byte> Blob;

public Filterable(
string Str,
SpacetimeDB.Identity Identity,
SpacetimeDB.ConnectionId ConnectionId,
NamespaceTestC TestC,
SpacetimeDB.Timestamp Timestamp,
SpacetimeDB.TimeDuration TimeDuration,
System.Collections.Generic.List<byte> Blob
)
{
this.Str = Str;
this.Identity = Identity;
this.ConnectionId = ConnectionId;
this.TestC = TestC;
this.Timestamp = Timestamp;
this.TimeDuration = TimeDuration;
this.Blob = Blob;
}

public Filterable()
{
this.Str = "";
this.Blob = new();
}
}
}
'''
"Types/Foobar.g.cs" = '''
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
Expand Down
Loading
Loading