Cysharp / Cysharp/MemoryPack

Source Generator didn't implement sufficient interfaces under certain circumstances

Open
#421 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stale
Dominant language
C#
Stars
4.7k
Forks
313
PR merge metrics
No merged PRs in 30d

Description

The following code won't compile; the error translates to:

Unimplmented interface member: 'void MemoryPack.IMemoryPackable<Data>.Deserialize(ref MemoryPackReader, scoped ref Data)'
using System.Buffers;
using System.Numerics;
using System.Runtime.InteropServices;
using MemoryPack;

internal class Program
{
    public static void Main(string[] args)
    {
        var writer = new MemoryPackWriter();

        writer.Write(new Data([1,2,3,4,5]));
        writer.Write(new Data([6,7,8,9,10]));
        writer.Write(new Data([11,12,13,14,15]));
        writer.Write(new Data([16,17,18,19,20]));

        var span = writer.AsSpan();

        var reader = new MemoryPackReader();
        reader.Reload(span);

        var deserialized1 = reader.Read<Data>();
        var deserialized2 = reader.Read<Data>();
        var deserialized3 = reader.Read<Data>();
        var deserialized4 = reader.Read<Data>();

        Console.WriteLine();
    }
}

[MemoryPackable]
partial record struct Data(byte[] Payload);

public class MemoryPackReader
{
    private readonly List<byte> _buffer = new(2048);
    private int _index;
    
    public void Reload(ReadOnlySpan<byte> span)
    {
        _buffer.Clear();
        _buffer.AddRange(span);
    }
    
    public T? Read<T>()
    {
        var span = CollectionsMarshal.AsSpan(_buffer)[_index..];
        T? value = default;
        var bytes = MemoryPackSerializer.Deserialize(span, ref value);
        _index += bytes;
        return value;
    }
}

public class MemoryPackWriter : IBufferWriter<byte>
{
    public void Write<T>(T value) => MemoryPackSerializer.Serialize(this, value);

    private byte[] _buffer = new byte[2048];
    private int _index;

    void IBufferWriter<byte>.Advance(int count)
    {
        if (_index + count > _buffer.Length)
            throw new InvalidOperationException("Cannot advance beyond the buffer size.");
        _index += count;
    }

    public Memory<byte> GetMemory(int sizeHint = 0)
    {
        Ensure(sizeHint);
        return _buffer.AsMemory(_index);
    }

    public Span<byte> GetSpan(int sizeHint = 0)
    {
        Ensure(sizeHint);
        return _buffer.AsSpan(_index);
    }

    private void Ensure(int sizeHint)
    {
        ArgumentOutOfRangeException.ThrowIfNegative(sizeHint);
        var available = _buffer.Length - _index;
        if (available >= sizeHint) return;
        var newSize = BitOperations.RoundUpToPowerOf2((uint)(_buffer.Length + sizeHint - available));
        Array.Resize(ref _buffer, (int)newSize);
    }
    
    public ReadOnlySpan<byte> AsSpan() => _buffer.AsSpan(0, _index);

    public void Clear()
    {
        Array.Clear(_buffer, 0, _index);
        _index = 0;
    }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the provided C# reproduction and inspect the generated implementation for the [MemoryPackable] partial record struct Data. Verify the required IMemoryPackable members, then rerun the sample to confirm all four values serialize and deserialize successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.