diff options
7 files changed, 16 insertions, 58 deletions
diff --git a/src/Common/src/CoreLib/System/Globalization/CompareInfo.cs b/src/Common/src/CoreLib/System/Globalization/CompareInfo.cs index 71dc270bc2..4ccd739bcd 100644 --- a/src/Common/src/CoreLib/System/Globalization/CompareInfo.cs +++ b/src/Common/src/CoreLib/System/Globalization/CompareInfo.cs @@ -1249,7 +1249,7 @@ namespace System.Globalization int charsWritten = source.AsSpan().ToUpperInvariant(span); // Slice the array to the size returned by ToUpperInvariant. - int hash = Marvin.ComputeHash32(span.Slice(0, charsWritten).AsBytes(), Marvin.DefaultSeed); + int hash = Marvin.ComputeHash32(MemoryMarshal.AsBytes(span.Slice(0, charsWritten)), Marvin.DefaultSeed); // Return the borrowed array if necessary. if (borrowedArr != null) diff --git a/src/System.Memory/ref/System.Memory.cs b/src/System.Memory/ref/System.Memory.cs index 3ee59c099c..b532f049df 100644 --- a/src/System.Memory/ref/System.Memory.cs +++ b/src/System.Memory/ref/System.Memory.cs @@ -9,8 +9,6 @@ namespace System { public static partial class MemoryExtensions { - public static System.ReadOnlySpan<byte> AsBytes<T>(this System.ReadOnlySpan<T> span) where T : struct { throw null; } - public static System.Span<byte> AsBytes<T>(this System.Span<T> span) where T : struct { throw null; } public static System.Memory<T> AsMemory<T>(this System.ArraySegment<T> segment) { throw null; } public static System.Memory<T> AsMemory<T>(this System.ArraySegment<T> segment, int start) { throw null; } public static System.Memory<T> AsMemory<T>(this System.ArraySegment<T> segment, int start, int length) { throw null; } diff --git a/src/System.Memory/src/System/MemoryExtensions.Portable.cs b/src/System.Memory/src/System/MemoryExtensions.Portable.cs index 0782249d6d..c33b508355 100644 --- a/src/System.Memory/src/System/MemoryExtensions.Portable.cs +++ b/src/System.Memory/src/System/MemoryExtensions.Portable.cs @@ -274,50 +274,6 @@ namespace System } /// <summary> - /// Casts a Span of one primitive type <typeparamref name="T"/> to Span of bytes. - /// That type may not contain pointers or references. This is checked at runtime in order to preserve type safety. - /// </summary> - /// <param name="span">The source slice, of type <typeparamref name="T"/>.</param> - /// <exception cref="System.ArgumentException"> - /// Thrown when <typeparamref name="T"/> contains pointers. - /// </exception> - /// <exception cref="System.OverflowException"> - /// Thrown if the Length property of the new Span would exceed Int32.MaxValue. - /// </exception> - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Span<byte> AsBytes<T>(this Span<T> span) - where T : struct - { - if (SpanHelpers.IsReferenceOrContainsReferences<T>()) - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); - - int newLength = checked(span.Length * Unsafe.SizeOf<T>()); - return new Span<byte>(Unsafe.As<Pinnable<byte>>(span.Pinnable), span.ByteOffset, newLength); - } - - /// <summary> - /// Casts a ReadOnlySpan of one primitive type <typeparamref name="T"/> to ReadOnlySpan of bytes. - /// That type may not contain pointers or references. This is checked at runtime in order to preserve type safety. - /// </summary> - /// <param name="span">The source slice, of type <typeparamref name="T"/>.</param> - /// <exception cref="System.ArgumentException"> - /// Thrown when <typeparamref name="T"/> contains pointers. - /// </exception> - /// <exception cref="System.OverflowException"> - /// Thrown if the Length property of the new Span would exceed Int32.MaxValue. - /// </exception> - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ReadOnlySpan<byte> AsBytes<T>(this ReadOnlySpan<T> span) - where T : struct - { - if (SpanHelpers.IsReferenceOrContainsReferences<T>()) - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); - - int newLength = checked(span.Length * Unsafe.SizeOf<T>()); - return new ReadOnlySpan<byte>(Unsafe.As<Pinnable<byte>>(span.Pinnable), span.ByteOffset, newLength); - } - - /// <summary> /// Creates a new readonly span over the portion of the target string. /// </summary> /// <param name="text">The target string.</param> diff --git a/src/System.Memory/tests/Performance/Perf.Span.IndexOf.cs b/src/System.Memory/tests/Performance/Perf.Span.IndexOf.cs index dd678920f4..b260183cc3 100644 --- a/src/System.Memory/tests/Performance/Perf.Span.IndexOf.cs +++ b/src/System.Memory/tests/Performance/Perf.Span.IndexOf.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Runtime.InteropServices; using Microsoft.Xunit.Performance; using Xunit; @@ -44,7 +45,7 @@ namespace System.Memory.Tests { Span<char> charSpan = new char[size]; charSpan[size / 2] = '5'; - Span<byte> byteSpan = charSpan.AsBytes(); + Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan); int index = 0; foreach (BenchmarkIteration iteration in Benchmark.Iterations) @@ -120,7 +121,7 @@ namespace System.Memory.Tests { Span<char> charSpan = new char[size]; charSpan[size / 2] = '5'; - Span<byte> byteSpan = charSpan.AsBytes(); + Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan); int index = 0; foreach (BenchmarkIteration iteration in Benchmark.Iterations) diff --git a/src/System.Memory/tests/Performance/Perf.Span.IndexOfAny.cs b/src/System.Memory/tests/Performance/Perf.Span.IndexOfAny.cs index 26c5f5bf95..f4a78276f5 100644 --- a/src/System.Memory/tests/Performance/Perf.Span.IndexOfAny.cs +++ b/src/System.Memory/tests/Performance/Perf.Span.IndexOfAny.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Runtime.InteropServices; using Microsoft.Xunit.Performance; using Xunit; @@ -44,7 +45,7 @@ namespace System.Memory.Tests { Span<char> charSpan = new char[size]; charSpan[size / 2] = '5'; - Span<byte> byteSpan = charSpan.AsBytes(); + Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan); int index = 0; foreach (BenchmarkIteration iteration in Benchmark.Iterations) @@ -118,7 +119,7 @@ namespace System.Memory.Tests { Span<char> charSpan = new char[size]; charSpan[size / 2] = '5'; - Span<byte> byteSpan = charSpan.AsBytes(); + Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan); int index = 0; foreach (BenchmarkIteration iteration in Benchmark.Iterations) @@ -193,7 +194,7 @@ namespace System.Memory.Tests { Span<char> charSpan = new char[size]; charSpan[size / 2] = '5'; - Span<byte> byteSpan = charSpan.AsBytes(); + Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan); ReadOnlySpan<byte> values = new ReadOnlySpan<byte>(new byte[] { 53, 54, 55, 56 }); // '5' = 53 int index = 0; @@ -219,7 +220,7 @@ namespace System.Memory.Tests { Span<char> charSpan = new char[size]; charSpan[size / 2] = '5'; - Span<byte> byteSpan = charSpan.AsBytes(); + Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan); ReadOnlySpan<byte> values = new ReadOnlySpan<byte>(new byte[] { 54, 55, 56, 57 }); // '5' = 53 int index = 0; @@ -245,7 +246,7 @@ namespace System.Memory.Tests { Span<char> charSpan = new char[size]; charSpan[size / 2] = '5'; - Span<byte> byteSpan = charSpan.AsBytes(); + Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan); ReadOnlySpan<byte> values = new ReadOnlySpan<byte>(new byte[] { 54, 55, 56, 53 }); // '5' = 53 int index = 0; diff --git a/src/System.Net.Primitives/src/System/Net/IPAddress.cs b/src/System.Net.Primitives/src/System/Net/IPAddress.cs index d74f0b60a3..a8e9775899 100644 --- a/src/System.Net.Primitives/src/System/Net/IPAddress.cs +++ b/src/System.Net.Primitives/src/System/Net/IPAddress.cs @@ -6,6 +6,7 @@ using System.Buffers.Binary; using System.Diagnostics; using System.Net.Sockets; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace System.Net { @@ -610,7 +611,7 @@ namespace System.Net const int addressAndScopeIdLength = IPAddressParserStatics.IPv6AddressBytes + sizeof(uint); Span<byte> addressAndScopeIdSpan = stackalloc byte[addressAndScopeIdLength]; - new ReadOnlySpan<ushort>(_numbers).AsBytes().CopyTo(addressAndScopeIdSpan); + MemoryMarshal.AsBytes(new ReadOnlySpan<ushort>(_numbers)).CopyTo(addressAndScopeIdSpan); Span<byte> scopeIdSpan = addressAndScopeIdSpan.Slice(IPAddressParserStatics.IPv6AddressBytes); bool scopeWritten = BitConverter.TryWriteBytes(scopeIdSpan, _addressOrScopeId); Debug.Assert(scopeWritten); @@ -626,7 +627,7 @@ namespace System.Net // For IPv4 addresses, we use Marvin on the integer representation of the Address. hashCode = Marvin.ComputeHash32( - addressOrScopeIdSpan.AsBytes(), + MemoryMarshal.AsBytes(addressOrScopeIdSpan), Marvin.DefaultSeed); } diff --git a/src/System.Private.Xml/src/System/Xml/NameTable.cs b/src/System.Private.Xml/src/System/Xml/NameTable.cs index 60ab26f557..2317dd9767 100644 --- a/src/System.Private.Xml/src/System/Xml/NameTable.cs +++ b/src/System.Private.Xml/src/System/Xml/NameTable.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Runtime.InteropServices; namespace System.Xml { @@ -234,13 +235,13 @@ namespace System.Xml private static int ComputeHash32(string key) { - ReadOnlySpan<byte> bytes = key.AsSpan().AsBytes(); + ReadOnlySpan<byte> bytes = MemoryMarshal.AsBytes(key.AsSpan()); return Marvin.ComputeHash32(bytes, Marvin.DefaultSeed); } private static int ComputeHash32(char[] key, int start, int len) { - ReadOnlySpan<byte> bytes = key.AsSpan(start, len).AsBytes(); + ReadOnlySpan<byte> bytes = MemoryMarshal.AsBytes(key.AsSpan(start, len)); return Marvin.ComputeHash32(bytes, Marvin.DefaultSeed); } } |