Skip to content

Commit

Permalink
Merge pull request #3 from xledger/support-collection-builders
Browse files Browse the repository at this point in the history
Support collection builders in .NET 8+.
  • Loading branch information
isaksky authored Oct 16, 2024
2 parents aa5f4c7 + 88a0889 commit c19c74a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Xledger.Collections.Test/TestImmArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> xs = ["hi", "how", "are", "you?"];
Assert.Equal(ys, xs);
}
#endif

[Fact]
public void TestCopy() {
int[] arr = [1, 2, 3, 4, 5, 6];
Expand Down
9 changes: 9 additions & 0 deletions Xledger.Collections.Test/TestImmSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> xs = ["hi", "how", "are", "you?"];
Assert.Equal(ys, xs);
}
#endif

[Fact]
public void TestCopy() {
int[] arr = [1, 2, 3, 4, 5, 6];
Expand Down
7 changes: 5 additions & 2 deletions Xledger.Collections.Test/Xledger.Collections.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
<PropertyGroup>
<PackageId>Xledger.Collections.Test</PackageId>

<TargetFrameworks>net48;net6.0</TargetFrameworks>
<TargetFrameworks>net48;net6.0;net8.0</TargetFrameworks>
<LangVersion>12.0</LangVersion>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>

<IsPackable>false</IsPackable>

<!-- To allow use in .NET 8 -->
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -19,7 +22,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
12 changes: 12 additions & 0 deletions Xledger.Collections/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public static ImmArray<T> ToImmArray<T>(this IEnumerable<T> xs) {
};
}

#if NET
public static ImmArray<T> ToImmArray<T>(this ReadOnlySpan<T> xs) {
return new ImmArray<T>(xs.ToArray());
}
#endif

public static ImmSet<T> ToImmSet<T>(this IEnumerable<T> xs) {
return xs switch {
null => ImmSet<T>.Empty,
Expand All @@ -20,6 +26,12 @@ public static ImmSet<T> ToImmSet<T>(this IEnumerable<T> xs) {
};
}

#if NET
public static ImmSet<T> ToImmSet<T>(this ReadOnlySpan<T> xs) {
return new ImmSet<T>(xs.ToArray());
}
#endif

public static ImmDict<K, V> ToImmDict<K, V>(this IEnumerable<KeyValuePair<K, V>> xs) {
return xs switch {
null => ImmDict<K, V>.Empty,
Expand Down
9 changes: 9 additions & 0 deletions Xledger.Collections/ImmArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ public static class ImmArray {
public static ImmArray<T> Of<T>(params T[] arr) {
return arr.ToImmArray();
}

#if NET
public static ImmArray<T> Of<T>(ReadOnlySpan<T> 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<T> : IReadOnlyList<T>, IEquatable<ImmArray<T>>, IList<T>,
ICollection, IComparable, IComparable<ImmArray<T>>, IStructuralComparable
{
Expand Down
9 changes: 9 additions & 0 deletions Xledger.Collections/ImmSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ public static class ImmSet {
public static ImmSet<T> Of<T>(params T[] arr) {
return arr.ToImmSet();
}

#if NET
public static ImmSet<T> Of<T>(ReadOnlySpan<T> 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<T> : IReadOnlyCollection<T>, ISet<T>, IEquatable<ImmSet<T>>, ICollection
#if NET6_0_OR_GREATER
, IReadOnlySet<T>
Expand Down
2 changes: 1 addition & 1 deletion Xledger.Collections/Xledger.Collections.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<RootNamespace>Xledger.Collections</RootNamespace>
<AssemblyName>Xledger.Collections</AssemblyName>

<TargetFrameworks>net48;net6.0</TargetFrameworks>
<TargetFrameworks>net48;net6.0;net8.0</TargetFrameworks>
<LangVersion>12.0</LangVersion>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
Expand Down

0 comments on commit c19c74a

Please sign in to comment.