Skip to content

Commit 0c0e262

Browse files
committed
Make Timestam, TimeDuration,Vec<u8> implement FilterableValue
1 parent 5e0c0f7 commit 0c0e262

File tree

13 files changed

+574
-25
lines changed

13 files changed

+574
-25
lines changed

crates/bindings-csharp/BSATN.Runtime.Tests/Tests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,26 @@ public static void TimestampConversionChecks()
165165

166166
var newIntervalUs = 333L;
167167
var newInterval = new TimeDuration(newIntervalUs);
168+
var laterInterval = new TimeDuration(newIntervalUs + 1);
168169
var laterStamp = stamp + newInterval;
169170
Assert.Equal(laterStamp.MicrosecondsSinceUnixEpoch, us + newIntervalUs);
170171
Assert.Equal(laterStamp.TimeDurationSince(stamp), newInterval);
171172

172173
#pragma warning disable CS1718
173174
Assert.True(stamp == stamp);
175+
Assert.True(newInterval == newInterval);
174176
#pragma warning restore CS1718
175177
Assert.False(stamp == laterStamp);
176178
Assert.True(stamp < laterStamp);
177179
Assert.False(laterStamp < stamp);
178180
Assert.Equal(-1, stamp.CompareTo(laterStamp));
179181
Assert.Equal(+1, laterStamp.CompareTo(stamp));
182+
183+
Assert.False(newInterval == laterInterval);
184+
Assert.True(newInterval < laterInterval);
185+
Assert.False(laterInterval < newInterval);
186+
Assert.Equal(-1, newInterval.CompareTo(laterInterval));
187+
Assert.Equal(+1, laterInterval.CompareTo(newInterval));
180188
}
181189

182190
[Fact]

crates/bindings-csharp/BSATN.Runtime/Builtins.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public readonly TimeDuration TimeDurationSince(Timestamp earlier) =>
365365
public static Timestamp operator -(Timestamp point, TimeDuration interval) =>
366366
new Timestamp(checked(point.MicrosecondsSinceUnixEpoch - interval.Microseconds));
367367

368-
public int CompareTo(Timestamp that)
368+
public readonly int CompareTo(Timestamp that)
369369
{
370370
return this.MicrosecondsSinceUnixEpoch.CompareTo(that.MicrosecondsSinceUnixEpoch);
371371
}
@@ -428,7 +428,9 @@ public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
428428
/// This type has less precision than TimeSpan (units of microseconds rather than units of 100ns).
429429
/// </summary>
430430
[StructLayout(LayoutKind.Sequential)]
431-
public record struct TimeDuration(long Microseconds) : IStructuralReadWrite
431+
public record struct TimeDuration(long Microseconds)
432+
: IStructuralReadWrite,
433+
IComparable<TimeDuration>
432434
{
433435
public static readonly TimeDuration ZERO = new(0);
434436

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

462+
/// <inheritdoc cref="IComparable{T}.CompareTo(T)" />
463+
public readonly int CompareTo(TimeDuration that)
464+
{
465+
return this.Microseconds.CompareTo(that.Microseconds);
466+
}
467+
468+
public static bool operator <(TimeDuration l, TimeDuration r)
469+
{
470+
return l.CompareTo(r) == -1;
471+
}
472+
473+
public static bool operator >(TimeDuration l, TimeDuration r)
474+
{
475+
return l.CompareTo(r) == 1;
476+
}
477+
460478
// --- auto-generated ---
461479
public void ReadFields(BinaryReader reader)
462480
{

crates/bindings/tests/ui/tables.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ error[E0277]: `&'a Alpha` cannot appear as an argument to an index filtering ope
132132
&RawMiscModuleExportV9
133133
&TableAccess
134134
&TableType
135-
&bool
136-
&ethnum::int::I256
135+
&TimeDuration
136+
&[u8]
137137
and $N others
138138
note: required by a bound in `UniqueColumn::<Tbl, <Col as Column>::ColType, Col>::find`
139139
--> src/table.rs
@@ -159,8 +159,8 @@ error[E0277]: the trait bound `Alpha: IndexScanRangeBounds<(Alpha,), SingleBound
159159
&RawMiscModuleExportV9
160160
&TableAccess
161161
&TableType
162-
&bool
163-
&ethnum::int::I256
162+
&TimeDuration
163+
&[u8]
164164
and $N others
165165
= note: required for `Alpha` to implement `IndexScanRangeBounds<(Alpha,), SingleBound>`
166166
note: required by a bound in `RangedIndex::<Tbl, IndexType, Idx>::filter`

crates/codegen/tests/snapshots/codegen__codegen_csharp.snap

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ namespace SpacetimeDB
10351035
{
10361036
public RemoteTables(DbConnection conn)
10371037
{
1038+
AddTable(Filterable = new(conn));
10381039
AddTable(HasSpecialStuff = new(conn));
10391040
AddTable(LoggedOutPlayer = new(conn));
10401041
AddTable(Person = new(conn));
@@ -1538,6 +1539,67 @@ namespace SpacetimeDB
15381539
}
15391540
}
15401541
'''
1542+
"Tables/Filterable.g.cs" = '''
1543+
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
1544+
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
1545+
1546+
VERSION_COMMENT
1547+
1548+
#nullable enable
1549+
1550+
using System;
1551+
using SpacetimeDB.BSATN;
1552+
using SpacetimeDB.ClientApi;
1553+
using System.Collections.Generic;
1554+
using System.Runtime.Serialization;
1555+
1556+
namespace SpacetimeDB
1557+
{
1558+
public sealed partial class RemoteTables
1559+
{
1560+
public sealed class FilterableHandle : RemoteTableHandle<EventContext, Filterable>
1561+
{
1562+
protected override string RemoteTableName => "filterable";
1563+
1564+
public sealed class BlobIndex : BTreeIndexBase<System.Collections.Generic.List<byte>>
1565+
{
1566+
protected override System.Collections.Generic.List<byte> GetKey(Filterable row) => row.Blob;
1567+
1568+
public BlobIndex(FilterableHandle table) : base(table) { }
1569+
}
1570+
1571+
public readonly BlobIndex Blob;
1572+
1573+
public sealed class TimeDurationIndex : BTreeIndexBase<SpacetimeDB.TimeDuration>
1574+
{
1575+
protected override SpacetimeDB.TimeDuration GetKey(Filterable row) => row.TimeDuration;
1576+
1577+
public TimeDurationIndex(FilterableHandle table) : base(table) { }
1578+
}
1579+
1580+
public readonly TimeDurationIndex TimeDuration;
1581+
1582+
public sealed class TimestampIndex : BTreeIndexBase<SpacetimeDB.Timestamp>
1583+
{
1584+
protected override SpacetimeDB.Timestamp GetKey(Filterable row) => row.Timestamp;
1585+
1586+
public TimestampIndex(FilterableHandle table) : base(table) { }
1587+
}
1588+
1589+
public readonly TimestampIndex Timestamp;
1590+
1591+
internal FilterableHandle(DbConnection conn) : base(conn)
1592+
{
1593+
Blob = new(this);
1594+
TimeDuration = new(this);
1595+
Timestamp = new(this);
1596+
}
1597+
}
1598+
1599+
public readonly FilterableHandle Filterable;
1600+
}
1601+
}
1602+
'''
15411603
"Tables/HasSpecialStuff.g.cs" = '''
15421604
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
15431605
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
@@ -2105,6 +2167,49 @@ namespace SpacetimeDB
21052167
}
21062168
}
21072169
'''
2170+
"Types/Filterable.g.cs" = '''
2171+
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
2172+
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
2173+
2174+
VERSION_COMMENT
2175+
2176+
#nullable enable
2177+
2178+
using System;
2179+
using System.Collections.Generic;
2180+
using System.Runtime.Serialization;
2181+
2182+
namespace SpacetimeDB
2183+
{
2184+
[SpacetimeDB.Type]
2185+
[DataContract]
2186+
public sealed partial class Filterable
2187+
{
2188+
[DataMember(Name = "timestamp")]
2189+
public SpacetimeDB.Timestamp Timestamp;
2190+
[DataMember(Name = "time_duration")]
2191+
public SpacetimeDB.TimeDuration TimeDuration;
2192+
[DataMember(Name = "blob")]
2193+
public System.Collections.Generic.List<byte> Blob;
2194+
2195+
public Filterable(
2196+
SpacetimeDB.Timestamp Timestamp,
2197+
SpacetimeDB.TimeDuration TimeDuration,
2198+
System.Collections.Generic.List<byte> Blob
2199+
)
2200+
{
2201+
this.Timestamp = Timestamp;
2202+
this.TimeDuration = TimeDuration;
2203+
this.Blob = Blob;
2204+
}
2205+
2206+
public Filterable()
2207+
{
2208+
this.Blob = new();
2209+
}
2210+
}
2211+
}
2212+
'''
21082213
"Types/Foobar.g.cs" = '''
21092214
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
21102215
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.

0 commit comments

Comments
 (0)