diff --git a/src/core-taggeds-optional/Optional/Optional.T.InternalComparer/Optional.T.InternalComparer.cs b/src/core-taggeds-optional/Optional/Optional.T.InternalComparer/Optional.T.InternalComparer.cs index 7a9d8235..73f00a2c 100644 --- a/src/core-taggeds-optional/Optional/Optional.T.InternalComparer/Optional.T.InternalComparer.cs +++ b/src/core-taggeds-optional/Optional/Optional.T.InternalComparer/Optional.T.InternalComparer.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Runtime.CompilerServices; namespace System; @@ -13,20 +12,8 @@ internal InternalComparer(IComparer comparer) => this.comparer = comparer; - [MethodImpl(MethodImplOptions.AggressiveInlining)] public int Compare(Optional x, Optional y) - { - if (x.hasValue != y.hasValue) - { - return x.hasValue ? ComparisonResult.GreaterThan : ComparisonResult.LessThan; - } - - if (x.hasValue) - { - return ComparisonResult.Normalize(comparer.Compare(x.value, y.value)); - } - - return ComparisonResult.EqualTo; - } + => + x.InternalCompareTo(y, comparer); } } diff --git a/src/core-taggeds-optional/Optional/Optional.T/Internal.Comparison/Optional.T.InternalComparison.cs b/src/core-taggeds-optional/Optional/Optional.T/Internal.Comparison/Optional.T.InternalComparison.cs new file mode 100644 index 00000000..56f391ee --- /dev/null +++ b/src/core-taggeds-optional/Optional/Optional.T/Internal.Comparison/Optional.T.InternalComparison.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using System.Runtime.CompilerServices; + +namespace System; + +partial struct Optional +{ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal int InternalCompareTo(Optional other, IComparer comparer) + { + if (hasValue != other.hasValue) + { + return hasValue ? ComparisonResult.GreaterThan : ComparisonResult.LessThan; + } + + if (hasValue) + { + return ComparisonResult.Normalize(comparer.Compare(value, other.value)); + } + + return ComparisonResult.EqualTo; + } +} diff --git a/src/core-taggeds-optional/Optional/Optional/Optional.Comparison.cs b/src/core-taggeds-optional/Optional/Optional/Optional.Comparison.cs index 9fbe6d6b..1ce23a18 100644 --- a/src/core-taggeds-optional/Optional/Optional/Optional.Comparison.cs +++ b/src/core-taggeds-optional/Optional/Optional/Optional.Comparison.cs @@ -1,4 +1,6 @@ -namespace System; +using System.Collections.Generic; + +namespace System; partial class Optional { @@ -6,5 +8,5 @@ partial class Optional internal static int Compare(Optional left, Optional right) where T : IComparable => - OptionalComparer.Default.Compare(left, right); + left.InternalCompareTo(right, Comparer.Default); } diff --git a/src/core-taggeds-optional/Optional/OptionalComparer.T/OptionalComparer.T.cs b/src/core-taggeds-optional/Optional/OptionalComparer.T/OptionalComparer.T.cs index af2e31a6..f1830baa 100644 --- a/src/core-taggeds-optional/Optional/OptionalComparer.T/OptionalComparer.T.cs +++ b/src/core-taggeds-optional/Optional/OptionalComparer.T/OptionalComparer.T.cs @@ -6,11 +6,11 @@ namespace System; internal sealed class OptionalComparer : IComparer> where T : IComparable { - private readonly Optional.InternalComparer comparer; + private readonly IComparer comparer; private OptionalComparer(IComparer comparer) => - this.comparer = new(comparer); + this.comparer = comparer; public static OptionalComparer Create(IComparer? comparer) => @@ -24,7 +24,7 @@ public static OptionalComparer Create() public int Compare(Optional x, Optional y) => - comparer.Compare(x, y); + x.InternalCompareTo(y, comparer); private static class InnerDefault { diff --git a/src/core-taggeds-optional/Optional/OptionalExtensions/Comparison.cs b/src/core-taggeds-optional/Optional/OptionalExtensions/Comparison.cs index 0adb8eab..ee6d9fa1 100644 --- a/src/core-taggeds-optional/Optional/OptionalExtensions/Comparison.cs +++ b/src/core-taggeds-optional/Optional/OptionalExtensions/Comparison.cs @@ -1,4 +1,6 @@ -namespace System; +using System.Collections.Generic; + +namespace System; partial class OptionalExtensions { @@ -6,5 +8,5 @@ partial class OptionalExtensions internal static int CompareTo(this Optional optional, Optional other) where T : IComparable => - OptionalComparer.Default.Compare(optional, other); + optional.InternalCompareTo(other, Comparer.Default); }