Skip to content

Commit eca3eb6

Browse files
Merge branch 'master' into improvement/dispatcher-queue-helper
2 parents 9ada6d9 + 1ef0ab2 commit eca3eb6

File tree

157 files changed

+12767
-307
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

157 files changed

+12767
-307
lines changed

Microsoft.Toolkit.HighPerformance/Box{T}.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static Box<T> GetFrom(object obj)
7878
ThrowInvalidCastExceptionForGetFrom();
7979
}
8080

81-
return Unsafe.As<Box<T>>(obj);
81+
return Unsafe.As<Box<T>>(obj)!;
8282
}
8383

8484
/// <summary>
@@ -94,7 +94,7 @@ public static Box<T> GetFrom(object obj)
9494
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9595
public static Box<T> DangerousGetFrom(object obj)
9696
{
97-
return Unsafe.As<Box<T>>(obj);
97+
return Unsafe.As<Box<T>>(obj)!;
9898
}
9999

100100
/// <summary>
@@ -108,7 +108,7 @@ public static bool TryGetFrom(object obj, [NotNullWhen(true)] out Box<T>? box)
108108
{
109109
if (obj.GetType() == typeof(T))
110110
{
111-
box = Unsafe.As<Box<T>>(obj);
111+
box = Unsafe.As<Box<T>>(obj)!;
112112

113113
return true;
114114
}
@@ -145,7 +145,7 @@ public static implicit operator Box<T>(T value)
145145
// manually be implemented in the Box<T> type. For instance, boxing a float
146146
// and calling ToString() on it directly, on its boxed object or on a Box<T>
147147
// reference retrieved from it will produce the same result in all cases.
148-
return Unsafe.As<Box<T>>(value);
148+
return Unsafe.As<Box<T>>(value)!;
149149
}
150150

151151
/// <inheritdoc/>

Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,11 @@ public object SyncRoot
422422
/// <param name="value">The input <see cref="string"/> instance to cache.</param>
423423
/// <param name="hashcode">The precomputed hashcode for <paramref name="value"/>.</param>
424424
[MethodImpl(MethodImplOptions.AggressiveInlining)]
425-
public unsafe void Add(string value, int hashcode)
425+
public void Add(string value, int hashcode)
426426
{
427427
ref string target = ref TryGet(value.AsSpan(), hashcode);
428428

429-
if (Unsafe.AreSame(ref target, ref Unsafe.AsRef<string>(null)))
429+
if (Unsafe.IsNullRef(ref target))
430430
{
431431
Insert(value, hashcode);
432432
}
@@ -443,11 +443,11 @@ public unsafe void Add(string value, int hashcode)
443443
/// <param name="hashcode">The precomputed hashcode for <paramref name="value"/>.</param>
444444
/// <returns>A <see cref="string"/> instance with the contents of <paramref name="value"/>.</returns>
445445
[MethodImpl(MethodImplOptions.AggressiveInlining)]
446-
public unsafe string GetOrAdd(string value, int hashcode)
446+
public string GetOrAdd(string value, int hashcode)
447447
{
448448
ref string result = ref TryGet(value.AsSpan(), hashcode);
449449

450-
if (!Unsafe.AreSame(ref result, ref Unsafe.AsRef<string>(null)))
450+
if (!Unsafe.IsNullRef(ref result))
451451
{
452452
return result;
453453
}
@@ -464,11 +464,11 @@ public unsafe string GetOrAdd(string value, int hashcode)
464464
/// <param name="hashcode">The precomputed hashcode for <paramref name="span"/>.</param>
465465
/// <returns>A <see cref="string"/> instance with the contents of <paramref name="span"/>, cached if possible.</returns>
466466
[MethodImpl(MethodImplOptions.AggressiveInlining)]
467-
public unsafe string GetOrAdd(ReadOnlySpan<char> span, int hashcode)
467+
public string GetOrAdd(ReadOnlySpan<char> span, int hashcode)
468468
{
469469
ref string result = ref TryGet(span, hashcode);
470470

471-
if (!Unsafe.AreSame(ref result, ref Unsafe.AsRef<string>(null)))
471+
if (!Unsafe.IsNullRef(ref result))
472472
{
473473
return result;
474474
}
@@ -488,11 +488,11 @@ public unsafe string GetOrAdd(ReadOnlySpan<char> span, int hashcode)
488488
/// <param name="value">The resulting cached <see cref="string"/> instance, if present</param>
489489
/// <returns>Whether or not the target <see cref="string"/> instance was found.</returns>
490490
[MethodImpl(MethodImplOptions.AggressiveInlining)]
491-
public unsafe bool TryGet(ReadOnlySpan<char> span, int hashcode, [NotNullWhen(true)] out string? value)
491+
public bool TryGet(ReadOnlySpan<char> span, int hashcode, [NotNullWhen(true)] out string? value)
492492
{
493493
ref string result = ref TryGet(span, hashcode);
494494

495-
if (!Unsafe.AreSame(ref result, ref Unsafe.AsRef<string>(null)))
495+
if (!Unsafe.IsNullRef(ref result))
496496
{
497497
value = result;
498498

@@ -527,7 +527,7 @@ public void Reset()
527527
private unsafe ref string TryGet(ReadOnlySpan<char> span, int hashcode)
528528
{
529529
ref MapEntry mapEntriesRef = ref this.mapEntries.DangerousGetReference();
530-
ref MapEntry entry = ref Unsafe.AsRef<MapEntry>(null);
530+
ref MapEntry entry = ref Unsafe.NullRef<MapEntry>();
531531
int
532532
length = this.buckets.Length,
533533
bucketIndex = hashcode & (length - 1);
@@ -547,7 +547,7 @@ private unsafe ref string TryGet(ReadOnlySpan<char> span, int hashcode)
547547
}
548548
}
549549

550-
return ref Unsafe.AsRef<string>(null);
550+
return ref Unsafe.NullRef<string>();
551551
}
552552

553553
/// <summary>

Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static partial class ArrayExtensions
3131
public static ref T DangerousGetReference<T>(this T[] array)
3232
{
3333
#if NETCORE_RUNTIME
34-
var arrayData = Unsafe.As<RawArrayData>(array);
34+
var arrayData = Unsafe.As<RawArrayData>(array)!;
3535
ref T r0 = ref Unsafe.As<byte, T>(ref arrayData.Data);
3636

3737
return ref r0;
@@ -55,7 +55,7 @@ public static ref T DangerousGetReference<T>(this T[] array)
5555
public static ref T DangerousGetReferenceAt<T>(this T[] array, int i)
5656
{
5757
#if NETCORE_RUNTIME
58-
var arrayData = Unsafe.As<RawArrayData>(array);
58+
var arrayData = Unsafe.As<RawArrayData>(array)!;
5959
ref T r0 = ref Unsafe.As<byte, T>(ref arrayData.Data);
6060
ref T ri = ref Unsafe.Add(ref r0, (nint)(uint)i);
6161

Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static partial class ArrayExtensions
3333
public static ref T DangerousGetReference<T>(this T[,] array)
3434
{
3535
#if NETCORE_RUNTIME
36-
var arrayData = Unsafe.As<RawArray2DData>(array);
36+
var arrayData = Unsafe.As<RawArray2DData>(array)!;
3737
ref T r0 = ref Unsafe.As<byte, T>(ref arrayData.Data);
3838

3939
return ref r0;
@@ -63,7 +63,7 @@ public static ref T DangerousGetReference<T>(this T[,] array)
6363
public static ref T DangerousGetReferenceAt<T>(this T[,] array, int i, int j)
6464
{
6565
#if NETCORE_RUNTIME
66-
var arrayData = Unsafe.As<RawArray2DData>(array);
66+
var arrayData = Unsafe.As<RawArray2DData>(array)!;
6767
nint offset = ((nint)(uint)i * (nint)(uint)arrayData.Width) + (nint)(uint)j;
6868
ref T r0 = ref Unsafe.As<byte, T>(ref arrayData.Data);
6969
ref T ri = ref Unsafe.Add(ref r0, offset);

Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static partial class ArrayExtensions
3232
public static ref T DangerousGetReference<T>(this T[,,] array)
3333
{
3434
#if NETCORE_RUNTIME
35-
var arrayData = Unsafe.As<RawArray3DData>(array);
35+
var arrayData = Unsafe.As<RawArray3DData>(array)!;
3636
ref T r0 = ref Unsafe.As<byte, T>(ref arrayData.Data);
3737

3838
return ref r0;
@@ -63,7 +63,7 @@ public static ref T DangerousGetReference<T>(this T[,,] array)
6363
public static ref T DangerousGetReferenceAt<T>(this T[,,] array, int i, int j, int k)
6464
{
6565
#if NETCORE_RUNTIME
66-
var arrayData = Unsafe.As<RawArray3DData>(array);
66+
var arrayData = Unsafe.As<RawArray3DData>(array)!;
6767
nint offset =
6868
((nint)(uint)i * (nint)(uint)arrayData.Height * (nint)(uint)arrayData.Width) +
6969
((nint)(uint)j * (nint)(uint)arrayData.Width) + (nint)(uint)k;

Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static class ObjectExtensions
3131
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3232
public static IntPtr DangerousGetObjectDataByteOffset<T>(this object obj, ref T data)
3333
{
34-
var rawObj = Unsafe.As<RawObjectData>(obj);
34+
var rawObj = Unsafe.As<RawObjectData>(obj)!;
3535
ref byte r0 = ref rawObj.Data;
3636
ref byte r1 = ref Unsafe.As<T, byte>(ref data);
3737

@@ -55,7 +55,7 @@ public static IntPtr DangerousGetObjectDataByteOffset<T>(this object obj, ref T
5555
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5656
public static ref T DangerousGetObjectDataReferenceAt<T>(this object obj, IntPtr offset)
5757
{
58-
var rawObj = Unsafe.As<RawObjectData>(obj);
58+
var rawObj = Unsafe.As<RawObjectData>(obj)!;
5959
ref byte r0 = ref rawObj.Data;
6060
ref byte r1 = ref Unsafe.AddByteOffset(ref r0, offset);
6161
ref T r2 = ref Unsafe.As<byte, T>(ref r1);

Microsoft.Toolkit.HighPerformance/Extensions/StringExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static ref char DangerousGetReference(this string text)
3131
#if NETCOREAPP3_1
3232
return ref Unsafe.AsRef(text.GetPinnableReference());
3333
#elif NETCOREAPP2_1
34-
var stringData = Unsafe.As<RawStringData>(text);
34+
var stringData = Unsafe.As<RawStringData>(text)!;
3535

3636
return ref stringData.Data;
3737
#else
@@ -53,7 +53,7 @@ public static ref char DangerousGetReferenceAt(this string text, int i)
5353
#if NETCOREAPP3_1
5454
ref char r0 = ref Unsafe.AsRef(text.GetPinnableReference());
5555
#elif NETCOREAPP2_1
56-
ref char r0 = ref Unsafe.As<RawStringData>(text).Data;
56+
ref char r0 = ref Unsafe.As<RawStringData>(text)!.Data;
5757
#else
5858
ref char r0 = ref MemoryMarshal.GetReference(text.AsSpan());
5959
#endif

Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ public bool TryGetMemory(out Memory<T> memory)
772772
}
773773
else if (typeof(T) == typeof(char) && this.instance.GetType() == typeof(string))
774774
{
775-
string text = Unsafe.As<string>(this.instance);
775+
string text = Unsafe.As<string>(this.instance)!;
776776
int index = text.AsSpan().IndexOf(in text.DangerousGetObjectDataReferenceAt<char>(this.offset));
777777
ReadOnlyMemory<char> temp = text.AsMemory(index, (int)Length);
778778

@@ -786,16 +786,13 @@ public bool TryGetMemory(out Memory<T> memory)
786786
}
787787
else if (this.instance is MemoryManager<T> memoryManager)
788788
{
789-
unsafe
790-
{
791-
// If the object is a MemoryManager<T>, just slice it as needed
792-
memory = memoryManager.Memory.Slice((int)(void*)this.offset, this.height * this.width);
793-
}
789+
// If the object is a MemoryManager<T>, just slice it as needed
790+
memory = memoryManager.Memory.Slice((int)(nint)this.offset, this.height * this.width);
794791
}
795792
else if (this.instance.GetType() == typeof(T[]))
796793
{
797794
// If it's a T[] array, also handle the initial offset
798-
T[] array = Unsafe.As<T[]>(this.instance);
795+
T[] array = Unsafe.As<T[]>(this.instance)!;
799796
int index = array.AsSpan().IndexOf(ref array.DangerousGetObjectDataReferenceAt<T>(this.offset));
800797

801798
memory = array.AsMemory(index, this.height * this.width);

Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -794,24 +794,21 @@ public bool TryGetMemory(out ReadOnlyMemory<T> memory)
794794
// difference between the start of the Span<char> (which directly wraps just the actual character data
795795
// within the string), and the input reference, which we can get from the byte offset in use. The result
796796
// is the character index which we can use to create the final Memory<char> instance.
797-
string text = Unsafe.As<string>(this.instance);
797+
string text = Unsafe.As<string>(this.instance)!;
798798
int index = text.AsSpan().IndexOf(in text.DangerousGetObjectDataReferenceAt<char>(this.offset));
799799
ReadOnlyMemory<char> temp = text.AsMemory(index, (int)Length);
800800

801801
memory = Unsafe.As<ReadOnlyMemory<char>, ReadOnlyMemory<T>>(ref temp);
802802
}
803803
else if (this.instance is MemoryManager<T> memoryManager)
804804
{
805-
unsafe
806-
{
807-
// If the object is a MemoryManager<T>, just slice it as needed
808-
memory = memoryManager.Memory.Slice((int)(void*)this.offset, this.height * this.width);
809-
}
805+
// If the object is a MemoryManager<T>, just slice it as needed
806+
memory = memoryManager.Memory.Slice((int)(nint)this.offset, this.height * this.width);
810807
}
811808
else if (this.instance.GetType() == typeof(T[]))
812809
{
813810
// If it's a T[] array, also handle the initial offset
814-
T[] array = Unsafe.As<T[]>(this.instance);
811+
T[] array = Unsafe.As<T[]>(this.instance)!;
815812
int index = array.AsSpan().IndexOf(ref array.DangerousGetObjectDataReferenceAt<T>(this.offset));
816813

817814
memory = array.AsMemory(index, this.height * this.width);

Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
4242
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
4343
<PackageReference Include="System.Threading.Tasks.Parallel" Version="4.3.0" />
44-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" />
44+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
4545
</ItemGroup>
4646
</When>
4747
<When Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
@@ -51,14 +51,14 @@
5151
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.0" />
5252
<PackageReference Include="System.Memory" Version="4.5.4" />
5353
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
54-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" />
54+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
5555
</ItemGroup>
5656
</When>
5757
<When Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
5858
<ItemGroup>
5959

6060
<!-- .NET Standard 2.1 doesn't have the Unsafe type -->
61-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" />
61+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
6262
</ItemGroup>
6363
<PropertyGroup>
6464

@@ -76,6 +76,9 @@
7676
</PropertyGroup>
7777
</When>
7878
<When Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
79+
<ItemGroup>
80+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
81+
</ItemGroup>
7982
<PropertyGroup>
8083

8184
<!-- NETCORE_RUNTIME: to avoid issues with APIs that assume a specific memory layout, we define a
@@ -89,7 +92,7 @@
8992
</When>
9093
<When Condition=" '$(TargetFramework)' == 'netcoreapp2.1' ">
9194
<ItemGroup>
92-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" />
95+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
9396
</ItemGroup>
9497
<PropertyGroup>
9598
<DefineConstants>SPAN_RUNTIME_SUPPORT;NETCORE_RUNTIME</DefineConstants>

0 commit comments

Comments
 (0)