Open
Description
I created the following template:
[TStructId]
file readonly partial record struct TSelf(long Value)
{
[DebuggerStepThrough]
public static TSelf operator +(TSelf left, TSelf right) => new(left.Value + right.Value);
}
file readonly partial record struct TSelf : INanoValueAddition;
I expected that, since NanoTime
does not inherit INanoValueAddition
, the addition template would not be applied to it. Here's the NanoTime
value id:
[ProtoContract]
[DebuggerDisplay("{Value} - {ToDateTimeImprecise()}")]
public readonly partial record struct NanoTime([property: ProtoMember(1)] ulong Value) : IStructId<ulong>
{
private static readonly long _epochTicks = DateTime.UnixEpoch.Ticks;
[DebuggerStepThrough]
public static NanoTime Now() => FromDateTime(DateTime.UtcNow);
// changes to these implementations can cause unnoticed overflow errors.
[DebuggerStepThrough]
public DateTime ToDateTimeImprecise()
{
var ticks = (long)(Value / TimeSpan.NanosecondsPerTick) + _epochTicks;
return new DateTime(ticks, DateTimeKind.Utc);
}
[DebuggerStepThrough]
public static NanoTime FromDateTime(DateTime value)
{
// Protobuf deserializes datetimes as `Unspecified`, so make allowance for those values here.
if (value.Kind is not DateTimeKind.Utc and not DateTimeKind.Unspecified)
throw new ArgumentException("Value must be UTC", nameof(value));
if (value.Ticks < _epochTicks)
throw new ArgumentOutOfRangeException(nameof(value), "Value must not be earlier than the Unix epoch.");
return new((ulong)(value.Ticks - _epochTicks) * TimeSpan.NanosecondsPerTick);
}
}
Here's the "offending" generated partial file that incorrectly adds the addition template to it: