| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Buffers; |
| | 4 | | using System.Collections.Immutable; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Runtime.InteropServices; |
| | 7 | |
|
| | 8 | | namespace ZeroC.Slice; |
| | 9 | |
|
| | 10 | | /// <summary>Provides extension methods for <see cref="SliceEncoder" /> to encode sequences or dictionaries.</summary> |
| | 11 | | public static class SliceEncoderExtensions |
| | 12 | | { |
| | 13 | | /// <summary>Encodes a dictionary.</summary> |
| | 14 | | /// <typeparam name="TKey">The dictionary key type.</typeparam> |
| | 15 | | /// <typeparam name="TValue">The dictionary value type.</typeparam> |
| | 16 | | /// <param name="encoder">The Slice encoder.</param> |
| | 17 | | /// <param name="v">The dictionary to encode.</param> |
| | 18 | | /// <param name="keyEncodeAction">The encode action for the keys.</param> |
| | 19 | | /// <param name="valueEncodeAction">The encode action for the values.</param> |
| | 20 | | public static void EncodeDictionary<TKey, TValue>( |
| | 21 | | this ref SliceEncoder encoder, |
| | 22 | | IEnumerable<KeyValuePair<TKey, TValue>> v, |
| | 23 | | EncodeAction<TKey> keyEncodeAction, |
| | 24 | | EncodeAction<TValue> valueEncodeAction) |
| | 25 | | where TKey : notnull |
| 5525 | 26 | | { |
| 5525 | 27 | | encoder.EncodeSize(v.Count()); |
| 33595 | 28 | | foreach ((TKey key, TValue value) in v) |
| 8511 | 29 | | { |
| 8511 | 30 | | keyEncodeAction(ref encoder, key); |
| 8511 | 31 | | valueEncodeAction(ref encoder, value); |
| 8509 | 32 | | } |
| 5523 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary>Encodes a dictionary with an optional value type (T? in Slice).</summary> |
| | 36 | | /// <typeparam name="TKey">The dictionary key type.</typeparam> |
| | 37 | | /// <typeparam name="TValue">The dictionary value type.</typeparam> |
| | 38 | | /// <param name="encoder">The Slice encoder.</param> |
| | 39 | | /// <param name="v">The dictionary to encode.</param> |
| | 40 | | /// <param name="keyEncodeAction">The encode action for the keys.</param> |
| | 41 | | /// <param name="valueEncodeAction">The encode action for the non-null values.</param> |
| | 42 | | public static void EncodeDictionaryWithOptionalValueType<TKey, TValue>( |
| | 43 | | this ref SliceEncoder encoder, |
| | 44 | | IEnumerable<KeyValuePair<TKey, TValue>> v, |
| | 45 | | EncodeAction<TKey> keyEncodeAction, |
| | 46 | | EncodeAction<TValue> valueEncodeAction) |
| | 47 | | where TKey : notnull |
| 17 | 48 | | { |
| 17 | 49 | | int count = v.Count(); |
| 17 | 50 | | encoder.EncodeSize(count); |
| 17 | 51 | | if (count > 0) |
| 17 | 52 | | { |
| 6275 | 53 | | foreach ((TKey key, TValue value) in v) |
| 3112 | 54 | | { |
| | 55 | | // Each entry is encoded like a: |
| | 56 | | // compact struct Pair |
| | 57 | | // { |
| | 58 | | // key: Key, |
| | 59 | | // value: Value? |
| | 60 | | // } |
| 3112 | 61 | | encoder.EncodeBool(value is not null); // simplified bit sequence |
| | 62 | |
|
| 3112 | 63 | | keyEncodeAction(ref encoder, key); |
| 3112 | 64 | | if (value is not null) |
| 538 | 65 | | { |
| 538 | 66 | | valueEncodeAction(ref encoder, value); |
| 538 | 67 | | } |
| 3112 | 68 | | } |
| 17 | 69 | | } |
| 17 | 70 | | } |
| | 71 | |
|
| | 72 | | /// <summary>Encodes a result.</summary> |
| | 73 | | /// <typeparam name="TSuccess">The type of the success value.</typeparam> |
| | 74 | | /// <typeparam name="TFailure">The type of the failure value.</typeparam> |
| | 75 | | /// <param name="encoder">The Slice encoder.</param> |
| | 76 | | /// <param name="v">The result to encode.</param> |
| | 77 | | /// <param name="successEncodeAction">The encode action for the success type.</param> |
| | 78 | | /// <param name="failureEncodeAction">The encode action for the failure type.</param> |
| | 79 | | public static void EncodeResult<TSuccess, TFailure>( |
| | 80 | | this ref SliceEncoder encoder, |
| | 81 | | Result<TSuccess, TFailure> v, |
| | 82 | | EncodeAction<TSuccess> successEncodeAction, |
| | 83 | | EncodeAction<TFailure> failureEncodeAction) |
| 4 | 84 | | { |
| 4 | 85 | | switch (v) |
| | 86 | | { |
| | 87 | | case Result<TSuccess, TFailure>.Success success: |
| 2 | 88 | | encoder.EncodeVarInt32(0); |
| 2 | 89 | | successEncodeAction(ref encoder, success.Value); |
| 2 | 90 | | break; |
| | 91 | |
|
| | 92 | | case Result<TSuccess, TFailure>.Failure failure: |
| 2 | 93 | | encoder.EncodeVarInt32(1); |
| 2 | 94 | | failureEncodeAction(ref encoder, failure.Value); |
| 2 | 95 | | break; |
| | 96 | |
|
| | 97 | | default: |
| | 98 | | // Our result type somehow got extended with an additional enumerator. |
| 0 | 99 | | Debug.Fail("Unexpected result type"); |
| 0 | 100 | | break; |
| | 101 | | } |
| 4 | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary>Encodes a sequence of fixed-size numeric values, such as int or ulong.</summary> |
| | 105 | | /// <typeparam name="T">The sequence element type.</typeparam> |
| | 106 | | /// <param name="encoder">The Slice encoder.</param> |
| | 107 | | /// <param name="v">The sequence of numeric values.</param> |
| | 108 | | public static void EncodeSequence<T>(this ref SliceEncoder encoder, IEnumerable<T> v) |
| | 109 | | where T : struct |
| 6381 | 110 | | { |
| 6381 | 111 | | switch (v) |
| | 112 | | { |
| | 113 | | case T[] vArray: |
| 6368 | 114 | | encoder.EncodeSpan(new ReadOnlySpan<T>(vArray)); |
| 6368 | 115 | | break; |
| | 116 | |
|
| | 117 | | case ImmutableArray<T> vImmutableArray: |
| 4 | 118 | | encoder.EncodeSpan(vImmutableArray.AsSpan()); |
| 4 | 119 | | break; |
| | 120 | |
|
| | 121 | | case ArraySegment<T> vArraySegment: |
| 4 | 122 | | encoder.EncodeSpan((ReadOnlySpan<T>)vArraySegment.AsSpan()); |
| 4 | 123 | | break; |
| | 124 | |
|
| | 125 | | case List<T> list: |
| 1 | 126 | | encoder.EncodeSpan((ReadOnlySpan<T>)CollectionsMarshal.AsSpan(list)); |
| 1 | 127 | | break; |
| | 128 | |
|
| | 129 | | default: |
| 4 | 130 | | encoder.EncodeSequence( |
| 4 | 131 | | v, |
| 516 | 132 | | (ref SliceEncoder encoder, T element) => encoder.EncodeFixedSizeNumeric(element)); |
| 4 | 133 | | break; |
| | 134 | | } |
| 6381 | 135 | | } |
| | 136 | |
|
| | 137 | | /// <summary>Encodes a sequence.</summary> |
| | 138 | | /// <typeparam name="T">The type of the sequence elements. It is non-nullable except for nullable class and nullable |
| | 139 | | /// custom types with Slice1.</typeparam> |
| | 140 | | /// <param name="encoder">The Slice encoder.</param> |
| | 141 | | /// <param name="v">The sequence to encode.</param> |
| | 142 | | /// <param name="encodeAction">The encode action for an element.</param> |
| | 143 | | public static void EncodeSequence<T>(this ref SliceEncoder encoder, IEnumerable<T> v, EncodeAction<T> encodeAction) |
| 57 | 144 | | { |
| 57 | 145 | | encoder.EncodeSize(v.Count()); // potentially slow Linq Count() |
| 11665 | 146 | | foreach (T item in v) |
| 5747 | 147 | | { |
| 5747 | 148 | | encodeAction(ref encoder, item); |
| 5747 | 149 | | } |
| 57 | 150 | | } |
| | 151 | |
|
| | 152 | | /// <summary>Encodes a sequence where the element type is an optional Slice type (T?).</summary> |
| | 153 | | /// <typeparam name="T">The nullable type of the sequence elements.</typeparam> |
| | 154 | | /// <param name="encoder">The Slice encoder.</param> |
| | 155 | | /// <param name="v">The sequence to encode.</param> |
| | 156 | | /// <param name="encodeAction">The encode action for a non-null value.</param> |
| | 157 | | /// <remarks>This method always encodes a bit sequence.</remarks> |
| | 158 | | public static void EncodeSequenceOfOptionals<T>( |
| | 159 | | this ref SliceEncoder encoder, |
| | 160 | | IEnumerable<T> v, |
| | 161 | | EncodeAction<T> encodeAction) |
| 17 | 162 | | { |
| 17 | 163 | | int count = v.Count(); // potentially slow Linq Count() |
| 17 | 164 | | encoder.EncodeSize(count); |
| 17 | 165 | | if (count > 0) |
| 17 | 166 | | { |
| 17 | 167 | | BitSequenceWriter bitSequenceWriter = encoder.GetBitSequenceWriter(count); |
| 2583 | 168 | | foreach (T item in v) |
| 1266 | 169 | | { |
| 1266 | 170 | | bitSequenceWriter.Write(item is not null); |
| 1266 | 171 | | if (item is not null) |
| 540 | 172 | | { |
| 540 | 173 | | encodeAction(ref encoder, item); |
| 540 | 174 | | } |
| 1266 | 175 | | } |
| 17 | 176 | | } |
| 17 | 177 | | } |
| | 178 | |
|
| | 179 | | /// <summary>Encodes a span of fixed-size numeric values, such as int or ulong.</summary> |
| | 180 | | /// <typeparam name="T">The span element type.</typeparam> |
| | 181 | | /// <param name="encoder">The Slice encoder.</param> |
| | 182 | | /// <param name="v">The span of numeric values represented by a <see cref="ReadOnlySpan{T}" />.</param> |
| | 183 | | public static void EncodeSpan<T>(this ref SliceEncoder encoder, ReadOnlySpan<T> v) |
| | 184 | | where T : struct |
| 6396 | 185 | | { |
| | 186 | | // This method works because (as long as) there is no padding in the memory representation of the ReadOnlySpan. |
| 6396 | 187 | | encoder.EncodeSize(v.Length); |
| 6396 | 188 | | if (!v.IsEmpty) |
| 6390 | 189 | | { |
| 6390 | 190 | | encoder.WriteByteSpan(MemoryMarshal.AsBytes(v)); |
| 6390 | 191 | | } |
| 6396 | 192 | | } |
| | 193 | |
|
| | 194 | | /// <summary>Copies a sequence of bytes to the underlying buffer writer.</summary> |
| | 195 | | /// <param name="encoder">The Slice encoder.</param> |
| | 196 | | /// <param name="v">The sequence to copy.</param> |
| | 197 | | public static void WriteByteSequence(this ref SliceEncoder encoder, ReadOnlySequence<byte> v) |
| 11 | 198 | | { |
| 11 | 199 | | if (!v.IsEmpty) |
| 6 | 200 | | { |
| 6 | 201 | | if (v.IsSingleSegment) |
| 6 | 202 | | { |
| 6 | 203 | | encoder.WriteByteSpan(v.FirstSpan); |
| 6 | 204 | | } |
| | 205 | | else |
| 0 | 206 | | { |
| 0 | 207 | | foreach (ReadOnlyMemory<byte> buffer in v) |
| 0 | 208 | | { |
| 0 | 209 | | encoder.WriteByteSpan(buffer.Span); |
| 0 | 210 | | } |
| 0 | 211 | | } |
| 6 | 212 | | } |
| 11 | 213 | | } |
| | 214 | | } |