-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add a protobuf serializer for UInt128.
- Loading branch information
Showing
7 changed files
with
44 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Buffers.Binary; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using ProtoBuf; | ||
using ProtoBuf.Serializers; | ||
|
||
namespace Exo.Contracts.Ui; | ||
|
||
internal class UInt128Serializer : ISerializer<UInt128> | ||
{ | ||
public SerializerFeatures Features => SerializerFeatures.CategoryRepeated | SerializerFeatures.CategoryScalar | SerializerFeatures.WireTypeString; | ||
|
||
public UInt128 Read(ref ProtoReader.State state, UInt128 value) | ||
{ | ||
Unsafe.SkipInit(out value); | ||
var result = state.ReadBytes(MemoryMarshal.Cast<UInt128, byte>(MemoryMarshal.CreateSpan(ref value, 1))); | ||
if (result.Length != Unsafe.SizeOf<UInt128>()) throw new InvalidDataException(); | ||
return BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value); | ||
} | ||
|
||
public void Write(ref ProtoWriter.State state, UInt128 value) | ||
{ | ||
if (!BitConverter.IsLittleEndian) value = BinaryPrimitives.ReverseEndianness(value); | ||
state.WriteBytes(MemoryMarshal.Cast<UInt128, byte>(MemoryMarshal.CreateSpan(ref value, 1))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters