jamescourtney / FlatSharp

Fast, idiomatic C# implementation of Flatbuffers
Apache License 2.0
510 stars 51 forks source link

Undocumented parameters required for Serializer.Write and Serializer.Parse #441

Closed Theome closed 2 months ago

Theome commented 2 months ago

I followed the example code from Example00-Basics with a custom class MessagePacket, but the following code won't compile:

MessagePacket packet = /* ... */
int maxSize = MessagePacket.Serializer.GetMaxSize(packet);
byte[] buffer = new byte[maxSize];
int bytesWritten = MessagePacket.Serializer.Write(buffer, packet);

The generated Write method has an additional TSpanWriter parameter:

int Write<TSpanWriter>(TSpanWriter writer, Span<byte> destination, T item) where TSpanWriter : ISpanWriter;

Apparently I can provide a default SpanWriter like this:

int bytesWritten = MessagePacket.Serializer.Write(default(FlatSharp.SpanWriter), buffer, packet);

And now the code works as intended, but I don't know what this means. Where does the extra parameter come from, and what am I supposed to do? Am I doing something wrong, or did the API change and the examples haven't been updated yet?

Similarly, the following won't compile either:

byte[] data = /* ... */
var messagePacket = MessagePacket.Serializer.Parse(data);

as the Parse method expects an ArrayInputBuffer instead of a byte[] array. I can fix that like this:

var buffer = new FlatSharp.ArrayInputBuffer(data);
var messagePacket = MessagePacket.Serializer.Parse(buffer);

But again, I don't know why the method looks like that.

Could someone please clarify this?

Theome commented 2 months ago

I found out this is a duplicate of #286. Adding using FlatSharp; makes the extension methods from ISerializerExtensions.cs available that are used in the example code.