Add and use ArgumentOutOfRangeException.ThrowIfNegative

このコミットが含まれているのは:
Sergio Pedri committed 2023-02-25 16:36:08 +01:00
1 親 7be3d577c4
コミット ee6002bbdb
6ファイル変更
+44 -10

検索条件に一致するファイルはありません

+1 -1
ファイルの表示
@@ -55,7 +55,7 @@ internal static class ArgumentNullExceptionExtensions
/// <inheritdoc cref="Throw(ArgumentNullException?, string?)"/>
[DoesNotReturn]
public static void Throw(string? parameterName)
private static void Throw(string? parameterName)
{
throw new ArgumentNullException(parameterName);
}
+36
ファイルの表示
@@ -0,0 +1,36 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System;
/// <summary>
/// Throw helper extensions for <see cref="ArgumentOutOfRangeException"/>.
/// </summary>
internal static class ArgumentOutOfRangeExceptionExtensions
{
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative.
/// </summary>
/// <param name="_">Dummy value to invoke the extension upon (always pass <see langword="null"/>.</param>
/// <param name="value">The argument to validate as non-negative.</param>
/// <param name="parameterName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfNegative(this ArgumentOutOfRangeException? _, int value, [CallerArgumentExpression(nameof(value))] string? parameterName = null)
{
if (value < 0)
{
Throw(parameterName);
}
}
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> for a given parameter name.
/// </summary>
/// <param name="parameterName">The name of the parameter to report in the exception.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown with <paramref name="parameterName"/>.</exception>
[DoesNotReturn]
private static void Throw(string? parameterName)
{
throw new ArgumentOutOfRangeException(parameterName);
}
}
+1 -2
ファイルの表示
@@ -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<T>(count);
+1 -2
ファイルの表示
@@ -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];
+2 -2
ファイルの表示
@@ -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];
+3 -3
ファイルの表示
@@ -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];