Skip to content

Commit

Permalink
Removing the static virtual declarations since things are falling over
Browse files Browse the repository at this point in the history
  • Loading branch information
tannergooding committed May 25, 2022
1 parent 8ab8539 commit baf69de
Show file tree
Hide file tree
Showing 23 changed files with 1,519 additions and 57 deletions.
70 changes: 70 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,76 @@ bool IBinaryInteger<byte>.TryWriteLittleEndian(Span<byte> destination, out int b
/// <inheritdoc cref="INumberBase{TSelf}.Abs(TSelf)" />
static byte INumberBase<byte>.Abs(byte value) => value;

/// <summary>Creates an instance of the current type from a value, throwing an overflow exception for any values that fall outside the representable range of the current type.</summary>
/// <typeparam name="TOther">The type of <paramref name="value" />.</typeparam>
/// <param name="value">The value which is used to create the instance of <see cref="byte" />.</param>
/// <returns>An instance of <see cref="byte" /> created from <paramref name="value" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
/// <exception cref="OverflowException"><paramref name="value" /> is not representable by <see cref="byte" />.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte CreateChecked<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
byte result;

if (typeof(TOther) == typeof(byte))
{
result = (byte)(object)value;
}
else if (!NumberBase<byte>.TryConvertFromChecked(value, out result) && !TOther.TryConvertToChecked(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <summary>Creates an instance of the current type from a value, saturating any values that fall outside the representable range of the current type.</summary>
/// <typeparam name="TOther">The type of <paramref name="value" />.</typeparam>
/// <param name="value">The value which is used to create the instance of <see cref="byte" />.</param>
/// <returns>An instance of <see cref="byte" /> created from <paramref name="value" />, saturating if <paramref name="value" /> falls outside the representable range of <see cref="byte" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte CreateSaturating<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
byte result;

if (typeof(TOther) == typeof(byte))
{
result = (byte)(object)value;
}
else if (!NumberBase<byte>.TryConvertFromSaturating(value, out result) && !TOther.TryConvertToSaturating(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <summary>Creates an instance of the current type from a value, truncating any values that fall outside the representable range of the current type.</summary>
/// <typeparam name="TOther">The type of <paramref name="value" />.</typeparam>
/// <param name="value">The value which is used to create the instance of <see cref="byte" />.</param>
/// <returns>An instance of <see cref="byte" /> created from <paramref name="value" />, truncating if <paramref name="value" /> falls outside the representable range of <see cref="byte" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte CreateTruncating<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
byte result;

if (typeof(TOther) == typeof(byte))
{
result = (byte)(object)value;
}
else if (!NumberBase<byte>.TryConvertFromTruncating(value, out result) && !TOther.TryConvertToTruncating(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <inheritdoc cref="INumberBase{TSelf}.IsCanonical(TSelf)" />
static bool INumberBase<byte>.IsCanonical(byte value) => true;

Expand Down
70 changes: 70 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,76 @@ bool IBinaryInteger<char>.TryWriteLittleEndian(Span<byte> destination, out int b
/// <inheritdoc cref="INumberBase{TSelf}.Abs(TSelf)" />
static char INumberBase<char>.Abs(char value) => value;

/// <summary>Creates an instance of the current type from a value, throwing an overflow exception for any values that fall outside the representable range of the current type.</summary>
/// <typeparam name="TOther">The type of <paramref name="value" />.</typeparam>
/// <param name="value">The value which is used to create the instance of <see cref="char" />.</param>
/// <returns>An instance of <see cref="char" /> created from <paramref name="value" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
/// <exception cref="OverflowException"><paramref name="value" /> is not representable by <see cref="char" />.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static char CreateChecked<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
char result;

if (typeof(TOther) == typeof(char))
{
result = (char)(object)value;
}
else if (!NumberBase<char>.TryConvertFromChecked(value, out result) && !TOther.TryConvertToChecked(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <summary>Creates an instance of the current type from a value, saturating any values that fall outside the representable range of the current type.</summary>
/// <typeparam name="TOther">The type of <paramref name="value" />.</typeparam>
/// <param name="value">The value which is used to create the instance of <see cref="char" />.</param>
/// <returns>An instance of <see cref="char" /> created from <paramref name="value" />, saturating if <paramref name="value" /> falls outside the representable range of <see cref="char" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static char CreateSaturating<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
char result;

if (typeof(TOther) == typeof(char))
{
result = (char)(object)value;
}
else if (!NumberBase<char>.TryConvertFromSaturating(value, out result) && !TOther.TryConvertToSaturating(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <summary>Creates an instance of the current type from a value, truncating any values that fall outside the representable range of the current type.</summary>
/// <typeparam name="TOther">The type of <paramref name="value" />.</typeparam>
/// <param name="value">The value which is used to create the instance of <see cref="char" />.</param>
/// <returns>An instance of <see cref="char" /> created from <paramref name="value" />, truncating if <paramref name="value" /> falls outside the representable range of <see cref="char" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static char CreateTruncating<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
char result;

if (typeof(TOther) == typeof(char))
{
result = (char)(object)value;
}
else if (!NumberBase<char>.TryConvertFromTruncating(value, out result) && !TOther.TryConvertToTruncating(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <inheritdoc cref="INumberBase{TSelf}.IsCanonical(TSelf)" />
static bool INumberBase<char>.IsCanonical(char value) => true;

Expand Down
70 changes: 70 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Decimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,76 @@ public static decimal Abs(decimal value)
return new decimal(in value, value._flags & ~SignMask);
}

/// <summary>Creates an instance of the current type from a value, throwing an overflow exception for any values that fall outside the representable range of the current type.</summary>
/// <typeparam name="TOther">The type of <paramref name="value" />.</typeparam>
/// <param name="value">The value which is used to create the instance of <see cref="decimal" />.</param>
/// <returns>An instance of <see cref="decimal" /> created from <paramref name="value" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
/// <exception cref="OverflowException"><paramref name="value" /> is not representable by <see cref="decimal" />.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static decimal CreateChecked<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
decimal result;

if (typeof(TOther) == typeof(decimal))
{
result = (decimal)(object)value;
}
else if (!NumberBase<decimal>.TryConvertFromChecked(value, out result) && !TOther.TryConvertToChecked(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <summary>Creates an instance of the current type from a value, saturating any values that fall outside the representable range of the current type.</summary>
/// <typeparam name="TOther">The type of <paramref name="value" />.</typeparam>
/// <param name="value">The value which is used to create the instance of <see cref="decimal" />.</param>
/// <returns>An instance of <see cref="decimal" /> created from <paramref name="value" />, saturating if <paramref name="value" /> falls outside the representable range of <see cref="decimal" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static decimal CreateSaturating<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
decimal result;

if (typeof(TOther) == typeof(decimal))
{
result = (decimal)(object)value;
}
else if (!NumberBase<decimal>.TryConvertFromSaturating(value, out result) && !TOther.TryConvertToSaturating(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <summary>Creates an instance of the current type from a value, truncating any values that fall outside the representable range of the current type.</summary>
/// <typeparam name="TOther">The type of <paramref name="value" />.</typeparam>
/// <param name="value">The value which is used to create the instance of <see cref="decimal" />.</param>
/// <returns>An instance of <see cref="decimal" /> created from <paramref name="value" />, truncating if <paramref name="value" /> falls outside the representable range of <see cref="decimal" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static decimal CreateTruncating<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
decimal result;

if (typeof(TOther) == typeof(decimal))
{
result = (decimal)(object)value;
}
else if (!NumberBase<decimal>.TryConvertFromTruncating(value, out result) && !TOther.TryConvertToTruncating(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <inheritdoc cref="INumberBase{TSelf}.IsCanonical(TSelf)" />
public static bool IsCanonical(decimal value)
{
Expand Down
70 changes: 70 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,76 @@ public static double MinNumber(double x, double y)
/// <inheritdoc cref="INumberBase{TSelf}.Abs(TSelf)" />
public static double Abs(double value) => Math.Abs(value);

/// <summary>Creates an instance of the current type from a value, throwing an overflow exception for any values that fall outside the representable range of the current type.</summary>
/// <typeparam name="TOther">The type of <paramref name="value" />.</typeparam>
/// <param name="value">The value which is used to create the instance of <see cref="double" />.</param>
/// <returns>An instance of <see cref="double" /> created from <paramref name="value" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
/// <exception cref="OverflowException"><paramref name="value" /> is not representable by <see cref="double" />.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double CreateChecked<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
double result;

if (typeof(TOther) == typeof(double))
{
result = (double)(object)value;
}
else if (!NumberBase<double>.TryConvertFromChecked(value, out result) && !TOther.TryConvertToChecked(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <summary>Creates an instance of the current type from a value, saturating any values that fall outside the representable range of the current type.</summary>
/// <typeparam name="TOther">The type of <paramref name="value" />.</typeparam>
/// <param name="value">The value which is used to create the instance of <see cref="double" />.</param>
/// <returns>An instance of <see cref="double" /> created from <paramref name="value" />, saturating if <paramref name="value" /> falls outside the representable range of <see cref="double" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double CreateSaturating<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
double result;

if (typeof(TOther) == typeof(double))
{
result = (double)(object)value;
}
else if (!NumberBase<double>.TryConvertFromSaturating(value, out result) && !TOther.TryConvertToSaturating(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <summary>Creates an instance of the current type from a value, truncating any values that fall outside the representable range of the current type.</summary>
/// <typeparam name="TOther">The type of <paramref name="value" />.</typeparam>
/// <param name="value">The value which is used to create the instance of <see cref="double" />.</param>
/// <returns>An instance of <see cref="double" /> created from <paramref name="value" />, truncating if <paramref name="value" /> falls outside the representable range of <see cref="double" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double CreateTruncating<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
double result;

if (typeof(TOther) == typeof(double))
{
result = (double)(object)value;
}
else if (!NumberBase<double>.TryConvertFromTruncating(value, out result) && !TOther.TryConvertToTruncating(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <inheritdoc cref="INumberBase{TSelf}.IsCanonical(TSelf)" />
static bool INumberBase<double>.IsCanonical(double value) => true;

Expand Down
Loading

0 comments on commit baf69de

Please sign in to comment.