From 88a0889457dd77ce4e8f8470bb40c25798ca330e Mon Sep 17 00:00:00 2001 From: Matthew O'Connor Date: Wed, 16 Oct 2024 11:55:10 -0600 Subject: [PATCH] Support collection builders in .NET 8+. --- Xledger.Collections.Test/TestImmArray.cs | 9 +++++++++ Xledger.Collections.Test/TestImmSet.cs | 9 +++++++++ .../Xledger.Collections.Test.csproj | 7 +++++-- Xledger.Collections/Extensions.cs | 12 ++++++++++++ Xledger.Collections/ImmArray.cs | 9 +++++++++ Xledger.Collections/ImmSet.cs | 9 +++++++++ Xledger.Collections/Xledger.Collections.csproj | 2 +- 7 files changed, 54 insertions(+), 3 deletions(-) diff --git a/Xledger.Collections.Test/TestImmArray.cs b/Xledger.Collections.Test/TestImmArray.cs index 39332f1..cfcf592 100644 --- a/Xledger.Collections.Test/TestImmArray.cs +++ b/Xledger.Collections.Test/TestImmArray.cs @@ -32,6 +32,15 @@ public void TestSingle() { Assert.Equal(imm2, imm1); } +#if NET8_0_OR_GREATER + [Fact] + public void TestCollectionBuilder() { + var ys = ImmArray.Of("hi", "how", "are", "you?"); + ImmArray xs = ["hi", "how", "are", "you?"]; + Assert.Equal(ys, xs); + } +#endif + [Fact] public void TestCopy() { int[] arr = [1, 2, 3, 4, 5, 6]; diff --git a/Xledger.Collections.Test/TestImmSet.cs b/Xledger.Collections.Test/TestImmSet.cs index 6bd2f69..cd9462e 100644 --- a/Xledger.Collections.Test/TestImmSet.cs +++ b/Xledger.Collections.Test/TestImmSet.cs @@ -32,6 +32,15 @@ public void TestSingle() { Assert.Equal(imm2, imm1); } +#if NET8_0_OR_GREATER + [Fact] + public void TestCollectionBuilder() { + var ys = ImmSet.Of("hi", "how", "are", "you?"); + ImmSet xs = ["hi", "how", "are", "you?"]; + Assert.Equal(ys, xs); + } +#endif + [Fact] public void TestCopy() { int[] arr = [1, 2, 3, 4, 5, 6]; diff --git a/Xledger.Collections.Test/Xledger.Collections.Test.csproj b/Xledger.Collections.Test/Xledger.Collections.Test.csproj index 4b3f39d..6390632 100644 --- a/Xledger.Collections.Test/Xledger.Collections.Test.csproj +++ b/Xledger.Collections.Test/Xledger.Collections.Test.csproj @@ -3,12 +3,15 @@ Xledger.Collections.Test - net48;net6.0 + net48;net6.0;net8.0 12.0 disable disable false + + + true @@ -19,7 +22,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Xledger.Collections/Extensions.cs b/Xledger.Collections/Extensions.cs index 1184091..223eebd 100644 --- a/Xledger.Collections/Extensions.cs +++ b/Xledger.Collections/Extensions.cs @@ -12,6 +12,12 @@ public static ImmArray ToImmArray(this IEnumerable xs) { }; } +#if NET + public static ImmArray ToImmArray(this ReadOnlySpan xs) { + return new ImmArray(xs.ToArray()); + } +#endif + public static ImmSet ToImmSet(this IEnumerable xs) { return xs switch { null => ImmSet.Empty, @@ -20,6 +26,12 @@ public static ImmSet ToImmSet(this IEnumerable xs) { }; } +#if NET + public static ImmSet ToImmSet(this ReadOnlySpan xs) { + return new ImmSet(xs.ToArray()); + } +#endif + public static ImmDict ToImmDict(this IEnumerable> xs) { return xs switch { null => ImmDict.Empty, diff --git a/Xledger.Collections/ImmArray.cs b/Xledger.Collections/ImmArray.cs index c62848c..9431b6a 100644 --- a/Xledger.Collections/ImmArray.cs +++ b/Xledger.Collections/ImmArray.cs @@ -4,11 +4,20 @@ public static class ImmArray { public static ImmArray Of(params T[] arr) { return arr.ToImmArray(); } + +#if NET + public static ImmArray Of(ReadOnlySpan span) { + return span.ToImmArray(); + } +#endif } [Serializable] [DebuggerDisplay("Count = {Count}")] [DebuggerTypeProxy(typeof(ImmArray<>.DebugView))] +#if NET8_0_OR_GREATER +[System.Runtime.CompilerServices.CollectionBuilder(typeof(ImmArray), nameof(ImmArray.Of))] +#endif public sealed class ImmArray : IReadOnlyList, IEquatable>, IList, ICollection, IComparable, IComparable>, IStructuralComparable { diff --git a/Xledger.Collections/ImmSet.cs b/Xledger.Collections/ImmSet.cs index 3be96cf..4495c77 100644 --- a/Xledger.Collections/ImmSet.cs +++ b/Xledger.Collections/ImmSet.cs @@ -4,11 +4,20 @@ public static class ImmSet { public static ImmSet Of(params T[] arr) { return arr.ToImmSet(); } + +#if NET + public static ImmSet Of(ReadOnlySpan span) { + return span.ToImmSet(); + } +#endif } [Serializable] [DebuggerDisplay("Count = {Count}")] [DebuggerTypeProxy(typeof(ImmSet<>.DebugView))] +#if NET8_0_OR_GREATER +[System.Runtime.CompilerServices.CollectionBuilder(typeof(ImmSet), nameof(ImmArray.Of))] +#endif public sealed class ImmSet : IReadOnlyCollection, ISet, IEquatable>, ICollection #if NET6_0_OR_GREATER , IReadOnlySet diff --git a/Xledger.Collections/Xledger.Collections.csproj b/Xledger.Collections/Xledger.Collections.csproj index 1bac6d2..8b7865a 100644 --- a/Xledger.Collections/Xledger.Collections.csproj +++ b/Xledger.Collections/Xledger.Collections.csproj @@ -5,7 +5,7 @@ Xledger.Collections Xledger.Collections - net48;net6.0 + net48;net6.0;net8.0 12.0 disable disable