10
10
11
11
namespace CommunityToolkit . Mvvm . SourceGenerators . Helpers ;
12
12
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
+
13
31
/// <summary>
14
32
/// An imutable, equatable array. This is equivalent to <see cref="ImmutableArray{T}"/> but with value equality support.
15
33
/// </summary>
@@ -31,6 +49,26 @@ public EquatableArray(ImmutableArray<T> array)
31
49
this . array = Unsafe . As < ImmutableArray < T > , T [ ] ? > ( ref array ) ;
32
50
}
33
51
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
+
34
72
/// <sinheritdoc/>
35
73
public bool Equals ( EquatableArray < T > array )
36
74
{
@@ -65,6 +103,7 @@ public override int GetHashCode()
65
103
/// Gets an <see cref="ImmutableArray{T}"/> instance from the current <see cref="EquatableArray{T}"/>.
66
104
/// </summary>
67
105
/// <returns>The <see cref="ImmutableArray{T}"/> from the current <see cref="EquatableArray{T}"/>.</returns>
106
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
68
107
public ImmutableArray < T > AsImmutableArray ( )
69
108
{
70
109
return Unsafe . As < T [ ] ? , ImmutableArray < T > > ( ref Unsafe . AsRef ( in this . array ) ) ;
@@ -80,6 +119,24 @@ public static EquatableArray<T> FromImmutableArray(ImmutableArray<T> array)
80
119
return new ( array ) ;
81
120
}
82
121
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
+
83
140
/// <summary>
84
141
/// Implicitly converts an <see cref="ImmutableArray{T}"/> to <see cref="EquatableArray{T}"/>.
85
142
/// </summary>
0 commit comments