Skip to content

Commit f81cf4b

Browse files
committed
Switch to target-type new() for direct return values
1 parent 57f4c3d commit f81cf4b

25 files changed

+78
-78
lines changed

CommunityToolkit.Diagnostics/Extensions/ValueTypeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ public static unsafe string ToHexString<T>(this T value)
6363
p[j] = (char)Unsafe.Add(ref rh, high);
6464
}
6565

66-
return new string(p, 0, bufferSize);
66+
return new(p, 0, bufferSize);
6767
}
6868
}

CommunityToolkit.HighPerformance/Buffers/ArrayPoolBufferWriter{T}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ public override string ToString()
325325
if (typeof(T) == typeof(char) &&
326326
this.array is char[] chars)
327327
{
328-
return new string(chars, 0, this.index);
328+
return new(chars, 0, this.index);
329329
}
330330

331331
// Same representation used in Span<T>

CommunityToolkit.HighPerformance/Buffers/Internals/ArrayMemoryManager{TFrom,TTo}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public override unsafe MemoryHandle Pin(int elementIndex = 0)
8686
ref byte r2 = ref Unsafe.Add(ref r1, byteOffset);
8787
void* pi = Unsafe.AsPointer(ref r2);
8888

89-
return new MemoryHandle(pi, handle);
89+
return new(pi, handle);
9090
}
9191

9292
/// <inheritdoc/>

CommunityToolkit.HighPerformance/Buffers/Internals/RawObjectMemoryManager{T}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public override unsafe MemoryHandle Pin(int elementIndex = 0)
7272
ref T r1 = ref Unsafe.Add(ref r0, (nint)(uint)elementIndex);
7373
void* p = Unsafe.AsPointer(ref r1);
7474

75-
return new MemoryHandle(p, handle);
75+
return new(p, handle);
7676
}
7777

7878
/// <inheritdoc/>

CommunityToolkit.HighPerformance/Buffers/Internals/StringMemoryManager{TTo}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public override unsafe MemoryHandle Pin(int elementIndex = 0)
8282
ref byte r2 = ref Unsafe.Add(ref r1, byteOffset);
8383
void* pi = Unsafe.AsPointer(ref r2);
8484

85-
return new MemoryHandle(pi, handle);
85+
return new(pi, handle);
8686
}
8787

8888
/// <inheritdoc/>

CommunityToolkit.HighPerformance/Buffers/MemoryOwner{T}.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public Memory<T> Memory
157157
ThrowObjectDisposedException();
158158
}
159159

160-
return new Memory<T>(array!, this.start, this.length);
160+
return new(array!, this.start, this.length);
161161
}
162162
}
163163

@@ -190,7 +190,7 @@ public Span<T> Span
190190
// especially if T is a value type, in which case the covariance check is JIT removed.
191191
return MemoryMarshal.CreateSpan(ref r0, this.length);
192192
#else
193-
return new Span<T>(array!, this.start, this.length);
193+
return new(array!, this.start, this.length);
194194
#endif
195195
}
196196
}
@@ -239,7 +239,7 @@ public ArraySegment<T> DangerousGetArray()
239239
ThrowObjectDisposedException();
240240
}
241241

242-
return new ArraySegment<T>(array!, this.start, this.length);
242+
return new(array!, this.start, this.length);
243243
}
244244

245245
/// <summary>
@@ -282,7 +282,7 @@ public MemoryOwner<T> Slice(int start, int length)
282282
// suppress the finalizer to reduce the overhead on the garbage collector.
283283
GC.SuppressFinalize(this);
284284

285-
return new MemoryOwner<T>(start, length, this.pool, array!);
285+
return new(start, length, this.pool, array!);
286286
}
287287

288288
/// <inheritdoc/>
@@ -312,7 +312,7 @@ public override string ToString()
312312
if (typeof(T) == typeof(char) &&
313313
this.array is char[] chars)
314314
{
315-
return new string(chars, this.start, this.length);
315+
return new(chars, this.start, this.length);
316316
}
317317

318318
// Same representation used in Span<T>

CommunityToolkit.HighPerformance/Buffers/SpanOwner{T}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public Span<T> Span
146146

147147
return MemoryMarshal.CreateSpan(ref r0, this.length);
148148
#else
149-
return new Span<T>(this.array, 0, this.length);
149+
return new(this.array, 0, this.length);
150150
#endif
151151
}
152152
}
@@ -192,7 +192,7 @@ public override string ToString()
192192
if (typeof(T) == typeof(char) &&
193193
this.array is char[] chars)
194194
{
195-
return new string(chars, 0, this.length);
195+
return new(chars, 0, this.length);
196196
}
197197

198198
// Same representation used in Span<T>

CommunityToolkit.HighPerformance/Enumerables/ReadOnlyRefEnumerable{T}.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static ReadOnlyRefEnumerable<T> DangerousCreate(in T value, int length, i
9898

9999
OverflowHelper.EnsureIsInNativeIntRange(length, 1, step);
100100

101-
return new ReadOnlyRefEnumerable<T>(in value, length, step);
101+
return new(in value, length, step);
102102
}
103103
#else
104104
/// <summary>
@@ -182,9 +182,9 @@ public ref readonly T this[Index index]
182182
public Enumerator GetEnumerator()
183183
{
184184
#if NETSTANDARD2_1_OR_GREATER
185-
return new Enumerator(this.span, this.step);
185+
return new(this.span, this.step);
186186
#else
187-
return new Enumerator(this.instance, this.offset, this.length, this.step);
187+
return new(this.instance, this.offset, this.length, this.step);
188188
#endif
189189
}
190190

@@ -342,9 +342,9 @@ public T[] ToArray()
342342
public static implicit operator ReadOnlyRefEnumerable<T>(RefEnumerable<T> enumerable)
343343
{
344344
#if NETSTANDARD2_1_OR_GREATER
345-
return new ReadOnlyRefEnumerable<T>(enumerable.Span, enumerable.Step);
345+
return new(enumerable.Span, enumerable.Step);
346346
#else
347-
return new ReadOnlyRefEnumerable<T>(enumerable.Instance, enumerable.Offset, enumerable.Length, enumerable.Step);
347+
return new(enumerable.Instance, enumerable.Offset, enumerable.Length, enumerable.Step);
348348
#endif
349349
}
350350

CommunityToolkit.HighPerformance/Enumerables/ReadOnlySpanEnumerable{T}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public readonly Item Current
6868
ref T ri = ref Unsafe.Add(ref r0, (nint)(uint)this.index);
6969

7070
// See comment in SpanEnumerable<T> about this
71-
return new Item(ref ri, this.index);
71+
return new(ref ri, this.index);
7272
#else
73-
return new Item(this.span, this.index);
73+
return new(this.span, this.index);
7474
#endif
7575
}
7676
}

CommunityToolkit.HighPerformance/Enumerables/RefEnumerable{T}.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static RefEnumerable<T> DangerousCreate(ref T value, int length, int step
8181

8282
OverflowHelper.EnsureIsInNativeIntRange(length, 1, step);
8383

84-
return new RefEnumerable<T>(ref value, length, step);
84+
return new(ref value, length, step);
8585
}
8686
#else
8787
/// <summary>
@@ -165,9 +165,9 @@ public ref T this[Index index]
165165
public Enumerator GetEnumerator()
166166
{
167167
#if NETSTANDARD2_1_OR_GREATER
168-
return new Enumerator(this.Span, this.Step);
168+
return new(this.Span, this.Step);
169169
#else
170-
return new Enumerator(this.Instance, this.Offset, this.Length, this.Step);
170+
return new(this.Instance, this.Offset, this.Length, this.Step);
171171
#endif
172172
}
173173

CommunityToolkit.HighPerformance/Enumerables/SpanEnumerable{T}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ public readonly Item Current
7373
// first item as the target reference, and the length as a host for the
7474
// current original offset. This is not possible on eg. .NET Standard 2.0,
7575
// as we lack the API to create Span<T>-s from arbitrary references.
76-
return new Item(ref ri, this.index);
76+
return new(ref ri, this.index);
7777
#else
78-
return new Item(this.span, this.index);
78+
return new(this.span, this.index);
7979
#endif
8080
}
8181
}

CommunityToolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ public static RefEnumerable<T> GetRow<T>(this T[,] array, int row)
136136
#if NETSTANDARD2_1_OR_GREATER
137137
ref T r0 = ref array.DangerousGetReferenceAt(row, 0);
138138

139-
return new RefEnumerable<T>(ref r0, width, 1);
139+
return new(ref r0, width, 1);
140140
#else
141141
ref T r0 = ref array.DangerousGetReferenceAt(row, 0);
142142
IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref r0);
143143

144-
return new RefEnumerable<T>(array, offset, width, 1);
144+
return new(array, offset, width, 1);
145145
#endif
146146
}
147147

@@ -189,12 +189,12 @@ public static RefEnumerable<T> GetColumn<T>(this T[,] array, int column)
189189
#if NETSTANDARD2_1_OR_GREATER
190190
ref T r0 = ref array.DangerousGetReferenceAt(0, column);
191191

192-
return new RefEnumerable<T>(ref r0, height, width);
192+
return new(ref r0, height, width);
193193
#else
194194
ref T r0 = ref array.DangerousGetReferenceAt(0, column);
195195
IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref r0);
196196

197-
return new RefEnumerable<T>(array, offset, height, width);
197+
return new(array, offset, height, width);
198198
#endif
199199
}
200200

CommunityToolkit.HighPerformance/Extensions/StreamExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public static ValueTask<int> ReadAsync(this Stream stream, Memory<byte> buffer,
3131
{
3232
if (cancellationToken.IsCancellationRequested)
3333
{
34-
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
34+
return new(Task.FromCanceled<int>(cancellationToken));
3535
}
3636

3737
// If the memory wraps an array, extract it and use it directly
3838
if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> segment))
3939
{
40-
return new ValueTask<int>(stream.ReadAsync(segment.Array!, segment.Offset, segment.Count, cancellationToken));
40+
return new(stream.ReadAsync(segment.Array!, segment.Offset, segment.Count, cancellationToken));
4141
}
4242

4343
// Local function used as the fallback path. This happens when the input memory
@@ -68,7 +68,7 @@ static async Task<int> ReadAsyncFallback(Stream stream, Memory<byte> buffer, Can
6868
}
6969
}
7070

71-
return new ValueTask<int>(ReadAsyncFallback(stream, buffer, cancellationToken));
71+
return new(ReadAsyncFallback(stream, buffer, cancellationToken));
7272
}
7373

7474
/// <summary>
@@ -82,12 +82,12 @@ public static ValueTask WriteAsync(this Stream stream, ReadOnlyMemory<byte> buff
8282
{
8383
if (cancellationToken.IsCancellationRequested)
8484
{
85-
return new ValueTask(Task.FromCanceled(cancellationToken));
85+
return new(Task.FromCanceled(cancellationToken));
8686
}
8787

8888
if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> segment))
8989
{
90-
return new ValueTask(stream.WriteAsync(segment.Array!, segment.Offset, segment.Count, cancellationToken));
90+
return new(stream.WriteAsync(segment.Array!, segment.Offset, segment.Count, cancellationToken));
9191
}
9292

9393
// Local function, same idea as above
@@ -107,7 +107,7 @@ static async Task WriteAsyncFallback(Stream stream, ReadOnlyMemory<byte> buffer,
107107
}
108108
}
109109

110-
return new ValueTask(WriteAsyncFallback(stream, buffer, cancellationToken));
110+
return new(WriteAsyncFallback(stream, buffer, cancellationToken));
111111
}
112112

113113
/// <summary>

CommunityToolkit.HighPerformance/Memory/Memory2D{T}.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ public static Memory2D<T> DangerousCreate(object instance, ref T value, int heig
545545

546546
IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(instance, ref value);
547547

548-
return new Memory2D<T>(instance, offset, height, width, pitch);
548+
return new(instance, offset, height, width, pitch);
549549
}
550550

551551
/// <summary>
@@ -605,16 +605,16 @@ public Span2D<T> Span
605605
ref T r0 = ref memoryManager.GetSpan().DangerousGetReference();
606606
ref T r1 = ref Unsafe.Add(ref r0, this.offset);
607607

608-
return new Span2D<T>(ref r1, this.height, this.width, this.pitch);
608+
return new(ref r1, this.height, this.width, this.pitch);
609609
}
610610
else
611611
{
612612
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(this.instance, this.offset);
613613

614-
return new Span2D<T>(ref r0, this.height, this.width, this.pitch);
614+
return new(ref r0, this.height, this.width, this.pitch);
615615
}
616616
#else
617-
return new Span2D<T>(this.instance, this.offset, this.height, this.width, this.pitch);
617+
return new(this.instance, this.offset, this.height, this.width, this.pitch);
618618
#endif
619619
}
620620

@@ -684,7 +684,7 @@ public Memory2D<T> Slice(int row, int column, int height, int width)
684684

685685
IntPtr offset = this.offset + (shift * Unsafe.SizeOf<T>());
686686

687-
return new Memory2D<T>(this.instance!, offset, height, width, pitch);
687+
return new(this.instance!, offset, height, width, pitch);
688688
}
689689

690690
/// <summary>
@@ -743,7 +743,7 @@ public unsafe MemoryHandle Pin()
743743

744744
void* pointer = Unsafe.AsPointer(ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(this.instance, this.offset));
745745

746-
return new MemoryHandle(pointer, handle);
746+
return new(pointer, handle);
747747
}
748748

749749
return default;

CommunityToolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public static ReadOnlyMemory2D<T> DangerousCreate(object instance, ref T value,
557557

558558
IntPtr offset = ObjectMarshal.DangerousGetObjectDataByteOffset(instance, ref value);
559559

560-
return new ReadOnlyMemory2D<T>(instance, offset, height, width, pitch);
560+
return new(instance, offset, height, width, pitch);
561561
}
562562

563563
/// <summary>
@@ -617,17 +617,17 @@ public ReadOnlySpan2D<T> Span
617617
ref T r0 = ref memoryManager.GetSpan().DangerousGetReference();
618618
ref T r1 = ref Unsafe.Add(ref r0, this.offset);
619619

620-
return new ReadOnlySpan2D<T>(in r1, this.height, this.width, this.pitch);
620+
return new(in r1, this.height, this.width, this.pitch);
621621
}
622622
else
623623
{
624624
// This handles both arrays and strings
625625
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(this.instance, this.offset);
626626

627-
return new ReadOnlySpan2D<T>(in r0, this.height, this.width, this.pitch);
627+
return new(in r0, this.height, this.width, this.pitch);
628628
}
629629
#else
630-
return new ReadOnlySpan2D<T>(this.instance, this.offset, this.height, this.width, this.pitch);
630+
return new(this.instance, this.offset, this.height, this.width, this.pitch);
631631
#endif
632632
}
633633

@@ -697,7 +697,7 @@ public ReadOnlyMemory2D<T> Slice(int row, int column, int height, int width)
697697

698698
IntPtr offset = this.offset + (shift * Unsafe.SizeOf<T>());
699699

700-
return new ReadOnlyMemory2D<T>(this.instance!, offset, height, width, pitch);
700+
return new(this.instance!, offset, height, width, pitch);
701701
}
702702

703703
/// <summary>
@@ -756,7 +756,7 @@ public unsafe MemoryHandle Pin()
756756

757757
void* pointer = Unsafe.AsPointer(ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(this.instance, this.offset));
758758

759-
return new MemoryHandle(pointer, handle);
759+
return new(pointer, handle);
760760
}
761761

762762
return default;

CommunityToolkit.HighPerformance/Memory/ReadOnlySpan2D{T}.Enumerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public ReadOnlyRefEnumerable<T> GetRow(int row)
3636
ref T r1 = ref Unsafe.Add(ref r0, startIndex);
3737

3838
#if NETSTANDARD2_1_OR_GREATER
39-
return new ReadOnlyRefEnumerable<T>(in r1, Width, 1);
39+
return new(in r1, Width, 1);
4040
#else
4141
IntPtr offset = RuntimeHelpers.GetObjectDataOrReferenceByteOffset(this.instance, ref r1);
4242

43-
return new ReadOnlyRefEnumerable<T>(this.instance!, offset, this.width, 1);
43+
return new(this.instance!, offset, this.width, 1);
4444
#endif
4545
}
4646

@@ -62,11 +62,11 @@ public ReadOnlyRefEnumerable<T> GetColumn(int column)
6262
ref T r1 = ref Unsafe.Add(ref r0, (nint)(uint)column);
6363

6464
#if NETSTANDARD2_1_OR_GREATER
65-
return new ReadOnlyRefEnumerable<T>(in r1, Height, this.stride);
65+
return new(in r1, Height, this.stride);
6666
#else
6767
IntPtr offset = RuntimeHelpers.GetObjectDataOrReferenceByteOffset(this.instance, ref r1);
6868

69-
return new ReadOnlyRefEnumerable<T>(this.instance!, offset, Height, this.stride);
69+
return new(this.instance!, offset, Height, this.stride);
7070
#endif
7171
}
7272

0 commit comments

Comments
 (0)