| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Buffers; |
| | | 4 | | |
| | | 5 | | namespace ZeroC.Slice; |
| | | 6 | | |
| | | 7 | | /// <summary>Provides an extension method for <see cref="SliceEncoder" /> to encode a <see cref="Guid" /> as a |
| | | 8 | | /// <c>WellKnownTypes::Uuid</c>.</summary> |
| | | 9 | | public static class UuidSliceEncoderExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary>Encodes a <see cref="Guid" /> as a <c>WellKnownTypes::Uuid</c>.</summary> |
| | | 12 | | /// <param name="encoder">The Slice encoder.</param> |
| | | 13 | | /// <param name="value">The value to encode.</param> |
| | | 14 | | public static void EncodeUuid(this ref SliceEncoder encoder, Guid value) |
| | 6 | 15 | | { |
| | 6 | 16 | | using IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(16); |
| | 6 | 17 | | Span<byte> span = owner.Memory.Span[..16]; |
| | 6 | 18 | | if (!value.TryWriteBytes(span)) |
| | 0 | 19 | | { |
| | 0 | 20 | | throw new InvalidOperationException($"Failed to encode UUID '{value}'."); |
| | | 21 | | } |
| | | 22 | | |
| | 6 | 23 | | encoder.WriteByteSpan(span); |
| | 12 | 24 | | } |
| | | 25 | | } |