|
| 1 | +#if NET8_0_OR_GREATER |
| 2 | +using MemoryPack.Formatters; |
| 3 | +using MemoryPack.Internal; |
| 4 | +using System.Collections.Frozen; |
| 5 | + |
| 6 | +// Frozen Collections formatters |
| 7 | + |
| 8 | +namespace MemoryPack |
| 9 | +{ |
| 10 | + public static partial class MemoryPackFormatterProvider |
| 11 | + { |
| 12 | + static readonly Dictionary<Type, Type> FrozenCollectionFormatters = new Dictionary<Type, Type>() |
| 13 | + { |
| 14 | + { typeof(FrozenDictionary<,>), typeof(FrozenDictionaryFormatter<,>) }, |
| 15 | + { typeof(FrozenSet<>), typeof(FrozenSetFormatter<>) }, |
| 16 | + }; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +namespace MemoryPack.Formatters |
| 21 | +{ |
| 22 | + [Preserve] |
| 23 | + public sealed class FrozenDictionaryFormatter<TKey, TValue> : MemoryPackFormatter<FrozenDictionary<TKey, TValue?>> |
| 24 | + where TKey : notnull |
| 25 | + { |
| 26 | + readonly IEqualityComparer<TKey>? equalityComparer; |
| 27 | + |
| 28 | + public FrozenDictionaryFormatter() : this(null) |
| 29 | + { |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + public FrozenDictionaryFormatter(IEqualityComparer<TKey>? equalityComparer) |
| 34 | + { |
| 35 | + this.equalityComparer = equalityComparer; |
| 36 | + } |
| 37 | + |
| 38 | + [Preserve] |
| 39 | + public override void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref FrozenDictionary<TKey, TValue?>? value) |
| 40 | + { |
| 41 | + if (value == null) |
| 42 | + { |
| 43 | + writer.WriteNullCollectionHeader(); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + var keyFormatter = writer.GetFormatter<TKey>(); |
| 48 | + var valueFormatter = writer.GetFormatter<TValue>(); |
| 49 | + |
| 50 | + var count = value.Count; |
| 51 | + writer.WriteCollectionHeader(count); |
| 52 | + var i = 0; |
| 53 | + foreach (var item in value) |
| 54 | + { |
| 55 | + i++; |
| 56 | + KeyValuePairFormatter.Serialize(keyFormatter, valueFormatter, ref writer, item!); |
| 57 | + } |
| 58 | + |
| 59 | + if (i != count) MemoryPackSerializationException.ThrowInvalidConcurrrentCollectionOperation(); |
| 60 | + } |
| 61 | + |
| 62 | + [Preserve] |
| 63 | + public override void Deserialize(ref MemoryPackReader reader, scoped ref FrozenDictionary<TKey, TValue?>? value) |
| 64 | + { |
| 65 | + if (!reader.TryReadCollectionHeader(out var length)) |
| 66 | + { |
| 67 | + value = null; |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + var dict = new Dictionary<TKey, TValue?>(length, equalityComparer); |
| 72 | + |
| 73 | + var keyFormatter = reader.GetFormatter<TKey>(); |
| 74 | + var valueFormatter = reader.GetFormatter<TValue>(); |
| 75 | + for (var i = 0; i < length; i++) |
| 76 | + { |
| 77 | + KeyValuePairFormatter.Deserialize(keyFormatter, valueFormatter, ref reader, out var k, out var v); |
| 78 | + dict.Add(k!, v); |
| 79 | + } |
| 80 | + value = dict.ToFrozenDictionary(equalityComparer); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public sealed class FrozenSetFormatter<T> : MemoryPackFormatter<FrozenSet<T?>> |
| 85 | + { |
| 86 | + readonly IEqualityComparer<T?>? equalityComparer; |
| 87 | + |
| 88 | + public FrozenSetFormatter() : this(null) |
| 89 | + { |
| 90 | + } |
| 91 | + |
| 92 | + public FrozenSetFormatter(IEqualityComparer<T?>? equalityComparer) |
| 93 | + { |
| 94 | + this.equalityComparer = equalityComparer; |
| 95 | + } |
| 96 | + |
| 97 | + [Preserve] |
| 98 | + public override void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref FrozenSet<T?>? value) |
| 99 | + { |
| 100 | + if (value == null) |
| 101 | + { |
| 102 | + writer.WriteNullCollectionHeader(); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + var formatter = writer.GetFormatter<T?>(); |
| 107 | + writer.WriteCollectionHeader(value.Count); |
| 108 | + foreach (var item in value) |
| 109 | + { |
| 110 | + var v = item; |
| 111 | + formatter.Serialize(ref writer, ref v); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + [Preserve] |
| 116 | + public override void Deserialize(ref MemoryPackReader reader, scoped ref FrozenSet<T?>? value) |
| 117 | + { |
| 118 | + if (!reader.TryReadCollectionHeader(out var length)) |
| 119 | + { |
| 120 | + value = null; |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + var set = new HashSet<T>(length, equalityComparer); |
| 125 | + |
| 126 | + var formatter = reader.GetFormatter<T?>(); |
| 127 | + for (int i = 0; i < length; i++) |
| 128 | + { |
| 129 | + T? v = default; |
| 130 | + formatter.Deserialize(ref reader, ref v); |
| 131 | + set.Add(v!); |
| 132 | + } |
| 133 | + |
| 134 | + value = set.ToFrozenSet(equalityComparer)!; |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | +#endif |
0 commit comments