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