Skip to content

Commit 829304f

Browse files
committed
Add more EquatableArray<T> APIs
1 parent f0e7729 commit 829304f

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

CommunityToolkit.Mvvm.SourceGenerators/Helpers/EquatableArray.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@
1010

1111
namespace CommunityToolkit.Mvvm.SourceGenerators.Helpers;
1212

13+
/// <summary>
14+
/// Extensions for <see cref="EquatableArray{T}"/>.
15+
/// </summary>
16+
internal static class EquatableArray
17+
{
18+
/// <summary>
19+
/// Creates an <see cref="EquatableArray{T}"/> instance from a given <see cref="ImmutableArray{T}"/>.
20+
/// </summary>
21+
/// <typeparam name="T">The type of items in the input array.</typeparam>
22+
/// <param name="array">The input <see cref="ImmutableArray{T}"/> instance.</param>
23+
/// <returns>An <see cref="EquatableArray{T}"/> instance from a given <see cref="ImmutableArray{T}"/>.</returns>
24+
public static EquatableArray<T> AsEquatableArray<T>(this ImmutableArray<T> array)
25+
where T : IEquatable<T>
26+
{
27+
return new(array);
28+
}
29+
}
30+
1331
/// <summary>
1432
/// An imutable, equatable array. This is equivalent to <see cref="ImmutableArray{T}"/> but with value equality support.
1533
/// </summary>
@@ -31,6 +49,26 @@ public EquatableArray(ImmutableArray<T> array)
3149
this.array = Unsafe.As<ImmutableArray<T>, T[]?>(ref array);
3250
}
3351

52+
/// <summary>
53+
/// Gets a reference to an item at a specified position within the array.
54+
/// </summary>
55+
/// <param name="index">The index of the item to retrieve a reference to.</param>
56+
/// <returns>A reference to an item at a specified position within the array.</returns>
57+
public ref readonly T this[int index]
58+
{
59+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
60+
get => ref AsImmutableArray().ItemRef(index);
61+
}
62+
63+
/// <summary>
64+
/// Gets a value indicating whether the current array is empty.
65+
/// </summary>
66+
public bool IsEmpty
67+
{
68+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
69+
get => AsImmutableArray().IsEmpty;
70+
}
71+
3472
/// <sinheritdoc/>
3573
public bool Equals(EquatableArray<T> array)
3674
{
@@ -65,6 +103,7 @@ public override int GetHashCode()
65103
/// Gets an <see cref="ImmutableArray{T}"/> instance from the current <see cref="EquatableArray{T}"/>.
66104
/// </summary>
67105
/// <returns>The <see cref="ImmutableArray{T}"/> from the current <see cref="EquatableArray{T}"/>.</returns>
106+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
68107
public ImmutableArray<T> AsImmutableArray()
69108
{
70109
return Unsafe.As<T[]?, ImmutableArray<T>>(ref Unsafe.AsRef(in this.array));
@@ -80,6 +119,24 @@ public static EquatableArray<T> FromImmutableArray(ImmutableArray<T> array)
80119
return new(array);
81120
}
82121

122+
/// <summary>
123+
/// Returns a <see cref="ReadOnlySpan{T}"/> wrapping the current items.
124+
/// </summary>
125+
/// <returns>A <see cref="ReadOnlySpan{T}"/> wrapping the current items.</returns>
126+
public ReadOnlySpan<T> AsSpan()
127+
{
128+
return AsImmutableArray().AsSpan();
129+
}
130+
131+
/// <summary>
132+
/// Copies the contents of this <see cref="EquatableArray{T}"/> instance. to a mutable array.
133+
/// </summary>
134+
/// <returns>The newly instantiated array.</returns>
135+
public T[] ToArray()
136+
{
137+
return AsImmutableArray().ToArray();
138+
}
139+
83140
/// <summary>
84141
/// Implicitly converts an <see cref="ImmutableArray{T}"/> to <see cref="EquatableArray{T}"/>.
85142
/// </summary>

0 commit comments

Comments
 (0)