8000 Template TSelf contraint does not seem to work · Issue #75 · devlooped/StructId · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Template TSelf contraint does not seem to work #75
Open
@truegoodwill

Description

@truegoodwill

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:

Image

Back this issue
Back this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0