From 0496db7a505ae3b8de7bf3d50e2262b351832d4c Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Thu, 12 Oct 2017 18:07:17 -0700 Subject: [PATCH 1/5] Adding ValidateTypeIsBlittable SpanHelper method and rename constant. --- src/System.Memory/src/System.Memory.csproj | 5 ++-- .../src/System/Buffers/Binary/Reader.cs | 26 +++---------------- .../src/System/Buffers/Binary/Writer.cs | 26 +++---------------- src/System.Memory/src/System/ReadOnlySpan.cs | 4 +-- src/System.Memory/src/System/Span.cs | 4 +-- .../src/System/SpanExtensions.Portable.cs | 20 +++++--------- src/System.Memory/src/System/SpanHelpers.T.cs | 15 +++++++++++ 7 files changed, 35 insertions(+), 65 deletions(-) diff --git a/src/System.Memory/src/System.Memory.csproj b/src/System.Memory/src/System.Memory.csproj index 41e7dd3c3cd0..88e9f01848c7 100644 --- a/src/System.Memory/src/System.Memory.csproj +++ b/src/System.Memory/src/System.Memory.csproj @@ -7,7 +7,7 @@ false $(OutputPath)$(MSBuildProjectName).xml true - $(DefineConstants);IsPartialFacade + $(DefineConstants);FEATURE_FASTSPAN $(DefineConstants);netcoreapp $(DefineConstants);netstandard11 @@ -55,9 +55,8 @@ + - - diff --git a/src/System.Memory/src/System/Buffers/Binary/Reader.cs b/src/System.Memory/src/System/Buffers/Binary/Reader.cs index c150d510923c..fc98a09d77ff 100644 --- a/src/System.Memory/src/System/Buffers/Binary/Reader.cs +++ b/src/System.Memory/src/System/Buffers/Binary/Reader.cs @@ -98,17 +98,8 @@ public static ulong ReverseEndianness(ulong value) public static T ReadMachineEndian(ReadOnlySpan buffer) where T : struct { -#if IsPartialFacade - if (RuntimeHelpers.IsReferenceOrContainsReferences()) - { - throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); - } -#else - if (SpanHelpers.IsReferenceOrContainsReferences()) - { - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); - } -#endif + SpanHelpers.ValidateTypeIsBlittable(); + if (Unsafe.SizeOf() > buffer.Length) { throw new ArgumentOutOfRangeException(); @@ -124,17 +115,8 @@ public static T ReadMachineEndian(ReadOnlySpan buffer) public static bool TryReadMachineEndian(ReadOnlySpan buffer, out T value) where T : struct { -#if IsPartialFacade - if (RuntimeHelpers.IsReferenceOrContainsReferences()) - { - throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); - } -#else - if (SpanHelpers.IsReferenceOrContainsReferences()) - { - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); - } -#endif + SpanHelpers.ValidateTypeIsBlittable(); + if (Unsafe.SizeOf() > (uint)buffer.Length) { value = default; diff --git a/src/System.Memory/src/System/Buffers/Binary/Writer.cs b/src/System.Memory/src/System/Buffers/Binary/Writer.cs index 6dd14b631831..31bc930cd9ef 100644 --- a/src/System.Memory/src/System/Buffers/Binary/Writer.cs +++ b/src/System.Memory/src/System/Buffers/Binary/Writer.cs @@ -16,17 +16,8 @@ public static partial class BinaryPrimitives public static void WriteMachineEndian(Span buffer, ref T value) where T : struct { -#if IsPartialFacade - if (RuntimeHelpers.IsReferenceOrContainsReferences()) - { - throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); - } -#else - if (SpanHelpers.IsReferenceOrContainsReferences()) - { - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); - } -#endif + SpanHelpers.ValidateTypeIsBlittable(); + if ((uint)Unsafe.SizeOf() > (uint)buffer.Length) { throw new ArgumentOutOfRangeException(); @@ -42,17 +33,8 @@ public static void WriteMachineEndian(Span buffer, ref T value) public static bool TryWriteMachineEndian(Span buffer, ref T value) where T : struct { -#if IsPartialFacade - if (RuntimeHelpers.IsReferenceOrContainsReferences()) - { - throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); - } -#else - if (SpanHelpers.IsReferenceOrContainsReferences()) - { - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); - } -#endif + SpanHelpers.ValidateTypeIsBlittable(); + if (Unsafe.SizeOf() > (uint)buffer.Length) { return false; diff --git a/src/System.Memory/src/System/ReadOnlySpan.cs b/src/System.Memory/src/System/ReadOnlySpan.cs index 8b5fbb320e86..5af137ce0488 100644 --- a/src/System.Memory/src/System/ReadOnlySpan.cs +++ b/src/System.Memory/src/System/ReadOnlySpan.cs @@ -80,8 +80,8 @@ public ReadOnlySpan(T[] array, int start, int length) [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe ReadOnlySpan(void* pointer, int length) { - if (SpanHelpers.IsReferenceOrContainsReferences()) - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + SpanHelpers.ValidateTypeIsBlittable(); + if (length < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); diff --git a/src/System.Memory/src/System/Span.cs b/src/System.Memory/src/System/Span.cs index eee3f040fcd1..f4badbbda097 100644 --- a/src/System.Memory/src/System/Span.cs +++ b/src/System.Memory/src/System/Span.cs @@ -84,8 +84,8 @@ public Span(T[] array, int start, int length) [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe Span(void* pointer, int length) { - if (SpanHelpers.IsReferenceOrContainsReferences()) - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + SpanHelpers.ValidateTypeIsBlittable(); + if (length < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); diff --git a/src/System.Memory/src/System/SpanExtensions.Portable.cs b/src/System.Memory/src/System/SpanExtensions.Portable.cs index 8651704e1bc1..f8a8c13d07ee 100644 --- a/src/System.Memory/src/System/SpanExtensions.Portable.cs +++ b/src/System.Memory/src/System/SpanExtensions.Portable.cs @@ -27,8 +27,7 @@ public static partial class SpanExtensions public static Span AsBytes(this Span source) where T : struct { - if (SpanHelpers.IsReferenceOrContainsReferences()) - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + SpanHelpers.ValidateTypeIsBlittable(); int newLength = checked(source.Length * Unsafe.SizeOf()); return new Span(Unsafe.As>(source.Pinnable), source.ByteOffset, newLength); @@ -49,8 +48,7 @@ public static Span AsBytes(this Span source) public static ReadOnlySpan AsBytes(this ReadOnlySpan source) where T : struct { - if (SpanHelpers.IsReferenceOrContainsReferences()) - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + SpanHelpers.ValidateTypeIsBlittable(); int newLength = checked(source.Length * Unsafe.SizeOf()); return new ReadOnlySpan(Unsafe.As>(source.Pinnable), source.ByteOffset, newLength); @@ -89,11 +87,8 @@ public static Span NonPortableCast(this Span source) where TFrom : struct where TTo : struct { - if (SpanHelpers.IsReferenceOrContainsReferences()) - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(TFrom)); - - if (SpanHelpers.IsReferenceOrContainsReferences()) - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(TTo)); + SpanHelpers.ValidateTypeIsBlittable(); + SpanHelpers.ValidateTypeIsBlittable(); int newLength = checked((int)((long)source.Length * Unsafe.SizeOf() / Unsafe.SizeOf())); return new Span(Unsafe.As>(source.Pinnable), source.ByteOffset, newLength); @@ -118,11 +113,8 @@ public static ReadOnlySpan NonPortableCast(this ReadOnlySpan()) - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(TFrom)); - - if (SpanHelpers.IsReferenceOrContainsReferences()) - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(TTo)); + SpanHelpers.ValidateTypeIsBlittable(); + SpanHelpers.ValidateTypeIsBlittable(); int newLength = checked((int)((long)source.Length * Unsafe.SizeOf() / Unsafe.SizeOf())); return new ReadOnlySpan(Unsafe.As>(source.Pinnable), source.ByteOffset, newLength); diff --git a/src/System.Memory/src/System/SpanHelpers.T.cs b/src/System.Memory/src/System/SpanHelpers.T.cs index b4d65c01e78e..eb4f16d33182 100644 --- a/src/System.Memory/src/System/SpanHelpers.T.cs +++ b/src/System.Memory/src/System/SpanHelpers.T.cs @@ -182,5 +182,20 @@ public static bool SequenceEqual(ref T first, ref T second, int length) NotEqual: // Workaround for https://github.com/dotnet/coreclr/issues/13549 return false; } + + public static void ValidateTypeIsBlittable() + { +#if FEATURE_FASTSPAN + if (RuntimeHelpers.IsReferenceOrContainsReferences()) + { + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); + } +#else + if (IsReferenceOrContainsReferences()) + { + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + } +#endif + } } } From 48a47b996d45be5241dbaeb45228f5cae575f1db Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Mon, 16 Oct 2017 19:35:52 -0700 Subject: [PATCH 2/5] Reverting SpanHelper change and renaming const to netstandard --- src/System.Memory/src/System.Memory.csproj | 2 +- .../src/System/Buffers/Binary/Reader.cs | 24 +++++++++++++++++-- .../src/System/Buffers/Binary/Writer.cs | 24 +++++++++++++++++-- src/System.Memory/src/System/ReadOnlySpan.cs | 3 ++- src/System.Memory/src/System/Span.cs | 3 ++- .../src/System/SpanExtensions.Portable.cs | 20 +++++++++++----- src/System.Memory/src/System/SpanHelpers.T.cs | 15 ------------ 7 files changed, 63 insertions(+), 28 deletions(-) diff --git a/src/System.Memory/src/System.Memory.csproj b/src/System.Memory/src/System.Memory.csproj index 88e9f01848c7..f0e48299b7f2 100644 --- a/src/System.Memory/src/System.Memory.csproj +++ b/src/System.Memory/src/System.Memory.csproj @@ -7,7 +7,7 @@ false $(OutputPath)$(MSBuildProjectName).xml true - $(DefineConstants);FEATURE_FASTSPAN + $(DefineConstants);netstandard $(DefineConstants);netcoreapp $(DefineConstants);netstandard11 diff --git a/src/System.Memory/src/System/Buffers/Binary/Reader.cs b/src/System.Memory/src/System/Buffers/Binary/Reader.cs index fc98a09d77ff..64eb5c3e513c 100644 --- a/src/System.Memory/src/System/Buffers/Binary/Reader.cs +++ b/src/System.Memory/src/System/Buffers/Binary/Reader.cs @@ -98,7 +98,17 @@ public static ulong ReverseEndianness(ulong value) public static T ReadMachineEndian(ReadOnlySpan buffer) where T : struct { - SpanHelpers.ValidateTypeIsBlittable(); +#if netstandard + if (RuntimeHelpers.IsReferenceOrContainsReferences()) + { + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); + } +#else + if (SpanHelpers.IsReferenceOrContainsReferences()) + { + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + } +#endif if (Unsafe.SizeOf() > buffer.Length) { @@ -115,7 +125,17 @@ public static T ReadMachineEndian(ReadOnlySpan buffer) public static bool TryReadMachineEndian(ReadOnlySpan buffer, out T value) where T : struct { - SpanHelpers.ValidateTypeIsBlittable(); +#if netstandard + if (RuntimeHelpers.IsReferenceOrContainsReferences()) + { + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); + } +#else + if (SpanHelpers.IsReferenceOrContainsReferences()) + { + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + } +#endif if (Unsafe.SizeOf() > (uint)buffer.Length) { diff --git a/src/System.Memory/src/System/Buffers/Binary/Writer.cs b/src/System.Memory/src/System/Buffers/Binary/Writer.cs index 31bc930cd9ef..a08e85367698 100644 --- a/src/System.Memory/src/System/Buffers/Binary/Writer.cs +++ b/src/System.Memory/src/System/Buffers/Binary/Writer.cs @@ -16,7 +16,17 @@ public static partial class BinaryPrimitives public static void WriteMachineEndian(Span buffer, ref T value) where T : struct { - SpanHelpers.ValidateTypeIsBlittable(); +#if netstandard + if (RuntimeHelpers.IsReferenceOrContainsReferences()) + { + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); + } +#else + if (SpanHelpers.IsReferenceOrContainsReferences()) + { + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + } +#endif if ((uint)Unsafe.SizeOf() > (uint)buffer.Length) { @@ -33,7 +43,17 @@ public static void WriteMachineEndian(Span buffer, ref T value) public static bool TryWriteMachineEndian(Span buffer, ref T value) where T : struct { - SpanHelpers.ValidateTypeIsBlittable(); +#if netstandard + if (RuntimeHelpers.IsReferenceOrContainsReferences()) + { + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); + } +#else + if (SpanHelpers.IsReferenceOrContainsReferences()) + { + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + } +#endif if (Unsafe.SizeOf() > (uint)buffer.Length) { diff --git a/src/System.Memory/src/System/ReadOnlySpan.cs b/src/System.Memory/src/System/ReadOnlySpan.cs index 5af137ce0488..0e27d3f65a03 100644 --- a/src/System.Memory/src/System/ReadOnlySpan.cs +++ b/src/System.Memory/src/System/ReadOnlySpan.cs @@ -80,7 +80,8 @@ public ReadOnlySpan(T[] array, int start, int length) [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe ReadOnlySpan(void* pointer, int length) { - SpanHelpers.ValidateTypeIsBlittable(); + if (SpanHelpers.IsReferenceOrContainsReferences()) + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); if (length < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); diff --git a/src/System.Memory/src/System/Span.cs b/src/System.Memory/src/System/Span.cs index f4badbbda097..d494fba00057 100644 --- a/src/System.Memory/src/System/Span.cs +++ b/src/System.Memory/src/System/Span.cs @@ -84,7 +84,8 @@ public Span(T[] array, int start, int length) [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe Span(void* pointer, int length) { - SpanHelpers.ValidateTypeIsBlittable(); + if (SpanHelpers.IsReferenceOrContainsReferences()) + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); if (length < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); diff --git a/src/System.Memory/src/System/SpanExtensions.Portable.cs b/src/System.Memory/src/System/SpanExtensions.Portable.cs index f8a8c13d07ee..8651704e1bc1 100644 --- a/src/System.Memory/src/System/SpanExtensions.Portable.cs +++ b/src/System.Memory/src/System/SpanExtensions.Portable.cs @@ -27,7 +27,8 @@ public static partial class SpanExtensions public static Span AsBytes(this Span source) where T : struct { - SpanHelpers.ValidateTypeIsBlittable(); + if (SpanHelpers.IsReferenceOrContainsReferences()) + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); int newLength = checked(source.Length * Unsafe.SizeOf()); return new Span(Unsafe.As>(source.Pinnable), source.ByteOffset, newLength); @@ -48,7 +49,8 @@ public static Span AsBytes(this Span source) public static ReadOnlySpan AsBytes(this ReadOnlySpan source) where T : struct { - SpanHelpers.ValidateTypeIsBlittable(); + if (SpanHelpers.IsReferenceOrContainsReferences()) + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); int newLength = checked(source.Length * Unsafe.SizeOf()); return new ReadOnlySpan(Unsafe.As>(source.Pinnable), source.ByteOffset, newLength); @@ -87,8 +89,11 @@ public static Span NonPortableCast(this Span source) where TFrom : struct where TTo : struct { - SpanHelpers.ValidateTypeIsBlittable(); - SpanHelpers.ValidateTypeIsBlittable(); + if (SpanHelpers.IsReferenceOrContainsReferences()) + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(TFrom)); + + if (SpanHelpers.IsReferenceOrContainsReferences()) + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(TTo)); int newLength = checked((int)((long)source.Length * Unsafe.SizeOf() / Unsafe.SizeOf())); return new Span(Unsafe.As>(source.Pinnable), source.ByteOffset, newLength); @@ -113,8 +118,11 @@ public static ReadOnlySpan NonPortableCast(this ReadOnlySpan(); - SpanHelpers.ValidateTypeIsBlittable(); + if (SpanHelpers.IsReferenceOrContainsReferences()) + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(TFrom)); + + if (SpanHelpers.IsReferenceOrContainsReferences()) + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(TTo)); int newLength = checked((int)((long)source.Length * Unsafe.SizeOf() / Unsafe.SizeOf())); return new ReadOnlySpan(Unsafe.As>(source.Pinnable), source.ByteOffset, newLength); diff --git a/src/System.Memory/src/System/SpanHelpers.T.cs b/src/System.Memory/src/System/SpanHelpers.T.cs index eb4f16d33182..b4d65c01e78e 100644 --- a/src/System.Memory/src/System/SpanHelpers.T.cs +++ b/src/System.Memory/src/System/SpanHelpers.T.cs @@ -182,20 +182,5 @@ public static bool SequenceEqual(ref T first, ref T second, int length) NotEqual: // Workaround for https://github.com/dotnet/coreclr/issues/13549 return false; } - - public static void ValidateTypeIsBlittable() - { -#if FEATURE_FASTSPAN - if (RuntimeHelpers.IsReferenceOrContainsReferences()) - { - throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); - } -#else - if (IsReferenceOrContainsReferences()) - { - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); - } -#endif - } } } From cc902de9612a197be6789a83e24ef7a112e88142 Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Mon, 16 Oct 2017 19:37:00 -0700 Subject: [PATCH 3/5] Removing extra spaces. --- src/System.Memory/src/System/ReadOnlySpan.cs | 1 - src/System.Memory/src/System/Span.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/src/System.Memory/src/System/ReadOnlySpan.cs b/src/System.Memory/src/System/ReadOnlySpan.cs index 0e27d3f65a03..8b5fbb320e86 100644 --- a/src/System.Memory/src/System/ReadOnlySpan.cs +++ b/src/System.Memory/src/System/ReadOnlySpan.cs @@ -82,7 +82,6 @@ public unsafe ReadOnlySpan(void* pointer, int length) { if (SpanHelpers.IsReferenceOrContainsReferences()) ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); - if (length < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); diff --git a/src/System.Memory/src/System/Span.cs b/src/System.Memory/src/System/Span.cs index d494fba00057..eee3f040fcd1 100644 --- a/src/System.Memory/src/System/Span.cs +++ b/src/System.Memory/src/System/Span.cs @@ -86,7 +86,6 @@ public unsafe Span(void* pointer, int length) { if (SpanHelpers.IsReferenceOrContainsReferences()) ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); - if (length < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); From f8aa20b8c272de9122e1a33c10d883433911bf13 Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Mon, 16 Oct 2017 19:37:31 -0700 Subject: [PATCH 4/5] Remove unnecessary new lines --- src/System.Memory/src/System/Buffers/Binary/Reader.cs | 2 -- src/System.Memory/src/System/Buffers/Binary/Writer.cs | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/System.Memory/src/System/Buffers/Binary/Reader.cs b/src/System.Memory/src/System/Buffers/Binary/Reader.cs index 64eb5c3e513c..caa435c9ebd5 100644 --- a/src/System.Memory/src/System/Buffers/Binary/Reader.cs +++ b/src/System.Memory/src/System/Buffers/Binary/Reader.cs @@ -109,7 +109,6 @@ public static T ReadMachineEndian(ReadOnlySpan buffer) ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #endif - if (Unsafe.SizeOf() > buffer.Length) { throw new ArgumentOutOfRangeException(); @@ -136,7 +135,6 @@ public static bool TryReadMachineEndian(ReadOnlySpan buffer, out T valu ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #endif - if (Unsafe.SizeOf() > (uint)buffer.Length) { value = default; diff --git a/src/System.Memory/src/System/Buffers/Binary/Writer.cs b/src/System.Memory/src/System/Buffers/Binary/Writer.cs index a08e85367698..17fb09958f89 100644 --- a/src/System.Memory/src/System/Buffers/Binary/Writer.cs +++ b/src/System.Memory/src/System/Buffers/Binary/Writer.cs @@ -27,7 +27,6 @@ public static void WriteMachineEndian(Span buffer, ref T value) ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #endif - if ((uint)Unsafe.SizeOf() > (uint)buffer.Length) { throw new ArgumentOutOfRangeException(); @@ -54,7 +53,6 @@ public static bool TryWriteMachineEndian(Span buffer, ref T value) ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #endif - if (Unsafe.SizeOf() > (uint)buffer.Length) { return false; From c58dbf3e8c51e9b56e21a4555cd929f7c1f1a31e Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Mon, 16 Oct 2017 21:16:42 -0700 Subject: [PATCH 5/5] Addressing PR feedback. --- .../src/System/Buffers/Binary/Reader.cs | 16 ++++++++-------- .../src/System/Buffers/Binary/Writer.cs | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/System.Memory/src/System/Buffers/Binary/Reader.cs b/src/System.Memory/src/System/Buffers/Binary/Reader.cs index caa435c9ebd5..9c7982aad95c 100644 --- a/src/System.Memory/src/System/Buffers/Binary/Reader.cs +++ b/src/System.Memory/src/System/Buffers/Binary/Reader.cs @@ -99,14 +99,14 @@ public static T ReadMachineEndian(ReadOnlySpan buffer) where T : struct { #if netstandard - if (RuntimeHelpers.IsReferenceOrContainsReferences()) + if (SpanHelpers.IsReferenceOrContainsReferences()) { - throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #else - if (SpanHelpers.IsReferenceOrContainsReferences()) + if (RuntimeHelpers.IsReferenceOrContainsReferences()) { - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); } #endif if (Unsafe.SizeOf() > buffer.Length) @@ -125,14 +125,14 @@ public static bool TryReadMachineEndian(ReadOnlySpan buffer, out T valu where T : struct { #if netstandard - if (RuntimeHelpers.IsReferenceOrContainsReferences()) + if (SpanHelpers.IsReferenceOrContainsReferences()) { - throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #else - if (SpanHelpers.IsReferenceOrContainsReferences()) + if (RuntimeHelpers.IsReferenceOrContainsReferences()) { - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); } #endif if (Unsafe.SizeOf() > (uint)buffer.Length) diff --git a/src/System.Memory/src/System/Buffers/Binary/Writer.cs b/src/System.Memory/src/System/Buffers/Binary/Writer.cs index 17fb09958f89..9834986f4a69 100644 --- a/src/System.Memory/src/System/Buffers/Binary/Writer.cs +++ b/src/System.Memory/src/System/Buffers/Binary/Writer.cs @@ -17,14 +17,14 @@ public static void WriteMachineEndian(Span buffer, ref T value) where T : struct { #if netstandard - if (RuntimeHelpers.IsReferenceOrContainsReferences()) + if (SpanHelpers.IsReferenceOrContainsReferences()) { - throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #else - if (SpanHelpers.IsReferenceOrContainsReferences()) + if (RuntimeHelpers.IsReferenceOrContainsReferences()) { - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); } #endif if ((uint)Unsafe.SizeOf() > (uint)buffer.Length) @@ -43,14 +43,14 @@ public static bool TryWriteMachineEndian(Span buffer, ref T value) where T : struct { #if netstandard - if (RuntimeHelpers.IsReferenceOrContainsReferences()) + if (SpanHelpers.IsReferenceOrContainsReferences()) { - throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #else - if (SpanHelpers.IsReferenceOrContainsReferences()) + if (RuntimeHelpers.IsReferenceOrContainsReferences()) { - ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); } #endif if (Unsafe.SizeOf() > (uint)buffer.Length)