From ee6002bbdb5253e89516658c4195399ec6d362d3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 Feb 2023 16:36:08 +0100 Subject: [PATCH] Add and use ArgumentOutOfRangeException.ThrowIfNegative --- .../ArgumentNullExceptionExtensions.cs | 2 +- .../ArgumentOutOfRangeExceptionExtensions.cs | 36 +++++++++++++++++++ .../Graphics/Extensions/BufferExtensions.cs | 3 +- .../Extensions/Texture1DExtensions.cs | 3 +- .../Extensions/Texture2DExtensions.cs | 4 +-- .../Extensions/Texture3DExtensions.cs | 6 ++-- 6 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentNullExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentNullExceptionExtensions.cs index 7f6e276d..ab256f00 100644 --- a/src/ComputeSharp.Core/Polyfills/ArgumentNullExceptionExtensions.cs +++ b/src/ComputeSharp.Core/Polyfills/ArgumentNullExceptionExtensions.cs @@ -55,7 +55,7 @@ internal static class ArgumentNullExceptionExtensions /// [DoesNotReturn] - public static void Throw(string? parameterName) + private static void Throw(string? parameterName) { throw new ArgumentNullException(parameterName); } diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs new file mode 100644 index 00000000..8a6e2986 --- /dev/null +++ b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs @@ -0,0 +1,36 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace System; + +/// +/// Throw helper extensions for . +/// +internal static class ArgumentOutOfRangeExceptionExtensions +{ + /// + /// Throws an if is negative. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The argument to validate as non-negative. + /// The name of the parameter with which corresponds. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNegative(this ArgumentOutOfRangeException? _, int value, [CallerArgumentExpression(nameof(value))] string? parameterName = null) + { + if (value < 0) + { + Throw(parameterName); + } + } + + /// + /// Throws an for a given parameter name. + /// + /// The name of the parameter to report in the exception. + /// Thrown with . + [DoesNotReturn] + private static void Throw(string? parameterName) + { + throw new ArgumentOutOfRangeException(parameterName); + } +} diff --git a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs index 1534cc01..4f40f97f 100644 --- a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.InteropServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; #if !NET6_0_OR_GREATER using GC = ComputeSharp.NetStandard.GC; @@ -39,7 +38,7 @@ public static class BufferExtensions where T : unmanaged { default(ArgumentNullException).ThrowIfNull(source); - Guard.IsGreaterThanOrEqualTo(count, 0); + default(ArgumentOutOfRangeException).ThrowIfNegative(count); T[] data = GC.AllocateUninitializedArray(count); diff --git a/src/ComputeSharp/Graphics/Extensions/Texture1DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture1DExtensions.cs index c91bd4f5..5b054655 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture1DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture1DExtensions.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.InteropServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; namespace ComputeSharp; @@ -40,7 +39,7 @@ public static class Texture1DExtensions where T : unmanaged { default(ArgumentNullException).ThrowIfNull(source); - Guard.IsGreaterThanOrEqualTo(width, 0); + default(ArgumentOutOfRangeException).ThrowIfNegative(width); T[] data = new T[width]; diff --git a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs index 56accba3..c6b57168 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs @@ -45,8 +45,8 @@ public static class Texture2DExtensions where T : unmanaged { default(ArgumentNullException).ThrowIfNull(source); - Guard.IsGreaterThanOrEqualTo(width, 0); - Guard.IsGreaterThanOrEqualTo(height, 0); + default(ArgumentOutOfRangeException).ThrowIfNegative(width); + default(ArgumentOutOfRangeException).ThrowIfNegative(height); T[,] data = new T[height, width]; diff --git a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs index db76bfee..6dcfbedf 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs @@ -49,9 +49,9 @@ public static class Texture3DExtensions where T : unmanaged { default(ArgumentNullException).ThrowIfNull(source); - Guard.IsGreaterThanOrEqualTo(width, 0); - Guard.IsGreaterThanOrEqualTo(height, 0); - Guard.IsGreaterThanOrEqualTo(depth, 0); + default(ArgumentOutOfRangeException).ThrowIfNegative(width); + default(ArgumentOutOfRangeException).ThrowIfNegative(height); + default(ArgumentOutOfRangeException).ThrowIfNegative(depth); T[,,] data = new T[depth, height, width];