GameCult.Thing.Contract: a nil payload is still refused after Bytes became ReadOnlyMemory<byte>

The nil-positions vector found the regression (invocation-all-inputs 4.1: the
view decodes nil to default and was accepted though Rust refuses). ThingDomain.Present
for byte views refuses an empty view with no backing store; an empty array stays a
present, empty payload. Test pins both and the empty payload's wire round trip.
The commits before this one fail NilPositionTests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-14 04:27:37 +02:00
commit 9d8f5706ad
3 changed files with 23 additions and 1 deletions

View file

@ -113,7 +113,7 @@ public sealed class RecordPayload
public RecordPayload(string schema, ReadOnlyMemory<byte> bytes)
{
Schema = ThingDomain.Present(schema, "Schema");
Bytes = bytes.ToArray();
Bytes = ThingDomain.Present(bytes, "Bytes").ToArray();
}
}

View file

@ -49,6 +49,15 @@ public static class ThingDomain
public static T Present<T>(T? value, string slot) where T : class =>
value ?? throw new ThingDomainException(slot, "present");
/// <summary>
/// A required byte slot. A null on the wire decodes to a default view with no backing store, so a view that is
/// empty and backed by nothing is refused as nil; an empty array is a present, empty payload.
/// </summary>
public static ReadOnlyMemory<byte> Present(ReadOnlyMemory<byte> value, string slot) =>
value.IsEmpty && !System.Runtime.InteropServices.MemoryMarshal.TryGetArray(value, out _)
? throw new ThingDomainException(slot, "present")
: value;
/// <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.

View file

@ -163,4 +163,17 @@ public sealed class ReadOnlyListTests
Assert.That(slice.Bytes.ToArray(), Is.EqualTo(new byte[] { 5, 6 }));
Assert.That(System.Runtime.InteropServices.MemoryMarshal.TryGetArray(payload.Bytes, out var segment) && ReferenceEquals(segment.Array, buffer), Is.False);
}
[Test]
public void ANilPayloadIsRefusedAndAnEmptyOneIsKeptThroughTheWire()
{
var error = Assert.Throws<ThingDomainException>(() => new RecordPayload("s", default))!;
Assert.That(error.Domain, Is.EqualTo("Bytes"));
Assert.That(error.Bound, Is.EqualTo("present"));
var empty = new CommandInvocation("i", "k", 1, "go", new RecordPayload("s", Array.Empty<byte>()), new List<CapturedInput>(), "c", "t", null);
var decoded = (CommandInvocation)Wire.Read(typeof(CommandInvocation), Wire.Write(empty));
Assert.That(decoded.Payload.Bytes.IsEmpty, Is.True);
Assert.That(Wire.Write(decoded), Is.EqualTo(Wire.Write(empty)));
}
}