Skip to content

Commit

Permalink
Inline HashCode.Combine polyfill (#2280)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint committed Apr 2, 2023
1 parent da1fea7 commit ccf63b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
29 changes: 0 additions & 29 deletions src/Sentry/Internal/Polyfills.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
// Polyfills to bridge the missing APIs in older targets.

#if NETFRAMEWORK || NETSTANDARD2_0
namespace System
{
internal static class HashCode
{
public static int Combine<T1, T2>(T1 value1, T2 value2)
{
unchecked
{
var hashCode = value1 != null ? value1.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (value2 != null ? value2.GetHashCode() : 0);
return hashCode;
}
}

public static int Combine<T1, T2, T3>(T1 value1, T2 value2, T3 value3)
{
unchecked
{
var hashCode = value1 != null ? value1.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (value2 != null ? value2.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (value3 != null ? value3.GetHashCode() : 0);
return hashCode;
}
}
}
}
#endif

#if NETFRAMEWORK
namespace System.Net.Http.Headers
{
Expand Down
18 changes: 16 additions & 2 deletions src/Sentry/MeasurementUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,21 @@ internal static MeasurementUnit Parse(string? name)
public override bool Equals(object? obj) => obj is MeasurementUnit other && Equals(other);

/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(_unit, _name, _unit?.GetType());
public override int GetHashCode()
{
var unitType = _unit?.GetType();
#if NETSTANDARD2_0 || NETFRAMEWORK
unchecked
{
var hashCode = _unit != null ? _unit.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (_name != null ? _name.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (unitType != null ? unitType.GetHashCode() : 0);
return hashCode;
}
#else
return HashCode.Combine(_unit, _name, unitType);
#endif
}

/// <summary>
/// Returns true if the operands are equal.
Expand All @@ -93,4 +107,4 @@ internal static MeasurementUnit Parse(string? name)
/// Returns true if the operands are not equal.
/// </summary>
public static bool operator !=(MeasurementUnit left, MeasurementUnit right) => !left.Equals(right);
}
}

0 comments on commit ccf63b5

Please sign in to comment.