C# contract: every list member is an ImmutableArray whole-value holder (no wire change)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-28 01:16:35 +01:00
commit c299b728dd
13 changed files with 38 additions and 32 deletions

View file

@ -28,7 +28,7 @@ pub struct AssetCatalog {
pub catalog_id: Segment,
#[cultcache(key = 2)]
pub version: Version,
#[cultcache(key = 3, type_name = "System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.AssetEntry>")]
#[cultcache(key = 3, type_name = "System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.AssetEntry>")]
pub assets: Vec<AssetEntry>,
}

View file

@ -106,7 +106,7 @@ pub struct CommandInvocation {
pub command: CommandId,
#[cultcache(key = 4)]
pub payload: RecordPayload,
#[cultcache(key = 5, type_name = "System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.CapturedInput>")]
#[cultcache(key = 5, type_name = "System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.CapturedInput>")]
pub captured: Vec<CapturedInput>,
#[cultcache(key = 6)]
pub client: String,

View file

@ -445,7 +445,7 @@ pub struct SurfaceDocument {
pub title: String,
#[cultcache(key = 4)]
pub root: Node,
#[cultcache(key = 5, type_name = "System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.CommandDecl>")]
#[cultcache(key = 5, type_name = "System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.CommandDecl>")]
pub commands: Vec<CommandDecl>,
}
@ -465,7 +465,7 @@ pub struct CompositionDocument {
pub name: Segment,
#[cultcache(key = 2)]
pub version: Version,
#[cultcache(key = 3, type_name = "System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.ParamDecl>")]
#[cultcache(key = 3, type_name = "System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.ParamDecl>")]
pub params: Vec<ParamDecl>,
#[cultcache(key = 4)]
pub body: Node,

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using GameCult.Caching;
using MessagePack;
@ -30,7 +31,7 @@ public sealed class AssetCatalog
[Key(0)] public string ProviderId { get; }
[Key(1)] public string CatalogId { get; }
[Key(2)] public long Version { get; }
[Key(3)] public IReadOnlyList<AssetEntry> Assets { get; }
[Key(3)] public ImmutableArray<AssetEntry> Assets { get; }
public AssetCatalog(string providerId, string catalogId, long version, IReadOnlyList<AssetEntry> assets)
{

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using GameCult.Caching;
using MessagePack;
@ -92,7 +93,7 @@ public sealed class CommandDecl
{
[Key(0)] public string Id { get; }
[Key(1)] public string PayloadSchema { get; }
[Key(2)] public IReadOnlyList<string> Captures { get; }
[Key(2)] public ImmutableArray<string> Captures { get; }
[Key(3)] public string? Confirm { get; }
public CommandDecl(string id, string payloadSchema, IReadOnlyList<string> captures, string? confirm)
@ -155,7 +156,7 @@ public sealed class CommandInvocation
[Key(2)] public long SurfaceVersion { get; }
[Key(3)] public string Command { get; }
[Key(4)] public RecordPayload Payload { get; }
[Key(5)] public IReadOnlyList<CapturedInput> Captured { get; }
[Key(5)] public ImmutableArray<CapturedInput> Captured { get; }
[Key(6)] public string Client { get; }
[Key(7)] public string IssuedAt { get; }
[Key(8)] public string? Delegation { get; }

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace GameCult.Thing.Contract;
@ -50,15 +51,14 @@ public static class ThingDomain
value ?? throw new ThingDomainException(slot, "present");
/// <summary>
/// A required list slot: a read-only copy, so the caller's list cannot change the value afterwards. A null list or
/// a null item is refused naming the slot.
/// A required list slot, as a whole-value holder: a copy, so the caller's list cannot change the value afterwards.
/// A null list, a default array (what a nil payload decodes to) or a null item is refused naming the slot.
/// </summary>
public static IReadOnlyList<T> Items<T>(IReadOnlyList<T>? items, string slot) where T : class
public static ImmutableArray<T> Items<T>(IReadOnlyList<T>? items, string slot) where T : class
{
if (items is null) throw new ThingDomainException(slot, "present");
var copy = new T[items.Count];
for (var i = 0; i < copy.Length; i++)
copy[i] = items[i] ?? throw new ThingDomainException(slot, "free of nil items");
return Array.AsReadOnly(copy);
if (items is null || items is ImmutableArray<T> { IsDefault: true }) throw new ThingDomainException(slot, "present");
var builder = ImmutableArray.CreateBuilder<T>(items.Count);
foreach (var item in items) builder.Add(item ?? throw new ThingDomainException(slot, "free of nil items"));
return builder.MoveToImmutable();
}
}

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using GameCult.Caching;
using MessagePack;
@ -30,7 +31,7 @@ public sealed class BoxNode : Node
[Key(1)] public BoxStyle Style { get; }
[Key(2)] public Paint Paint { get; }
[Key(3)] public Role Role { get; }
[Key(4)] public IReadOnlyList<Node> Children { get; }
[Key(4)] public ImmutableArray<Node> Children { get; }
public BoxNode(NodeCommon common, BoxStyle style, Paint paint, Role role, IReadOnlyList<Node> children)
{
@ -86,7 +87,7 @@ public sealed class ImageNode : Node
public sealed class ButtonNode : Node
{
[Key(0)] public NodeCommon Common { get; }
[Key(1)] public IReadOnlyList<Node> Content { get; }
[Key(1)] public ImmutableArray<Node> Content { get; }
[Key(2)] public CommandRef OnActivate { get; }
[Key(3)] public BoolSource Enabled { get; }
@ -197,7 +198,7 @@ public sealed class OverlayNode : Node
[Key(1)] public Anchor Anchor { get; }
[Key(2)] public bool Modal { get; }
[Key(3)] public CommandRef Dismiss { get; }
[Key(4)] public IReadOnlyList<Node> Children { get; }
[Key(4)] public ImmutableArray<Node> Children { get; }
public OverlayNode(NodeCommon common, Anchor anchor, bool modal, CommandRef dismiss, IReadOnlyList<Node> children)
{
@ -256,7 +257,7 @@ public sealed class UseNode : Node
{
[Key(0)] public NodeCommon Common { get; }
[Key(1)] public CompositionRef Composition { get; }
[Key(2)] public IReadOnlyList<Arg> Args { get; }
[Key(2)] public ImmutableArray<Arg> Args { get; }
public UseNode(NodeCommon common, CompositionRef composition, IReadOnlyList<Arg> args)
{
@ -344,7 +345,7 @@ public abstract class OptionList
[MessagePackObject]
public sealed class LiteralOptions : OptionList
{
[Key(0)] public IReadOnlyList<SelectOption> Options { get; }
[Key(0)] public ImmutableArray<SelectOption> Options { get; }
public LiteralOptions(IReadOnlyList<SelectOption> options)
{
@ -502,7 +503,7 @@ public sealed class CommandArg : ArgValue
[MessagePackObject]
public sealed class SlotArg : ArgValue
{
[Key(0)] public IReadOnlyList<Node> Nodes { get; }
[Key(0)] public ImmutableArray<Node> Nodes { get; }
public SlotArg(IReadOnlyList<Node> nodes)
{
@ -642,7 +643,7 @@ public sealed class SurfaceDocument
[Key(2)] public long Version { get; }
[Key(3)] public string Title { get; }
[Key(4)] public Node Root { get; }
[Key(5)] public IReadOnlyList<CommandDecl> Commands { get; }
[Key(5)] public ImmutableArray<CommandDecl> Commands { get; }
public SurfaceDocument(string providerId, string label, long version, string title, Node root, IReadOnlyList<CommandDecl> commands)
{
@ -664,7 +665,7 @@ public sealed class CompositionDocument
[Key(0)] public string Publisher { get; }
[Key(1)] public string Name { get; }
[Key(2)] public long Version { get; }
[Key(3)] public IReadOnlyList<ParamDecl> Params { get; }
[Key(3)] public ImmutableArray<ParamDecl> Params { get; }
[Key(4)] public Node Body { get; }
public CompositionDocument(string publisher, string name, long version, IReadOnlyList<ParamDecl> @params, Node body)

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using GameCult.Caching;
using MessagePack;
@ -327,7 +328,7 @@ public sealed class Bind
[MessagePackObject]
public sealed class MemberPath
{
[Key(0)] public IReadOnlyList<MemberStep> Steps { get; }
[Key(0)] public ImmutableArray<MemberStep> Steps { get; }
public MemberPath(IReadOnlyList<MemberStep> steps)
{

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using GameCult.Caching;
using MessagePack;
@ -438,8 +439,8 @@ public sealed class BoxStyle
[Key(16)] public double Grow { get; }
[Key(17)] public double Shrink { get; }
[Key(18)] public Dimension Basis { get; }
[Key(19)] public IReadOnlyList<Dimension> GridColumns { get; }
[Key(20)] public IReadOnlyList<Dimension> GridRows { get; }
[Key(19)] public ImmutableArray<Dimension> GridColumns { get; }
[Key(20)] public ImmutableArray<Dimension> GridRows { get; }
public BoxStyle(Display display, Direction direction, Wrap wrap, Size2 gap, Edges padding, Edges margin, Size2 size, Size2 minSize, Size2 maxSize, Position position, Edges inset, Overflow overflow, ItemAlign alignItems, ItemAlign alignSelf, ContentAlign alignContent, ContentAlign justifyContent, double grow, double shrink, Dimension basis, IReadOnlyList<Dimension> gridColumns, IReadOnlyList<Dimension> gridRows)
{

View file

@ -71,7 +71,7 @@ public sealed class NameSetTests
var error = Assert.Throws<InvalidOperationException>(() => Wire.Registry.ResolvePersistedSchema(renamed.SchemaId, new[] { renamed }))!;
Assert.That(error.Message, Does.Contain("changed type").And.Contain("System.Int32").And.Contain("System.Int64"));
var listed = Persisted(typeof(AssetCatalog), "System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.AssetEntry>",
var listed = Persisted(typeof(AssetCatalog), "System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.AssetEntry>",
"System.Collections.Generic.List<GameCult.Thing.Contract.AssetEntry>");
Assert.Throws<InvalidOperationException>(() => Wire.Registry.ResolvePersistedSchema(listed.SchemaId, new[] { listed }),
"a store written when Assets was a List is refused");

View file

@ -12,6 +12,7 @@ namespace GameCult.Thing.Contract.Tests;
public sealed class ReadOnlyListTests
{
private static bool IsMutableCollection(Type type) =>
!(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Collections.Immutable.ImmutableArray<>)) &&
(typeof(IList).IsAssignableFrom(type) || type.GetInterfaces().Concat(new[] { type }).Any(i =>
i.IsGenericType && (i.GetGenericTypeDefinition() == typeof(ICollection<>) || i.GetGenericTypeDefinition() == typeof(IDictionary<,>))));

View file

@ -4,7 +4,7 @@ The names the C# package `GameCult.Thing.Contract` gives its types. The C# packa
| File | Rows |
|---|---|
| `catalog-members.tsv` | One row per document slot, `schemaId`, `slot`, `memberName`, `typeName`, as `Wire.Registry`'s catalog entries print them. A list slot reads `System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.X>`. |
| `catalog-members.tsv` | One row per document slot, `schemaId`, `slot`, `memberName`, `typeName`, as `Wire.Registry`'s catalog entries print them. A list slot reads `System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.X>`. |
| `type-names.txt` | The full name of every public type in `GameCult.Thing.Contract`, sorted ordinally. |
| `written-by-cs.cc` | A single-file store the C# package's CultCache wrote from its own builders, one record of each kind. `packages/thing-contract/test/parity.test.ts` reads it. Regenerate: `THING_PARITY_WRITE=1 dotnet test tests/GameCult.Thing.Contract.Tests` (`ParityTests.cs`). |
| `written-by-ts.cc` | The same five records, written by `@gamecult/thing-contract`'s CultCache. `ParityTests.cs` reads it. Regenerate: `THING_PARITY_WRITE=1 npm test` in `packages/thing-contract` (`parity.test.ts`). |

View file

@ -3,25 +3,25 @@ sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 1 Surfac
sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 2 SurfaceVersion System.Int64
sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 3 Command System.String
sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 4 Payload GameCult.Thing.Contract.RecordPayload
sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 5 Captured System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.CapturedInput>
sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 5 Captured System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.CapturedInput>
sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 6 Client System.String
sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 7 IssuedAt System.String
sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 8 Delegation System.String
sha256:3d751a6fb07487b0c84cf0ae1c4fe55e5ef0c1656eab41ca3236846a638bdd3f 0 ProviderId System.String
sha256:3d751a6fb07487b0c84cf0ae1c4fe55e5ef0c1656eab41ca3236846a638bdd3f 1 CatalogId System.String
sha256:3d751a6fb07487b0c84cf0ae1c4fe55e5ef0c1656eab41ca3236846a638bdd3f 2 Version System.Int64
sha256:3d751a6fb07487b0c84cf0ae1c4fe55e5ef0c1656eab41ca3236846a638bdd3f 3 Assets System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.AssetEntry>
sha256:3d751a6fb07487b0c84cf0ae1c4fe55e5ef0c1656eab41ca3236846a638bdd3f 3 Assets System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.AssetEntry>
sha256:531f96346fbff5160daedc82ada2ad5cb3d0477631d2b580ccd1a2834f041b32 0 Publisher System.String
sha256:531f96346fbff5160daedc82ada2ad5cb3d0477631d2b580ccd1a2834f041b32 1 Name System.String
sha256:531f96346fbff5160daedc82ada2ad5cb3d0477631d2b580ccd1a2834f041b32 2 Version System.Int64
sha256:531f96346fbff5160daedc82ada2ad5cb3d0477631d2b580ccd1a2834f041b32 3 Params System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.ParamDecl>
sha256:531f96346fbff5160daedc82ada2ad5cb3d0477631d2b580ccd1a2834f041b32 3 Params System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.ParamDecl>
sha256:531f96346fbff5160daedc82ada2ad5cb3d0477631d2b580ccd1a2834f041b32 4 Body GameCult.Thing.Contract.Node
sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 0 ProviderId System.String
sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 1 Label System.String
sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 2 Version System.Int64
sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 3 Title System.String
sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 4 Root GameCult.Thing.Contract.Node
sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 5 Commands System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.CommandDecl>
sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 5 Commands System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.CommandDecl>
sha256:e0273199abadb60da0bd9a7bcb863e131ef08bb2284f7eb729f76159caa369fd 0 InvocationId System.String
sha256:e0273199abadb60da0bd9a7bcb863e131ef08bb2284f7eb729f76159caa369fd 1 Seq System.Int32
sha256:e0273199abadb60da0bd9a7bcb863e131ef08bb2284f7eb729f76159caa369fd 2 Outcome GameCult.Thing.Contract.Outcome

1 sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 0 InvocationId System.String
3 sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 2 SurfaceVersion System.Int64
4 sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 3 Command System.String
5 sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 4 Payload GameCult.Thing.Contract.RecordPayload
6 sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 5 Captured System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.CapturedInput> System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.CapturedInput>
7 sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 6 Client System.String
8 sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 7 IssuedAt System.String
9 sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 8 Delegation System.String
10 sha256:3d751a6fb07487b0c84cf0ae1c4fe55e5ef0c1656eab41ca3236846a638bdd3f 0 ProviderId System.String
11 sha256:3d751a6fb07487b0c84cf0ae1c4fe55e5ef0c1656eab41ca3236846a638bdd3f 1 CatalogId System.String
12 sha256:3d751a6fb07487b0c84cf0ae1c4fe55e5ef0c1656eab41ca3236846a638bdd3f 2 Version System.Int64
13 sha256:3d751a6fb07487b0c84cf0ae1c4fe55e5ef0c1656eab41ca3236846a638bdd3f 3 Assets System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.AssetEntry> System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.AssetEntry>
14 sha256:531f96346fbff5160daedc82ada2ad5cb3d0477631d2b580ccd1a2834f041b32 0 Publisher System.String
15 sha256:531f96346fbff5160daedc82ada2ad5cb3d0477631d2b580ccd1a2834f041b32 1 Name System.String
16 sha256:531f96346fbff5160daedc82ada2ad5cb3d0477631d2b580ccd1a2834f041b32 2 Version System.Int64
17 sha256:531f96346fbff5160daedc82ada2ad5cb3d0477631d2b580ccd1a2834f041b32 3 Params System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.ParamDecl> System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.ParamDecl>
18 sha256:531f96346fbff5160daedc82ada2ad5cb3d0477631d2b580ccd1a2834f041b32 4 Body GameCult.Thing.Contract.Node
19 sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 0 ProviderId System.String
20 sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 1 Label System.String
21 sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 2 Version System.Int64
22 sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 3 Title System.String
23 sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 4 Root GameCult.Thing.Contract.Node
24 sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 5 Commands System.Collections.Generic.IReadOnlyList<GameCult.Thing.Contract.CommandDecl> System.Collections.Immutable.ImmutableArray<GameCult.Thing.Contract.CommandDecl>
25 sha256:e0273199abadb60da0bd9a7bcb863e131ef08bb2284f7eb729f76159caa369fd 0 InvocationId System.String
26 sha256:e0273199abadb60da0bd9a7bcb863e131ef08bb2284f7eb729f76159caa369fd 1 Seq System.Int32
27 sha256:e0273199abadb60da0bd9a7bcb863e131ef08bb2284f7eb729f76159caa369fd 2 Outcome GameCult.Thing.Contract.Outcome