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

Add useful [MaybeNull] #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/Nito.Comparers.Core/ComparableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class ComparableBase<T> : IEquatable<T>, IComparable, IComparabl
/// </summary>
/// <param name="other">The object to compare with this instance. May be <c>null</c>.</param>
/// <returns>A value indicating whether this instance is equal to the specified object.</returns>
public bool Equals(T other) => ComparableImplementations.ImplementEquals(DefaultComparer, (T)this, other!);
public bool Equals([MaybeNull] T other) => ComparableImplementations.ImplementEquals(DefaultComparer, (T)this, other!);

/// <summary>
/// Returns a value indicating the relative order of this instance and the specified object: a negative value if this instance is less than the specified object; zero if this instance is equal to the specified object; and a positive value if this instance is greater than the specified object.
Expand All @@ -50,6 +50,6 @@ public abstract class ComparableBase<T> : IEquatable<T>, IComparable, IComparabl
/// </summary>
/// <param name="other">The object to compare with this instance. May be <c>null</c>.</param>
/// <returns>A value indicating the relative order of this instance and the specified object: a negative value if this instance is less than the specified object; zero if this instance is equal to the specified object; and a positive value if this instance is greater than the specified object.</returns>
public int CompareTo(T other) => ComparableImplementations.ImplementCompareTo(DefaultComparer, (T)this, other!);
public int CompareTo([MaybeNull] T other) => ComparableImplementations.ImplementCompareTo(DefaultComparer, (T)this, other!);
}
}
2 changes: 1 addition & 1 deletion src/Nito.Comparers.Core/EquatableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public abstract class EquatableBase<T> : IEquatable<T> where T : EquatableBase<T
/// </summary>
/// <param name="other">The object to compare with this instance. May be <c>null</c>.</param>
/// <returns>A value indicating whether this instance is equal to the specified object.</returns>
public bool Equals(T other) => ComparableImplementations.ImplementEquals(DefaultComparer, (T)this, other!);
public bool Equals([MaybeNull] T other) => ComparableImplementations.ImplementEquals(DefaultComparer, (T)this, other!);
}
}
6 changes: 6 additions & 0 deletions src/Nito.Comparers.Core/Nito.Comparers.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
<PackageTags>comparer;equalitycomparer;icomparable;iequatable</PackageTags>
<RootNamespace>Nito.Comparers</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Nullable" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
13 changes: 7 additions & 6 deletions src/Nito.Comparers.Core/Util/ComparableImplementations.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Nito.Comparers.Util
{
Expand Down Expand Up @@ -77,7 +78,7 @@ public static bool ImplementEquals(System.Collections.IEqualityComparer equality
/// <param name="equalityComparer">The comparer. May not be <c>null</c>.</param>
/// <param name="left">A value of type <typeparamref name="T"/> or <c>null</c>.</param>
/// <param name="right">A value of type <typeparamref name="T"/> or <c>null</c>.</param>
public static bool ImplementOpEquality<T>(IEqualityComparer<T> equalityComparer, T left, T right)
public static bool ImplementOpEquality<T>(IEqualityComparer<T> equalityComparer, [MaybeNull] T left, [MaybeNull] T right)
{
_ = equalityComparer ?? throw new ArgumentNullException(nameof(equalityComparer));
return equalityComparer.Equals(left, right);
Expand All @@ -90,7 +91,7 @@ public static bool ImplementOpEquality<T>(IEqualityComparer<T> equalityComparer,
/// <param name="equalityComparer">The comparer. May not be <c>null</c>.</param>
/// <param name="left">A value of type <typeparamref name="T"/> or <c>null</c>.</param>
/// <param name="right">A value of type <typeparamref name="T"/> or <c>null</c>.</param>
public static bool ImplementOpInequality<T>(IEqualityComparer<T> equalityComparer, T left, T right)
public static bool ImplementOpInequality<T>(IEqualityComparer<T> equalityComparer, [MaybeNull] T left, [MaybeNull] T right)
{
_ = equalityComparer ?? throw new ArgumentNullException(nameof(equalityComparer));
return !equalityComparer.Equals(left, right);
Expand All @@ -103,7 +104,7 @@ public static bool ImplementOpInequality<T>(IEqualityComparer<T> equalityCompare
/// <param name="comparer">The comparer. May not be <c>null</c>.</param>
/// <param name="left">A value of type <typeparamref name="T"/> or <c>null</c>.</param>
/// <param name="right">A value of type <typeparamref name="T"/> or <c>null</c>.</param>
public static bool ImplementOpLessThan<T>(IComparer<T> comparer, T left, T right)
public static bool ImplementOpLessThan<T>(IComparer<T> comparer, [MaybeNull] T left, [MaybeNull] T right)
{
_ = comparer ?? throw new ArgumentNullException(nameof(comparer));
return comparer.Compare(left, right) < 0;
Expand All @@ -116,7 +117,7 @@ public static bool ImplementOpLessThan<T>(IComparer<T> comparer, T left, T right
/// <param name="comparer">The comparer. May not be <c>null</c>.</param>
/// <param name="left">A value of type <typeparamref name="T"/> or <c>null</c>.</param>
/// <param name="right">A value of type <typeparamref name="T"/> or <c>null</c>.</param>
public static bool ImplementOpGreaterThan<T>(IComparer<T> comparer, T left, T right)
public static bool ImplementOpGreaterThan<T>(IComparer<T> comparer, [MaybeNull] T left, [MaybeNull] T right)
{
_ = comparer ?? throw new ArgumentNullException(nameof(comparer));
return comparer.Compare(left, right) > 0;
Expand All @@ -129,7 +130,7 @@ public static bool ImplementOpGreaterThan<T>(IComparer<T> comparer, T left, T ri
/// <param name="comparer">The comparer. May not be <c>null</c>.</param>
/// <param name="left">A value of type <typeparamref name="T"/> or <c>null</c>.</param>
/// <param name="right">A value of type <typeparamref name="T"/> or <c>null</c>.</param>
public static bool ImplementOpLessThanOrEqual<T>(IComparer<T> comparer, T left, T right)
public static bool ImplementOpLessThanOrEqual<T>(IComparer<T> comparer, [MaybeNull] T left, [MaybeNull] T right)
{
_ = comparer ?? throw new ArgumentNullException(nameof(comparer));
return comparer.Compare(left, right) <= 0;
Expand All @@ -142,7 +143,7 @@ public static bool ImplementOpLessThanOrEqual<T>(IComparer<T> comparer, T left,
/// <param name="comparer">The comparer. May not be <c>null</c>.</param>
/// <param name="left">A value of type <typeparamref name="T"/> or <c>null</c>.</param>
/// <param name="right">A value of type <typeparamref name="T"/> or <c>null</c>.</param>
public static bool ImplementOpGreaterThanOrEqual<T>(IComparer<T> comparer, T left, T right)
public static bool ImplementOpGreaterThanOrEqual<T>(IComparer<T> comparer, [MaybeNull] T left, [MaybeNull] T right)
{
_ = comparer ?? throw new ArgumentNullException(nameof(comparer));
return comparer.Compare(left, right) >= 0;
Expand Down