Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use allows ref struct in comparer interfaces #103604

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/libraries/System.Memory/ref/System.Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ public static partial class MemoryExtensions
public static System.Span<T> AsSpan<T>(this T[]? array, System.Range range) { throw null; }
public static int BinarySearch<T>(this System.ReadOnlySpan<T> span, System.IComparable<T> comparable) { throw null; }
public static int BinarySearch<T>(this System.Span<T> span, System.IComparable<T> comparable) { throw null; }
public static int BinarySearch<T, TComparer>(this System.ReadOnlySpan<T> span, T value, TComparer comparer) where TComparer : System.Collections.Generic.IComparer<T> { throw null; }
public static int BinarySearch<T, TComparable>(this System.ReadOnlySpan<T> span, TComparable comparable) where TComparable : System.IComparable<T> { throw null; }
public static int BinarySearch<T, TComparer>(this System.Span<T> span, T value, TComparer comparer) where TComparer : System.Collections.Generic.IComparer<T> { throw null; }
public static int BinarySearch<T, TComparable>(this System.Span<T> span, TComparable comparable) where TComparable : System.IComparable<T> { throw null; }
public static int BinarySearch<T, TComparer>(this System.ReadOnlySpan<T> span, T value, TComparer comparer) where TComparer : System.Collections.Generic.IComparer<T>, allows ref struct { throw null; }
public static int BinarySearch<T, TComparable>(this System.ReadOnlySpan<T> span, TComparable comparable) where TComparable : System.IComparable<T>, allows ref struct { throw null; }
public static int BinarySearch<T, TComparer>(this System.Span<T> span, T value, TComparer comparer) where TComparer : System.Collections.Generic.IComparer<T>, allows ref struct { throw null; }
public static int BinarySearch<T, TComparable>(this System.Span<T> span, TComparable comparable) where TComparable : System.IComparable<T>, allows ref struct { throw null; }
public static int CommonPrefixLength<T>(this System.Span<T> span, System.ReadOnlySpan<T> other) { throw null; }
public static int CommonPrefixLength<T>(this System.Span<T> span, System.ReadOnlySpan<T> other, System.Collections.Generic.IEqualityComparer<T>? comparer) { throw null; }
public static int CommonPrefixLength<T>(this System.ReadOnlySpan<T> span, System.ReadOnlySpan<T> other) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace System.Collections.Generic
/// </summary>
/// <typeparam name="TAlternate">The alternate type to compare.</typeparam>
/// <typeparam name="T">The type to compare.</typeparam>
public interface IAlternateEqualityComparer<in TAlternate, T> where TAlternate : allows ref struct
public interface IAlternateEqualityComparer<in TAlternate, T>
where TAlternate : allows ref struct
where T : allows ref struct
{
/// <summary>Determines whether the specified <paramref name="alternate"/> equals the specified <paramref name="other"/>.</summary>
/// <param name="alternate">The instance of type <typeparamref name="TAlternate"/> to compare.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace System.Collections.Generic
// The generic IComparer interface implements a method that compares
// two objects. It is used in conjunction with the Sort and
// BinarySearch methods on the Array, List, and SortedList classes.
public interface IComparer<in T>
public interface IComparer<in T> where T : allows ref struct
{
// Compares two objects. An implementation of this method must return a
// value less than zero if x is less than y, zero if x is equal to y, or a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace System.Collections.Generic
// The generic IEqualityComparer interface implements methods to check if two objects are equal
// and generate Hashcode for an object.
// It is used in Dictionary class.
public interface IEqualityComparer<in T>
public interface IEqualityComparer<in T> where T : allows ref struct
{
bool Equals(T? x, T? y);
int GetHashCode([DisallowNull] T obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public interface IComparable

// Generic version of IComparable.

public interface IComparable<in T>
public interface IComparable<in T> where T : allows ref struct
{
// Interface does not need to be marked with the serializable attribute

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace System
{
public interface IEquatable<T> // invariant due to questionable semantics around equality and inheritance
public interface IEquatable<T> where T : allows ref struct // invariant due to questionable semantics around equality and inheritance
{
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
/// <param name="other">An object to compare with this object.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3138,7 +3138,7 @@ public static int BinarySearch<T>(
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int BinarySearch<T, TComparable>(
this Span<T> span, TComparable comparable)
where TComparable : IComparable<T>
where TComparable : IComparable<T>, allows ref struct
{
return BinarySearch((ReadOnlySpan<T>)span, comparable);
}
Expand All @@ -3164,7 +3164,7 @@ public static int BinarySearch<T, TComparable>(
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int BinarySearch<T, TComparer>(
this Span<T> span, T value, TComparer comparer)
where TComparer : IComparer<T>
where TComparer : IComparer<T>, allows ref struct
{
return BinarySearch((ReadOnlySpan<T>)span, value, comparer);
}
Expand Down Expand Up @@ -3212,7 +3212,7 @@ public static int BinarySearch<T>(
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int BinarySearch<T, TComparable>(
this ReadOnlySpan<T> span, TComparable comparable)
where TComparable : IComparable<T>
where TComparable : IComparable<T>, allows ref struct
{
return SpanHelpers.BinarySearch(span, comparable);
}
Expand All @@ -3238,7 +3238,7 @@ public static int BinarySearch<T, TComparable>(
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int BinarySearch<T, TComparer>(
this ReadOnlySpan<T> span, T value, TComparer comparer)
where TComparer : IComparer<T>
where TComparer : IComparer<T>, allows ref struct
{
if (comparer == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static partial class SpanHelpers
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int BinarySearch<T, TComparable>(
this ReadOnlySpan<T> span, TComparable comparable)
where TComparable : IComparable<T>
where TComparable : IComparable<T>, allows ref struct
{
if (comparable == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparable);
Expand All @@ -22,7 +22,7 @@ public static int BinarySearch<T, TComparable>(

public static int BinarySearch<T, TComparable>(
ref T spanStart, int length, TComparable comparable)
where TComparable : IComparable<T>
where TComparable : IComparable<T>, allows ref struct
{
int lo = 0;
int hi = length - 1;
Expand Down Expand Up @@ -59,8 +59,8 @@ public static int BinarySearch<T, TComparable>(
}

// Helper to allow sharing all code via IComparable<T> inlineable
internal readonly struct ComparerComparable<T, TComparer> : IComparable<T>
where TComparer : IComparer<T>
internal readonly ref struct ComparerComparable<T, TComparer> : IComparable<T>
where TComparer : IComparer<T>, allows ref struct
{
private readonly T _value;
private readonly TComparer _comparer;
Expand Down
10 changes: 5 additions & 5 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3451,7 +3451,7 @@ public partial interface IComparable
{
int CompareTo(object? obj);
}
public partial interface IComparable<in T>
public partial interface IComparable<in T> where T : allows ref struct
{
int CompareTo(T? other);
}
Expand Down Expand Up @@ -3484,7 +3484,7 @@ public partial interface IDisposable
{
void Dispose();
}
public partial interface IEquatable<T>
public partial interface IEquatable<T> where T : allows ref struct
{
bool Equals(T? other);
}
Expand Down Expand Up @@ -8170,7 +8170,7 @@ public partial interface IStructuralEquatable
}
namespace System.Collections.Generic
{
public interface IAlternateEqualityComparer<in TAlternate, T> where TAlternate : allows ref struct
public interface IAlternateEqualityComparer<in TAlternate, T> where TAlternate : allows ref struct where T : allows ref struct
{
bool Equals(TAlternate alternate, T other);
int GetHashCode(TAlternate alternate);
Expand All @@ -8195,7 +8195,7 @@ public partial interface ICollection<T> : System.Collections.Generic.IEnumerable
void CopyTo(T[] array, int arrayIndex);
bool Remove(T item);
}
public partial interface IComparer<in T>
public partial interface IComparer<in T> where T : allows ref struct
{
int Compare(T? x, T? y);
}
Expand All @@ -8217,7 +8217,7 @@ public partial interface IEnumerator<out T> : System.Collections.IEnumerator, Sy
{
new T Current { get; }
}
public partial interface IEqualityComparer<in T>
public partial interface IEqualityComparer<in T> where T : allows ref struct
{
bool Equals(T? x, T? y);
int GetHashCode([System.Diagnostics.CodeAnalysis.DisallowNullAttribute] T obj);
Expand Down
Loading