This commit is contained in:
bbradson
2022-10-24 18:56:18 +02:00
parent 01ef055ff2
commit 9cdc8ccb4f
70 changed files with 19754 additions and 4 deletions
+80
View File
@@ -0,0 +1,80 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Runtime.CompilerServices;
namespace FisheryLib.Utility.Diagnostics;
/// <inheritdoc/>
public partial class Guard
{
/// <summary>
/// Asserts that the input <see cref="Stream"/> instance must support reading.
/// </summary>
/// <param name="stream">The input <see cref="Stream"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="stream"/> doesn't support reading.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CanRead(Stream stream, [CallerArgumentExpression("stream")] string name = "")
{
if (stream.CanRead)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForCanRead(stream, name);
}
/// <summary>
/// Asserts that the input <see cref="Stream"/> instance must support writing.
/// </summary>
/// <param name="stream">The input <see cref="Stream"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="stream"/> doesn't support writing.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CanWrite(Stream stream, [CallerArgumentExpression("stream")] string name = "")
{
if (stream.CanWrite)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForCanWrite(stream, name);
}
/// <summary>
/// Asserts that the input <see cref="Stream"/> instance must support seeking.
/// </summary>
/// <param name="stream">The input <see cref="Stream"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="stream"/> doesn't support seeking.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CanSeek(Stream stream, [CallerArgumentExpression("stream")] string name = "")
{
if (stream.CanSeek)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForCanSeek(stream, name);
}
/// <summary>
/// Asserts that the input <see cref="Stream"/> instance must be at the starting position.
/// </summary>
/// <param name="stream">The input <see cref="Stream"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="stream"/> is not at the starting position.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsAtStartPosition(Stream stream, [CallerArgumentExpression("stream")] string name = "")
{
if (stream.Position == 0)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsAtStartPosition(stream, name);
}
}