From 752022ee894cf53ad0b537d51e57d3e111b88fa8 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Sat, 17 Apr 2021 11:36:54 -0600 Subject: [PATCH] Add support for `IReadOnlySet` Closes #1217 --- .../Formatters/CollectionFormatter.cs | 22 +++++++++++++++++++ .../Resolvers/DynamicGenericResolver.cs | 3 +++ .../net5.0/PublicAPI.Unshipped.txt | 2 ++ 3 files changed, 27 insertions(+) diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/CollectionFormatter.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/CollectionFormatter.cs index f6bf5d23f..2255cd707 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/CollectionFormatter.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/CollectionFormatter.cs @@ -1271,6 +1271,28 @@ protected override HashSet Create(int count, MessagePackSerializerOptions opt } } +#if NET5_0_OR_GREATER + + public sealed class InterfaceReadOnlySetFormatter : CollectionFormatterBase, IReadOnlySet> + { + protected override void Add(HashSet collection, int index, T value, MessagePackSerializerOptions options) + { + collection.Add(value); + } + + protected override IReadOnlySet Complete(HashSet intermediateCollection) + { + return intermediateCollection; + } + + protected override HashSet Create(int count, MessagePackSerializerOptions options) + { + return new HashSet(options.Security.GetEqualityComparer()); + } + } + +#endif + public sealed class ConcurrentBagFormatter : CollectionFormatterBase> { protected override int? GetCount(ConcurrentBag sequence) diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/DynamicGenericResolver.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/DynamicGenericResolver.cs index bb555243e..dc54e6268 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/DynamicGenericResolver.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/DynamicGenericResolver.cs @@ -70,6 +70,9 @@ internal static class DynamicGenericResolverGetFormatterHelper { typeof(IReadOnlyList<>), typeof(InterfaceReadOnlyListFormatter<>) }, { typeof(IReadOnlyCollection<>), typeof(InterfaceReadOnlyCollectionFormatter<>) }, { typeof(ISet<>), typeof(InterfaceSetFormatter<>) }, +#if NET5_0_OR_GREATER + { typeof(IReadOnlySet<>), typeof(InterfaceReadOnlySetFormatter<>) }, +#endif { typeof(System.Collections.Concurrent.ConcurrentBag<>), typeof(ConcurrentBagFormatter<>) }, { typeof(System.Collections.Concurrent.ConcurrentQueue<>), typeof(ConcurrentQueueFormatter<>) }, { typeof(System.Collections.Concurrent.ConcurrentStack<>), typeof(ConcurrentStackFormatter<>) }, diff --git a/src/MessagePack/net5.0/PublicAPI.Unshipped.txt b/src/MessagePack/net5.0/PublicAPI.Unshipped.txt index 1525d0ccb..519b17eec 100644 --- a/src/MessagePack/net5.0/PublicAPI.Unshipped.txt +++ b/src/MessagePack/net5.0/PublicAPI.Unshipped.txt @@ -4,6 +4,8 @@ MessagePack.FormatterNotRegisteredException.FormatterNotRegisteredException(Syst MessagePack.Formatters.HalfFormatter MessagePack.Formatters.HalfFormatter.Deserialize(ref MessagePack.MessagePackReader reader, MessagePack.MessagePackSerializerOptions options) -> System.Half MessagePack.Formatters.HalfFormatter.Serialize(ref MessagePack.MessagePackWriter writer, System.Half value, MessagePack.MessagePackSerializerOptions options) -> void +MessagePack.Formatters.InterfaceReadOnlySetFormatter +MessagePack.Formatters.InterfaceReadOnlySetFormatter.InterfaceReadOnlySetFormatter() -> void MessagePack.MessagePackReader.MessagePackReader() -> void MessagePack.MessagePackSerializerOptions.SequencePool.get -> MessagePack.SequencePool MessagePack.MessagePackSerializerOptions.WithPool(MessagePack.SequencePool pool) -> MessagePack.MessagePackSerializerOptions