| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Buffers; |
| | 4 | | using System.IO.Pipelines; |
| | 5 | | using ZeroC.Slice; |
| | 6 | |
|
| | 7 | | namespace IceRpc.Slice; |
| | 8 | |
|
| | 9 | | /// <summary>Provides an extension method for <see cref="SliceEncoding" /> to create an empty struct payload.</summary> |
| | 10 | | public static class SliceEncodingExtensions |
| | 11 | | { |
| | 12 | | // 4 = varuint62 encoding of the size (1) |
| | 13 | | // 252 = varint32 encoding of the Slice2 tag end marker (-1) |
| 1 | 14 | | private static readonly ReadOnlySequence<byte> _emptyStructPayload = new(new byte[] { 4, 252 }); |
| | 15 | |
|
| | 16 | | /// <summary>Creates the payload of an empty struct.</summary> |
| | 17 | | /// <param name="encoding">The Slice encoding.</param> |
| | 18 | | /// <returns>The payload of an empty struct.</returns> |
| | 19 | | public static PipeReader CreateEmptyStructPayload(this SliceEncoding encoding) => |
| 12 | 20 | | encoding != SliceEncoding.Slice1 ? PipeReader.Create(_emptyStructPayload) : |
| 12 | 21 | | throw new NotSupportedException( |
| 12 | 22 | | $"{nameof(CreateEmptyStructPayload)} is only available for stream-capable Slice encodings."); |
| | 23 | | } |