Skip to content

Commit db9df5a

Browse files
committed
Add throw helpers
1 parent c5165c8 commit db9df5a

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

src/LibDeflate/Imports/libdeflate_options.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Runtime.CompilerServices;
34
namespace LibDeflate.Imports;
45

@@ -19,23 +20,29 @@ internal readonly struct libdeflate_options
1920
public libdeflate_options(malloc_func malloc, free_func free)
2021
{
2122
#if NET6_0_OR_GREATER
22-
ArgumentNullException.ThrowIfNull(malloc);
23-
ArgumentNullException.ThrowIfNull(free);
23+
ArgumentNullException.ThrowIfNull(malloc);
24+
ArgumentNullException.ThrowIfNull(free);
2425
#else
25-
//TODO: add throwhelpers
26-
if (malloc is null)
27-
{
28-
throw new ArgumentNullException(nameof(malloc));
29-
}
30-
31-
if (free is null)
32-
{
33-
throw new ArgumentNullException(nameof(free));
34-
}
26+
ThrowIfNull(malloc);
27+
ThrowIfNull(free);
3528
#endif
29+
3630
this.sizeof_options = Size;
3731
this.malloc = malloc;
3832
this.free = free;
33+
34+
#if !NET6_0_OR_GREATER
35+
static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
36+
{
37+
if(argument is null)
38+
{
39+
ThrowHelperArgumentNull(paramName!);
40+
}
41+
42+
[DoesNotReturn]
43+
static void ThrowHelperArgumentNull(string paramName) => throw new ArgumentNullException(paramName);
44+
}
45+
#endif
3946
}
4047

4148
/// <summary>

0 commit comments

Comments
 (0)