mirror of
https://github.com/BillyCool/MariesWonderland.git
synced 2026-05-06 12:53:38 +02:00
Initial commit
This commit is contained in:
30
src/MasterMemory/IntIntValueTupleFormatter.cs
Normal file
30
src/MasterMemory/IntIntValueTupleFormatter.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using MessagePack;
|
||||
using MessagePack.Formatters;
|
||||
|
||||
namespace MariesWonderland.MasterMemory;
|
||||
|
||||
/// <summary>
|
||||
/// MessagePack formatter for <c>(int, int)</c> value tuples stored as 2-element arrays
|
||||
/// in the master database binary header.
|
||||
/// </summary>
|
||||
internal sealed class IntIntValueTupleFormatter : IMessagePackFormatter<(int, int)>
|
||||
{
|
||||
public void Serialize(ref MessagePackWriter writer, (int, int) value, MessagePackSerializerOptions options)
|
||||
{
|
||||
writer.WriteArrayHeader(2);
|
||||
writer.WriteInt32(value.Item1);
|
||||
writer.WriteInt32(value.Item2);
|
||||
}
|
||||
|
||||
public (int, int) Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (reader.IsNil)
|
||||
throw new InvalidOperationException("Header tuple entry is nil.");
|
||||
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 2)
|
||||
throw new InvalidOperationException($"Expected 2-element tuple, got {count}.");
|
||||
|
||||
return (reader.ReadInt32(), reader.ReadInt32());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user