diff --git a/Microsoft.Toolkit.HighPerformance/Box{T}.cs b/Microsoft.Toolkit.HighPerformance/Box{T}.cs index f103f663663..cb6fad72813 100644 --- a/Microsoft.Toolkit.HighPerformance/Box{T}.cs +++ b/Microsoft.Toolkit.HighPerformance/Box{T}.cs @@ -78,7 +78,7 @@ public static Box GetFrom(object obj) ThrowInvalidCastExceptionForGetFrom(); } - return Unsafe.As>(obj); + return Unsafe.As>(obj)!; } /// @@ -94,7 +94,7 @@ public static Box GetFrom(object obj) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Box DangerousGetFrom(object obj) { - return Unsafe.As>(obj); + return Unsafe.As>(obj)!; } /// @@ -108,7 +108,7 @@ public static bool TryGetFrom(object obj, [NotNullWhen(true)] out Box? box) { if (obj.GetType() == typeof(T)) { - box = Unsafe.As>(obj); + box = Unsafe.As>(obj)!; return true; } @@ -145,7 +145,7 @@ public static implicit operator Box(T value) // manually be implemented in the Box type. For instance, boxing a float // and calling ToString() on it directly, on its boxed object or on a Box // reference retrieved from it will produce the same result in all cases. - return Unsafe.As>(value); + return Unsafe.As>(value)!; } /// diff --git a/Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs b/Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs index b2f75bbf0d3..c05a0f7fdfa 100644 --- a/Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs +++ b/Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs @@ -422,11 +422,11 @@ public object SyncRoot /// The input instance to cache. /// The precomputed hashcode for . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public unsafe void Add(string value, int hashcode) + public void Add(string value, int hashcode) { ref string target = ref TryGet(value.AsSpan(), hashcode); - if (Unsafe.AreSame(ref target, ref Unsafe.AsRef(null))) + if (Unsafe.IsNullRef(ref target)) { Insert(value, hashcode); } @@ -443,11 +443,11 @@ public unsafe void Add(string value, int hashcode) /// The precomputed hashcode for . /// A instance with the contents of . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public unsafe string GetOrAdd(string value, int hashcode) + public string GetOrAdd(string value, int hashcode) { ref string result = ref TryGet(value.AsSpan(), hashcode); - if (!Unsafe.AreSame(ref result, ref Unsafe.AsRef(null))) + if (!Unsafe.IsNullRef(ref result)) { return result; } @@ -464,11 +464,11 @@ public unsafe string GetOrAdd(string value, int hashcode) /// The precomputed hashcode for . /// A instance with the contents of , cached if possible. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public unsafe string GetOrAdd(ReadOnlySpan span, int hashcode) + public string GetOrAdd(ReadOnlySpan span, int hashcode) { ref string result = ref TryGet(span, hashcode); - if (!Unsafe.AreSame(ref result, ref Unsafe.AsRef(null))) + if (!Unsafe.IsNullRef(ref result)) { return result; } @@ -488,11 +488,11 @@ public unsafe string GetOrAdd(ReadOnlySpan span, int hashcode) /// The resulting cached instance, if present /// Whether or not the target instance was found. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public unsafe bool TryGet(ReadOnlySpan span, int hashcode, [NotNullWhen(true)] out string? value) + public bool TryGet(ReadOnlySpan span, int hashcode, [NotNullWhen(true)] out string? value) { ref string result = ref TryGet(span, hashcode); - if (!Unsafe.AreSame(ref result, ref Unsafe.AsRef(null))) + if (!Unsafe.IsNullRef(ref result)) { value = result; @@ -527,7 +527,7 @@ public void Reset() private unsafe ref string TryGet(ReadOnlySpan span, int hashcode) { ref MapEntry mapEntriesRef = ref this.mapEntries.DangerousGetReference(); - ref MapEntry entry = ref Unsafe.AsRef(null); + ref MapEntry entry = ref Unsafe.NullRef(); int length = this.buckets.Length, bucketIndex = hashcode & (length - 1); @@ -547,7 +547,7 @@ private unsafe ref string TryGet(ReadOnlySpan span, int hashcode) } } - return ref Unsafe.AsRef(null); + return ref Unsafe.NullRef(); } /// diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs index 887cdd0f989..36ea2ba06b2 100644 --- a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs +++ b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs @@ -31,7 +31,7 @@ public static partial class ArrayExtensions public static ref T DangerousGetReference(this T[] array) { #if NETCORE_RUNTIME - var arrayData = Unsafe.As(array); + var arrayData = Unsafe.As(array)!; ref T r0 = ref Unsafe.As(ref arrayData.Data); return ref r0; @@ -55,7 +55,7 @@ public static ref T DangerousGetReference(this T[] array) public static ref T DangerousGetReferenceAt(this T[] array, int i) { #if NETCORE_RUNTIME - var arrayData = Unsafe.As(array); + var arrayData = Unsafe.As(array)!; ref T r0 = ref Unsafe.As(ref arrayData.Data); ref T ri = ref Unsafe.Add(ref r0, (nint)(uint)i); diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs index 8807c558b33..9af8101f06a 100644 --- a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs +++ b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs @@ -33,7 +33,7 @@ public static partial class ArrayExtensions public static ref T DangerousGetReference(this T[,] array) { #if NETCORE_RUNTIME - var arrayData = Unsafe.As(array); + var arrayData = Unsafe.As(array)!; ref T r0 = ref Unsafe.As(ref arrayData.Data); return ref r0; @@ -63,7 +63,7 @@ public static ref T DangerousGetReference(this T[,] array) public static ref T DangerousGetReferenceAt(this T[,] array, int i, int j) { #if NETCORE_RUNTIME - var arrayData = Unsafe.As(array); + var arrayData = Unsafe.As(array)!; nint offset = ((nint)(uint)i * (nint)(uint)arrayData.Width) + (nint)(uint)j; ref T r0 = ref Unsafe.As(ref arrayData.Data); ref T ri = ref Unsafe.Add(ref r0, offset); diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs index 846e3c5eadb..3a08882a419 100644 --- a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs +++ b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs @@ -32,7 +32,7 @@ public static partial class ArrayExtensions public static ref T DangerousGetReference(this T[,,] array) { #if NETCORE_RUNTIME - var arrayData = Unsafe.As(array); + var arrayData = Unsafe.As(array)!; ref T r0 = ref Unsafe.As(ref arrayData.Data); return ref r0; @@ -63,7 +63,7 @@ public static ref T DangerousGetReference(this T[,,] array) public static ref T DangerousGetReferenceAt(this T[,,] array, int i, int j, int k) { #if NETCORE_RUNTIME - var arrayData = Unsafe.As(array); + var arrayData = Unsafe.As(array)!; nint offset = ((nint)(uint)i * (nint)(uint)arrayData.Height * (nint)(uint)arrayData.Width) + ((nint)(uint)j * (nint)(uint)arrayData.Width) + (nint)(uint)k; diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs index c94675ecdee..0a61350a17e 100644 --- a/Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs +++ b/Microsoft.Toolkit.HighPerformance/Extensions/ObjectExtensions.cs @@ -31,7 +31,7 @@ public static class ObjectExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IntPtr DangerousGetObjectDataByteOffset(this object obj, ref T data) { - var rawObj = Unsafe.As(obj); + var rawObj = Unsafe.As(obj)!; ref byte r0 = ref rawObj.Data; ref byte r1 = ref Unsafe.As(ref data); @@ -55,7 +55,7 @@ public static IntPtr DangerousGetObjectDataByteOffset(this object obj, ref T [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ref T DangerousGetObjectDataReferenceAt(this object obj, IntPtr offset) { - var rawObj = Unsafe.As(obj); + var rawObj = Unsafe.As(obj)!; ref byte r0 = ref rawObj.Data; ref byte r1 = ref Unsafe.AddByteOffset(ref r0, offset); ref T r2 = ref Unsafe.As(ref r1); diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/StringExtensions.cs b/Microsoft.Toolkit.HighPerformance/Extensions/StringExtensions.cs index 223480d5b06..02027dfb471 100644 --- a/Microsoft.Toolkit.HighPerformance/Extensions/StringExtensions.cs +++ b/Microsoft.Toolkit.HighPerformance/Extensions/StringExtensions.cs @@ -31,7 +31,7 @@ public static ref char DangerousGetReference(this string text) #if NETCOREAPP3_1 return ref Unsafe.AsRef(text.GetPinnableReference()); #elif NETCOREAPP2_1 - var stringData = Unsafe.As(text); + var stringData = Unsafe.As(text)!; return ref stringData.Data; #else @@ -53,7 +53,7 @@ public static ref char DangerousGetReferenceAt(this string text, int i) #if NETCOREAPP3_1 ref char r0 = ref Unsafe.AsRef(text.GetPinnableReference()); #elif NETCOREAPP2_1 - ref char r0 = ref Unsafe.As(text).Data; + ref char r0 = ref Unsafe.As(text)!.Data; #else ref char r0 = ref MemoryMarshal.GetReference(text.AsSpan()); #endif diff --git a/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs b/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs index 6b3cb0cc82e..87055a7714a 100644 --- a/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs +++ b/Microsoft.Toolkit.HighPerformance/Memory/Memory2D{T}.cs @@ -772,7 +772,7 @@ public bool TryGetMemory(out Memory memory) } else if (typeof(T) == typeof(char) && this.instance.GetType() == typeof(string)) { - string text = Unsafe.As(this.instance); + string text = Unsafe.As(this.instance)!; int index = text.AsSpan().IndexOf(in text.DangerousGetObjectDataReferenceAt(this.offset)); ReadOnlyMemory temp = text.AsMemory(index, (int)Length); @@ -786,16 +786,13 @@ public bool TryGetMemory(out Memory memory) } else if (this.instance is MemoryManager memoryManager) { - unsafe - { - // If the object is a MemoryManager, just slice it as needed - memory = memoryManager.Memory.Slice((int)(void*)this.offset, this.height * this.width); - } + // If the object is a MemoryManager, just slice it as needed + memory = memoryManager.Memory.Slice((int)(nint)this.offset, this.height * this.width); } else if (this.instance.GetType() == typeof(T[])) { // If it's a T[] array, also handle the initial offset - T[] array = Unsafe.As(this.instance); + T[] array = Unsafe.As(this.instance)!; int index = array.AsSpan().IndexOf(ref array.DangerousGetObjectDataReferenceAt(this.offset)); memory = array.AsMemory(index, this.height * this.width); diff --git a/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs b/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs index 9b561233c1c..9bd22a40983 100644 --- a/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs +++ b/Microsoft.Toolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs @@ -794,7 +794,7 @@ public bool TryGetMemory(out ReadOnlyMemory memory) // difference between the start of the Span (which directly wraps just the actual character data // within the string), and the input reference, which we can get from the byte offset in use. The result // is the character index which we can use to create the final Memory instance. - string text = Unsafe.As(this.instance); + string text = Unsafe.As(this.instance)!; int index = text.AsSpan().IndexOf(in text.DangerousGetObjectDataReferenceAt(this.offset)); ReadOnlyMemory temp = text.AsMemory(index, (int)Length); @@ -802,16 +802,13 @@ public bool TryGetMemory(out ReadOnlyMemory memory) } else if (this.instance is MemoryManager memoryManager) { - unsafe - { - // If the object is a MemoryManager, just slice it as needed - memory = memoryManager.Memory.Slice((int)(void*)this.offset, this.height * this.width); - } + // If the object is a MemoryManager, just slice it as needed + memory = memoryManager.Memory.Slice((int)(nint)this.offset, this.height * this.width); } else if (this.instance.GetType() == typeof(T[])) { // If it's a T[] array, also handle the initial offset - T[] array = Unsafe.As(this.instance); + T[] array = Unsafe.As(this.instance)!; int index = array.AsSpan().IndexOf(ref array.DangerousGetObjectDataReferenceAt(this.offset)); memory = array.AsMemory(index, this.height * this.width); diff --git a/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj b/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj index 06c9e584da5..e3fe544aaef 100644 --- a/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj +++ b/Microsoft.Toolkit.HighPerformance/Microsoft.Toolkit.HighPerformance.csproj @@ -41,7 +41,7 @@ - + @@ -51,14 +51,14 @@ - + - + @@ -76,6 +76,9 @@ + + + - + diff --git a/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj b/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj index 39663f0f30c..9800aa4b521 100644 --- a/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj +++ b/UnitTests/UnitTests.HighPerformance.UWP/UnitTests.HighPerformance.UWP.csproj @@ -160,7 +160,7 @@ 1.4.0 - 4.7.1 + 5.0.0 diff --git a/UnitTests/UnitTests.Shared/Diagnostics/Test_ThrowHelper.cs b/UnitTests/UnitTests.Shared/Diagnostics/Test_ThrowHelper.cs index 992ee7bca0f..2e91cc69930 100644 --- a/UnitTests/UnitTests.Shared/Diagnostics/Test_ThrowHelper.cs +++ b/UnitTests/UnitTests.Shared/Diagnostics/Test_ThrowHelper.cs @@ -3,10 +3,10 @@ // See the LICENSE file in the project root for more information. using System; -using System.Linq; using System.Collections.Generic; using System.ComponentModel; using System.IO; +using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Threading;