-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add BigInteger codec #9669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add BigInteger codec #9669
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,84 @@ | ||
| using System; | ||
| using System.Buffers; | ||
| using System.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
| using Orleans.Serialization.Buffers; | ||
| using Orleans.Serialization.WireProtocol; | ||
|
|
||
| namespace Orleans.Serialization.Codecs; | ||
|
|
||
| /// <summary> | ||
| /// Serializer for <see cref="BigInteger"/>. | ||
| /// </summary> | ||
| [RegisterSerializer] | ||
| public sealed class BigIntegerCodec : IFieldCodec<BigInteger> | ||
| { | ||
| /// <inheritdoc/> | ||
| void IFieldCodec<BigInteger>.WriteField<TBufferWriter>(ref Writer<TBufferWriter> writer, uint fieldIdDelta, | ||
| Type expectedType, BigInteger value) | ||
| { | ||
| ReferenceCodec.MarkValueField(writer.Session); | ||
| writer.WriteFieldHeader(fieldIdDelta, expectedType, typeof(BigInteger), WireType.LengthPrefixed); | ||
|
|
||
| WriteField(ref writer, value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes a field without type info (expected type is statically known). | ||
| /// </summary> | ||
| /// <typeparam name="TBufferWriter">The buffer writer type.</typeparam> | ||
| /// <param name="writer">The writer.</param> | ||
| /// <param name="fieldIdDelta">The field identifier delta.</param> | ||
| /// <param name="value">The value.</param> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static void WriteField<TBufferWriter>(ref Writer<TBufferWriter> writer, uint fieldIdDelta, BigInteger value) where TBufferWriter : IBufferWriter<byte> | ||
| { | ||
| ReferenceCodec.MarkValueField(writer.Session); | ||
| writer.WriteFieldHeaderExpected(fieldIdDelta, WireType.LengthPrefixed); | ||
|
|
||
| WriteField(ref writer, value); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| private static void WriteField<TBufferWriter>(ref Writer<TBufferWriter> writer, BigInteger value) | ||
| where TBufferWriter : IBufferWriter<byte> | ||
| { | ||
| var byteCount = value.GetByteCount(); | ||
| writer.WriteVarUInt32((uint)byteCount); | ||
|
|
||
| writer.EnsureContiguous(byteCount); | ||
| if (value.TryWriteBytes(writer.WritableSpan, out var bytesWritten)) | ||
| { | ||
| writer.AdvanceSpan(bytesWritten); | ||
| } | ||
| else | ||
| { | ||
| writer.Write(value.ToByteArray()); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| BigInteger IFieldCodec<BigInteger>.ReadValue<TInput>(ref Reader<TInput> reader, Field field) => ReadValue(ref reader, field); | ||
|
|
||
| /// <summary> | ||
| /// Reads a value. | ||
| /// </summary> | ||
| /// <typeparam name="TInput">The reader input type.</typeparam> | ||
| /// <param name="reader">The reader.</param> | ||
| /// <param name="field">The field.</param> | ||
| /// <returns>The value.</returns> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static BigInteger ReadValue<TInput>(ref Reader<TInput> reader, Field field) | ||
| { | ||
| ReferenceCodec.MarkValueField(reader.Session); | ||
|
|
||
| if (field.WireType != WireType.LengthPrefixed) | ||
| { | ||
| throw new UnexpectedLengthPrefixValueException(nameof(BigInteger), 0, 0); | ||
| } | ||
|
|
||
| var length = reader.ReadVarUInt32(); | ||
| var bytes = reader.ReadBytes(length); | ||
| return new BigInteger(bytes); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.