Skip to content

Commit de13325

Browse files
Add support for .NET Standard 2.0
On .NET Standard 2.0, the library uses Microsoft.Bcl.AsyncInterfaces as a polyfill for IAsyncEnumerable. Also, nullable reference types and attributes are disabled on .NET Standard 2.0.
1 parent b367bb6 commit de13325

File tree

11 files changed

+72
-25
lines changed

11 files changed

+72
-25
lines changed

Matchmaker/Linq/MatchResultExtensions.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
namespace Matchmaker.Linq;
22

33
using System;
4+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
45
using System.Diagnostics.CodeAnalysis;
6+
#endif
57
using System.Threading.Tasks;
68

79
/// <summary>
@@ -19,7 +21,9 @@ public static class MatchResultExtensions
1921
/// <returns>
2022
/// The result's value if it's successful, or the default one otherwise.
2123
/// </returns>
24+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
2225
[return: MaybeNull]
26+
#endif
2327
public static T GetValueOrDefault<T>(this MatchResult<T> result) =>
2428
result!.GetValueOrDefault(default(T));
2529

@@ -32,8 +36,15 @@ public static T GetValueOrDefault<T>(this MatchResult<T> result) =>
3236
/// <returns>
3337
/// The result's value if it's successful, or the default one otherwise.
3438
/// </returns>
39+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
3540
[return: MaybeNull]
36-
public static T GetValueOrDefault<T>(this MatchResult<T> result, [AllowNull] T defaultValue) =>
41+
#endif
42+
public static T GetValueOrDefault<T>(
43+
this MatchResult<T> result,
44+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
45+
[AllowNull]
46+
#endif
47+
T defaultValue) =>
3748
result.IsSuccessful ? result.Value : defaultValue;
3849

3950
/// <summary>
@@ -50,7 +61,9 @@ public static T GetValueOrDefault<T>(this MatchResult<T> result, [AllowNull] T d
5061
/// <exception cref="ArgumentNullException">
5162
/// <paramref name="defaultValueProvider" /> is <see langword="null" />.
5263
/// </exception>
64+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
5365
[return: MaybeNull]
66+
#endif
5467
public static T GetValueOrDefault<T>(this MatchResult<T> result, Func<T> defaultValueProvider) =>
5568
defaultValueProvider != null
5669
? result.IsSuccessful ? result.Value : defaultValueProvider()
@@ -68,7 +81,9 @@ public static T GetValueOrDefault<T>(this MatchResult<T> result, Func<T> default
6881
/// <exception cref="ArgumentNullException">
6982
/// <paramref name="exceptionProvider" /> is <see langword="null" />.
7083
/// </exception>
84+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
7185
[return: MaybeNull]
86+
#endif
7287
public static T GetValueOrThrow<T>(this MatchResult<T> result, Func<Exception> exceptionProvider) =>
7388
exceptionProvider != null
7489
? result.IsSuccessful ? result.Value : throw exceptionProvider()
@@ -225,7 +240,12 @@ public static async Task<T> GetValueOrDefault<T>(this Task<MatchResult<T>> futur
225240
/// <exception cref="ArgumentNullException">
226241
/// <paramref name="futureResult" /> is <see langword="null" />.
227242
/// </exception>
228-
public static async Task<T> GetValueOrDefault<T>(this Task<MatchResult<T>> futureResult, [AllowNull] T defaultValue)
243+
public static async Task<T> GetValueOrDefault<T>(
244+
this Task<MatchResult<T>> futureResult,
245+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
246+
[AllowNull]
247+
#endif
248+
T defaultValue)
229249
{
230250
var result = await (futureResult ?? throw new ArgumentNullException(nameof(futureResult)));
231251
return result.IsSuccessful ? result.Value! : defaultValue!;

Matchmaker/MatchException.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace Matchmaker;
22

33
using System;
44
using System.Diagnostics.CodeAnalysis;
5-
using System.Runtime.Serialization;
65

76
/// <summary>
87
/// Represents an exception which is thrown when a match expression hasn't found a successful pattern.
@@ -36,13 +35,4 @@ public MatchException(string message)
3635
public MatchException(string message, Exception innerException)
3736
: base(message, innerException)
3837
{ }
39-
40-
/// <summary>
41-
/// Initializes a new instance of the <see cref="MatchException" /> class.
42-
/// </summary>
43-
/// <param name="info">The serialization info.</param>
44-
/// <param name="context">The streaming context.</param>
45-
protected MatchException(SerializationInfo info, StreamingContext context)
46-
: base(info, context)
47-
{ }
4838
}

Matchmaker/MatchResult.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace Matchmaker;
22

3+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
34
using System.Diagnostics.CodeAnalysis;
5+
#endif
46

57
/// <summary>
68
/// A static class which is used to create match results.
@@ -13,7 +15,11 @@ public static class MatchResult
1315
/// </summary>
1416
/// <param name="value">The value of the result.</param>
1517
/// <returns>A successful match result with the specified value.</returns>
16-
public static MatchResult<T> Success<T>([AllowNull] T value) =>
18+
public static MatchResult<T> Success<T>(
19+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
20+
[AllowNull]
21+
#endif
22+
T value) =>
1723
new(true, value);
1824

1925
/// <summary>

Matchmaker/MatchResult_1.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
namespace Matchmaker;
22

33
using System;
4+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
45
using System.Diagnostics.CodeAnalysis;
6+
#endif
57

68
using Matchmaker.Linq;
79

@@ -20,16 +22,23 @@ namespace Matchmaker;
2022
/// <summary>
2123
/// The value of the result if it's successful.
2224
/// </summary>
25+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
2326
[AllowNull]
2427
[MaybeNull]
28+
#endif
2529
private readonly T value;
2630

2731
/// <summary>
2832
/// Initializes a new instance of the <see cref="MatchResult{T}" /> class.
2933
/// </summary>
3034
/// <param name="isSuccessful">The value which indicates whether the match result is successful.</param>
3135
/// <param name="value">The value of the result, if it is successful.</param>
32-
internal MatchResult(bool isSuccessful, [AllowNull] T value)
36+
internal MatchResult(
37+
bool isSuccessful,
38+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
39+
[AllowNull]
40+
#endif
41+
T value)
3342
{
3443
this.IsSuccessful = isSuccessful;
3544
this.value = value;
@@ -47,7 +56,9 @@ internal MatchResult(bool isSuccessful, [AllowNull] T value)
4756
/// <exception cref="InvalidOperationException">
4857
/// The result is not successful.
4958
/// </exception>
59+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
5060
[MaybeNull]
61+
#endif
5162
public T Value =>
5263
this.IsSuccessful
5364
? this.value
@@ -85,7 +96,11 @@ public bool Equals(MatchResult<T> other) =>
8596
/// </summary>
8697
/// <returns>The hash code of this match result.</returns>
8798
public override int GetHashCode() =>
99+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
88100
HashCode.Combine(this.IsSuccessful, this.value);
101+
#else
102+
this.value?.GetHashCode() ?? 0;
103+
#endif
89104

90105
/// <summary>
91106
/// Returns the string representation of this match result.

Matchmaker/Match_2.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ namespace Matchmaker;
22

33
using System;
44
using System.Collections.Generic;
5+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
56
using System.Diagnostics.CodeAnalysis;
7+
#endif
8+
69

710
using Matchmaker.Linq;
811
using Matchmaker.Patterns;
@@ -170,7 +173,9 @@ public Match<TInput, TOutput> Case<TType>(bool fallthrough, Func<TType, TOutput>
170173
/// <exception cref="MatchException">
171174
/// The match failed for all cases.
172175
/// </exception>
176+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
173177
[return: MaybeNull]
178+
#endif
174179
public TOutput ExecuteOn(TInput input) =>
175180
this.ExecuteNonStrict(input).GetValueOrThrow(() => new MatchException($"Could not match {input}"));
176181

Matchmaker/Matchmaker.csproj

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.1</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0</TargetFrameworks>
55
<LangVersion>latest</LangVersion>
66
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
</PropertyGroup>
9+
10+
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
711
<IsTrimmable>true</IsTrimmable>
12+
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
13+
</PropertyGroup>
14+
15+
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
16+
<Nullable>disable</Nullable>
17+
<NoWarn>$(NoWarn);nullable</NoWarn>
18+
</PropertyGroup>
19+
20+
<PropertyGroup>
821
<PackageTags>csharp pattern-matching functional-programming</PackageTags>
922
<PackageId>Matchmaker</PackageId>
1023
<Product>Matchmaker</Product>
@@ -48,6 +61,7 @@
4861
</ItemGroup>
4962

5063
<ItemGroup>
64+
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.5" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
5165
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
5266
<PrivateAssets>all</PrivateAssets>
5367
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Matchmaker/Matchmaker.xml

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Matchmaker/Patterns/Async/AsyncPattern_2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ protected AsyncPattern(string description) =>
5252
/// The description of this pattern, if it has one. Otherwise, the name of this pattern's type.
5353
/// </returns>
5454
public override string ToString() =>
55-
String.IsNullOrEmpty(this.Description) ? base.ToString() : this.Description;
55+
String.IsNullOrEmpty(this.Description) ? base.ToString()! : this.Description;
5656
}

Matchmaker/Patterns/Async/CachingAsyncPattern.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ internal sealed class CachingAsyncPattern<TInput, TMatchResult> : AsyncPattern<T
1919
/// <summary>
2020
/// The dictionary which holds this pattern's cache.
2121
/// </summary>
22+
#nullable disable
2223
private readonly ConcurrentDictionary<TInput, Task<MatchResult<TMatchResult>>> cache = new();
24+
#nullable enable
2325

2426
/// <summary>
2527
/// The cached result for the <see langword="null" /> input.

Matchmaker/Patterns/CachingPattern.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ internal sealed class CachingPattern<TInput, TMatchResult> : Pattern<TInput, TMa
1818
/// <summary>
1919
/// The dictionary which holds this pattern's cache.
2020
/// </summary>
21-
private readonly Dictionary<TInput, MatchResult<TMatchResult>> cache = new();
21+
#nullable disable
22+
private readonly Dictionary<TInput, MatchResult<TMatchResult>> cache = [];
23+
#nullable enable
2224

2325
/// <summary>
2426
/// The cached result for the <see langword="null" /> input.

0 commit comments

Comments
 (0)