Skip to content

Commit b1a2ccf

Browse files
Merge pull request #3346 from Sergio0694/feature/throw-helpers
ThrowHelper APIs
2 parents 30452cf + 96991c2 commit b1a2ccf

16 files changed

+1090
-363
lines changed

Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.g.cs

Lines changed: 106 additions & 106 deletions
Large diffs are not rendered by default.

Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ using Microsoft.Toolkit.Extensions;
1313
namespace Microsoft.Toolkit.Diagnostics
1414
{
1515
/// <summary>
16-
/// Helper methods to throw exceptions
16+
/// Helper methods to efficiently throw exceptions.
1717
/// </summary>
18-
internal static partial class ThrowHelper
18+
public static partial class ThrowHelper
1919
{
2020
<#
2121
GenerateTextForItems(EnumerableTypes, item =>
@@ -26,7 +26,7 @@ GenerateTextForItems(EnumerableTypes, item =>
2626
/// </summary>
2727
[MethodImpl(MethodImplOptions.NoInlining)]
2828
[DoesNotReturn]
29-
public static void ThrowArgumentExceptionForIsEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
29+
internal static void ThrowArgumentExceptionForIsEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
3030
{
3131
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be empty, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
3232
}
@@ -36,7 +36,7 @@ GenerateTextForItems(EnumerableTypes, item =>
3636
/// </summary>
3737
[MethodImpl(MethodImplOptions.NoInlining)]
3838
[DoesNotReturn]
39-
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
39+
internal static void ThrowArgumentExceptionForHasSizeEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
4040
{
4141
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
4242
}
@@ -46,7 +46,7 @@ GenerateTextForItems(EnumerableTypes, item =>
4646
/// </summary>
4747
[MethodImpl(MethodImplOptions.NoInlining)]
4848
[DoesNotReturn]
49-
public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
49+
internal static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
5050
{
5151
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size not equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
5252
}
@@ -56,7 +56,7 @@ GenerateTextForItems(EnumerableTypes, item =>
5656
/// </summary>
5757
[MethodImpl(MethodImplOptions.NoInlining)]
5858
[DoesNotReturn]
59-
public static void ThrowArgumentExceptionForHasSizeGreaterThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
59+
internal static void ThrowArgumentExceptionForHasSizeGreaterThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
6060
{
6161
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size over {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
6262
}
@@ -66,7 +66,7 @@ GenerateTextForItems(EnumerableTypes, item =>
6666
/// </summary>
6767
[MethodImpl(MethodImplOptions.NoInlining)]
6868
[DoesNotReturn]
69-
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
69+
internal static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
7070
{
7171
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size of at least {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
7272
}
@@ -76,7 +76,7 @@ GenerateTextForItems(EnumerableTypes, item =>
7676
/// </summary>
7777
[MethodImpl(MethodImplOptions.NoInlining)]
7878
[DoesNotReturn]
79-
public static void ThrowArgumentExceptionForHasSizeLessThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
79+
internal static void ThrowArgumentExceptionForHasSizeLessThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
8080
{
8181
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
8282
}
@@ -86,7 +86,7 @@ GenerateTextForItems(EnumerableTypes, item =>
8686
/// </summary>
8787
[MethodImpl(MethodImplOptions.NoInlining)]
8888
[DoesNotReturn]
89-
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
89+
internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
9090
{
9191
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
9292
}
@@ -96,7 +96,7 @@ GenerateTextForItems(EnumerableTypes, item =>
9696
/// </summary>
9797
[MethodImpl(MethodImplOptions.NoInlining)]
9898
[DoesNotReturn]
99-
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
99+
internal static void ThrowArgumentExceptionForHasSizeEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
100100
{
101101
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {destination.<#=item.Size#>.ToAssertString()} (the destination), had a size of {source.<#=item.Size#>.ToAssertString()}");
102102
}
@@ -106,7 +106,7 @@ GenerateTextForItems(EnumerableTypes, item =>
106106
/// </summary>
107107
[MethodImpl(MethodImplOptions.NoInlining)]
108108
[DoesNotReturn]
109-
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
109+
internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
110110
{
111111
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {destination.<#=item.Size#>.ToAssertString()} (the destination), had a size of {source.<#=item.Size#>.ToAssertString()}");
112112
}
@@ -116,19 +116,19 @@ GenerateTextForItems(EnumerableTypes, item =>
116116
/// </summary>
117117
[MethodImpl(MethodImplOptions.NoInlining)]
118118
[DoesNotReturn]
119-
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
119+
internal static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
120120
{
121-
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}");
121+
ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}");
122122
}
123123

124124
/// <summary>
125125
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
126126
/// </summary>
127127
[MethodImpl(MethodImplOptions.NoInlining)]
128128
[DoesNotReturn]
129-
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
129+
internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
130130
{
131-
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be an invalid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}");
131+
ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be an invalid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}");
132132
}
133133
<#
134134
});

Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.cs renamed to Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Collection.Generic.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
namespace Microsoft.Toolkit.Diagnostics
1313
{
1414
/// <summary>
15-
/// Helper methods to throw exceptions
15+
/// Helper methods to efficiently throw exceptions.
1616
/// </summary>
17-
internal static partial class ThrowHelper
17+
public static partial class ThrowHelper
1818
{
1919
/// <summary>
2020
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotEmpty{T}(Span{T},string)"/> fails.
@@ -23,7 +23,7 @@ internal static partial class ThrowHelper
2323
/// <remarks>This method is needed because <see cref="Span{T}"/> can't be used as a generic type parameter.</remarks>
2424
[MethodImpl(MethodImplOptions.NoInlining)]
2525
[DoesNotReturn]
26-
public static void ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(string name)
26+
internal static void ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(string name)
2727
{
2828
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must not be empty");
2929
}
@@ -35,7 +35,7 @@ public static void ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(string name)
3535
/// <remarks>This method is needed because <see cref="ReadOnlySpan{T}"/> can't be used as a generic type parameter.</remarks>
3636
[MethodImpl(MethodImplOptions.NoInlining)]
3737
[DoesNotReturn]
38-
public static void ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(string name)
38+
internal static void ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(string name)
3939
{
4040
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must not be empty");
4141
}
@@ -46,7 +46,7 @@ public static void ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(string
4646
/// <typeparam name="T">The item of items in the input collection.</typeparam>
4747
[MethodImpl(MethodImplOptions.NoInlining)]
4848
[DoesNotReturn]
49-
public static void ThrowArgumentExceptionForIsNotEmpty<T>(string name)
49+
internal static void ThrowArgumentExceptionForIsNotEmpty<T>(string name)
5050
{
5151
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be empty");
5252
}

0 commit comments

Comments
 (0)