Skip to content

Commit

Permalink
Update Vezel.Ruptura.Diagnostics code.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrp committed Jan 8, 2024
1 parent b134689 commit 4519ce0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 50 deletions.
22 changes: 22 additions & 0 deletions src/common/Diagnostics/Assert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Vezel.Ruptura.Diagnostics;

[StackTraceHidden]
internal static class Assert
{
public static void Always(
[DoesNotReturnIf(false)] bool condition,
[CallerArgumentExpression(nameof(condition))] string expression = "")
{
if (!condition)
throw new AssertionException(expression);
}

[Conditional("DEBUG")]
public static void Debug(
[DoesNotReturnIf(false)] bool condition,
[CallerArgumentExpression(nameof(condition))] string expression = "")
{
if (!condition)
throw new AssertionException(expression);
}
}
11 changes: 11 additions & 0 deletions src/common/Diagnostics/AssertionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Vezel.Ruptura.Diagnostics;

[SuppressMessage("", "CA1032")]
[SuppressMessage("", "CA1064")]
internal sealed class AssertionException : Exception
{
public AssertionException(string expression)
: base($"Assertion '{expression}' failed.")
{
}
}
54 changes: 6 additions & 48 deletions src/common/Diagnostics/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,57 +77,12 @@ public string ToStringAndClear()
}
}

public static class Always
{
public static void Assert(
[DoesNotReturnIf(false)] bool condition,
[CallerArgumentExpression(nameof(condition))] string? expression = null)
{
if (!condition)
throw new UnreachableException($"Hard assertion '{expression}' failed.");
}
}

public static class Debug
{
[Conditional("DEBUG")]
public static void Assert(
[DoesNotReturnIf(false)] bool condition,
[CallerArgumentExpression(nameof(condition))] string? expression = null)
{
if (!condition)
throw new UnreachableException($"Debug assertion '{expression}' failed.");
}
}

public static class Release
{
[Conditional("RELEASE")]
public static void Assert(
[DoesNotReturnIf(false)] bool condition,
[CallerArgumentExpression(nameof(condition))] string? expression = null)
{
if (!condition)
throw new UnreachableException($"Release assertion '{expression}' failed.");
}
}

public static void Argument([DoesNotReturnIf(false)] bool condition)
{
if (!condition)
throw new ArgumentException(message: null);
}

public static void Argument<T>(
[DoesNotReturnIf(false)] bool condition,
in T value,
[CallerArgumentExpression(nameof(value))] string? name = null)
{
_ = value;

Argument(condition, name!);
}

// TODO: https://github.com/dotnet/csharplang/issues/1148
public static void Argument<T>(
[DoesNotReturnIf(false)] bool condition,
Expand All @@ -136,17 +91,20 @@ public static void Argument<T>(
{
_ = value;

Argument(condition, name!);
if (!condition)
throw new ArgumentException(message: null, name);
}

public static void Null([NotNull] object? value, [CallerArgumentExpression(nameof(value))] string? name = null)
{
ArgumentNullException.ThrowIfNull(value, name);
if (value == null)
throw new ArgumentNullException(name);
}

public static unsafe void Null(void* value, [CallerArgumentExpression(nameof(value))] string? name = null)
{
ArgumentNullException.ThrowIfNull(value, name);
if (value == null)
throw new ArgumentNullException(name);
}

public static void Range<T>(
Expand Down
4 changes: 2 additions & 2 deletions src/system/SynchronizationObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private protected SynchronizationObject(nint handle)
private static WAIT_EVENT WaitMultiple(
scoped ReadOnlySpan<SynchronizationObject> objects, bool all, TimeSpan timeout, bool alertable)
{
Check.Argument(objects.Length is > 0 and <= (int)MAXIMUM_WAIT_OBJECTS, nameof(objects));
Check.Argument(objects.Length is > 0 and <= (int)MAXIMUM_WAIT_OBJECTS, objects);

var count = objects.Length;
var safeHandles = new SafeKernelHandle[count];
Expand All @@ -26,7 +26,7 @@ private static WAIT_EVENT WaitMultiple(
{
var handle = objects[i]?.SafeHandle;

Check.Argument(handle != null, nameof(objects));
Check.Argument(handle != null, objects);

var unused = false;

Expand Down

0 comments on commit 4519ce0

Please sign in to comment.