From c299b728dd40eacb452bdfb3147976e0c2cfd1f2 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 28 Nov 2026 01:16:35 +0100 Subject: [PATCH] C# contract: every list member is an ImmutableArray whole-value holder (no wire change) Co-Authored-By: Claude Sonnet 5.5 --- crates/thing-contract/src/asset.rs | 2 +- crates/thing-contract/src/command.rs | 2 +- crates/thing-contract/src/node.rs | 4 ++-- packages/GameCult.Thing.Contract/Asset.cs | 3 ++- packages/GameCult.Thing.Contract/Command.cs | 5 +++-- packages/GameCult.Thing.Contract/Domain.cs | 16 ++++++++-------- packages/GameCult.Thing.Contract/Node.cs | 17 +++++++++-------- packages/GameCult.Thing.Contract/Source.cs | 3 ++- packages/GameCult.Thing.Contract/Style.cs | 5 +++-- .../NameSetTests.cs | 2 +- .../ReadOnlyListTests.cs | 1 + tests/thing-contract-parity/README.md | 2 +- tests/thing-contract-parity/catalog-members.tsv | 8 ++++---- 13 files changed, 38 insertions(+), 32 deletions(-) diff --git a/crates/thing-contract/src/asset.rs b/crates/thing-contract/src/asset.rs index 2e43a1e..0827521 100644 --- a/crates/thing-contract/src/asset.rs +++ b/crates/thing-contract/src/asset.rs @@ -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")] + #[cultcache(key = 3, type_name = "System.Collections.Immutable.ImmutableArray")] pub assets: Vec, } diff --git a/crates/thing-contract/src/command.rs b/crates/thing-contract/src/command.rs index eb0984f..9daf1c3 100644 --- a/crates/thing-contract/src/command.rs +++ b/crates/thing-contract/src/command.rs @@ -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")] + #[cultcache(key = 5, type_name = "System.Collections.Immutable.ImmutableArray")] pub captured: Vec, #[cultcache(key = 6)] pub client: String, diff --git a/crates/thing-contract/src/node.rs b/crates/thing-contract/src/node.rs index 2986865..8be831c 100644 --- a/crates/thing-contract/src/node.rs +++ b/crates/thing-contract/src/node.rs @@ -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")] + #[cultcache(key = 5, type_name = "System.Collections.Immutable.ImmutableArray")] pub commands: Vec, } @@ -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")] + #[cultcache(key = 3, type_name = "System.Collections.Immutable.ImmutableArray")] pub params: Vec, #[cultcache(key = 4)] pub body: Node, diff --git a/packages/GameCult.Thing.Contract/Asset.cs b/packages/GameCult.Thing.Contract/Asset.cs index cc0054d..f7ef90e 100644 --- a/packages/GameCult.Thing.Contract/Asset.cs +++ b/packages/GameCult.Thing.Contract/Asset.cs @@ -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 Assets { get; } + [Key(3)] public ImmutableArray Assets { get; } public AssetCatalog(string providerId, string catalogId, long version, IReadOnlyList assets) { diff --git a/packages/GameCult.Thing.Contract/Command.cs b/packages/GameCult.Thing.Contract/Command.cs index 193574c..eae7dbc 100644 --- a/packages/GameCult.Thing.Contract/Command.cs +++ b/packages/GameCult.Thing.Contract/Command.cs @@ -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 Captures { get; } + [Key(2)] public ImmutableArray Captures { get; } [Key(3)] public string? Confirm { get; } public CommandDecl(string id, string payloadSchema, IReadOnlyList 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 Captured { get; } + [Key(5)] public ImmutableArray Captured { get; } [Key(6)] public string Client { get; } [Key(7)] public string IssuedAt { get; } [Key(8)] public string? Delegation { get; } diff --git a/packages/GameCult.Thing.Contract/Domain.cs b/packages/GameCult.Thing.Contract/Domain.cs index ca23b16..b9d865b 100644 --- a/packages/GameCult.Thing.Contract/Domain.cs +++ b/packages/GameCult.Thing.Contract/Domain.cs @@ -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"); /// - /// 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. /// - public static IReadOnlyList Items(IReadOnlyList? items, string slot) where T : class + public static ImmutableArray Items(IReadOnlyList? 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 { IsDefault: true }) throw new ThingDomainException(slot, "present"); + var builder = ImmutableArray.CreateBuilder(items.Count); + foreach (var item in items) builder.Add(item ?? throw new ThingDomainException(slot, "free of nil items")); + return builder.MoveToImmutable(); } } diff --git a/packages/GameCult.Thing.Contract/Node.cs b/packages/GameCult.Thing.Contract/Node.cs index 8b8bdd6..2ae7423 100644 --- a/packages/GameCult.Thing.Contract/Node.cs +++ b/packages/GameCult.Thing.Contract/Node.cs @@ -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 Children { get; } + [Key(4)] public ImmutableArray Children { get; } public BoxNode(NodeCommon common, BoxStyle style, Paint paint, Role role, IReadOnlyList 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 Content { get; } + [Key(1)] public ImmutableArray 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 Children { get; } + [Key(4)] public ImmutableArray Children { get; } public OverlayNode(NodeCommon common, Anchor anchor, bool modal, CommandRef dismiss, IReadOnlyList 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 Args { get; } + [Key(2)] public ImmutableArray Args { get; } public UseNode(NodeCommon common, CompositionRef composition, IReadOnlyList args) { @@ -344,7 +345,7 @@ public abstract class OptionList [MessagePackObject] public sealed class LiteralOptions : OptionList { - [Key(0)] public IReadOnlyList Options { get; } + [Key(0)] public ImmutableArray Options { get; } public LiteralOptions(IReadOnlyList options) { @@ -502,7 +503,7 @@ public sealed class CommandArg : ArgValue [MessagePackObject] public sealed class SlotArg : ArgValue { - [Key(0)] public IReadOnlyList Nodes { get; } + [Key(0)] public ImmutableArray Nodes { get; } public SlotArg(IReadOnlyList 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 Commands { get; } + [Key(5)] public ImmutableArray Commands { get; } public SurfaceDocument(string providerId, string label, long version, string title, Node root, IReadOnlyList 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 Params { get; } + [Key(3)] public ImmutableArray Params { get; } [Key(4)] public Node Body { get; } public CompositionDocument(string publisher, string name, long version, IReadOnlyList @params, Node body) diff --git a/packages/GameCult.Thing.Contract/Source.cs b/packages/GameCult.Thing.Contract/Source.cs index b794345..ad8120c 100644 --- a/packages/GameCult.Thing.Contract/Source.cs +++ b/packages/GameCult.Thing.Contract/Source.cs @@ -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 Steps { get; } + [Key(0)] public ImmutableArray Steps { get; } public MemberPath(IReadOnlyList steps) { diff --git a/packages/GameCult.Thing.Contract/Style.cs b/packages/GameCult.Thing.Contract/Style.cs index 7aeb814..23fd0da 100644 --- a/packages/GameCult.Thing.Contract/Style.cs +++ b/packages/GameCult.Thing.Contract/Style.cs @@ -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 GridColumns { get; } - [Key(20)] public IReadOnlyList GridRows { get; } + [Key(19)] public ImmutableArray GridColumns { get; } + [Key(20)] public ImmutableArray 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 gridColumns, IReadOnlyList gridRows) { diff --git a/tests/GameCult.Thing.Contract.Tests/NameSetTests.cs b/tests/GameCult.Thing.Contract.Tests/NameSetTests.cs index 30341cb..fa05164 100644 --- a/tests/GameCult.Thing.Contract.Tests/NameSetTests.cs +++ b/tests/GameCult.Thing.Contract.Tests/NameSetTests.cs @@ -71,7 +71,7 @@ public sealed class NameSetTests var error = Assert.Throws(() => 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", + var listed = Persisted(typeof(AssetCatalog), "System.Collections.Immutable.ImmutableArray", "System.Collections.Generic.List"); Assert.Throws(() => Wire.Registry.ResolvePersistedSchema(listed.SchemaId, new[] { listed }), "a store written when Assets was a List is refused"); diff --git a/tests/GameCult.Thing.Contract.Tests/ReadOnlyListTests.cs b/tests/GameCult.Thing.Contract.Tests/ReadOnlyListTests.cs index 6a59403..34caade 100644 --- a/tests/GameCult.Thing.Contract.Tests/ReadOnlyListTests.cs +++ b/tests/GameCult.Thing.Contract.Tests/ReadOnlyListTests.cs @@ -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<,>)))); diff --git a/tests/thing-contract-parity/README.md b/tests/thing-contract-parity/README.md index 4b2c59a..017de16 100644 --- a/tests/thing-contract-parity/README.md +++ b/tests/thing-contract-parity/README.md @@ -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`. | +| `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`. | | `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`). | diff --git a/tests/thing-contract-parity/catalog-members.tsv b/tests/thing-contract-parity/catalog-members.tsv index 2f449fb..725242d 100644 --- a/tests/thing-contract-parity/catalog-members.tsv +++ b/tests/thing-contract-parity/catalog-members.tsv @@ -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 +sha256:186674dff7658d23646b25b28f54d27b3f05cee34b51a631580be65002c41aa8 5 Captured System.Collections.Immutable.ImmutableArray 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 +sha256:3d751a6fb07487b0c84cf0ae1c4fe55e5ef0c1656eab41ca3236846a638bdd3f 3 Assets System.Collections.Immutable.ImmutableArray 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 +sha256:531f96346fbff5160daedc82ada2ad5cb3d0477631d2b580ccd1a2834f041b32 3 Params System.Collections.Immutable.ImmutableArray 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 +sha256:da2ca90e225d803191fd9a31ad1894e7dfab186f82e892717be8130d2e7ebf33 5 Commands System.Collections.Immutable.ImmutableArray sha256:e0273199abadb60da0bd9a7bcb863e131ef08bb2284f7eb729f76159caa369fd 0 InvocationId System.String sha256:e0273199abadb60da0bd9a7bcb863e131ef08bb2284f7eb729f76159caa369fd 1 Seq System.Int32 sha256:e0273199abadb60da0bd9a7bcb863e131ef08bb2284f7eb729f76159caa369fd 2 Outcome GameCult.Thing.Contract.Outcome