Add GetPinnableReference back to Span and ReadOnlySpan (#17504)
authorAhson Khan <ahkha@microsoft.com>
Wed, 11 Apr 2018 13:43:10 +0000 (06:43 -0700)
committerJan Kotas <jkotas@microsoft.com>
Wed, 11 Apr 2018 13:43:10 +0000 (06:43 -0700)
src/mscorlib/shared/System/ReadOnlySpan.Fast.cs
src/mscorlib/shared/System/Span.Fast.cs

index 9bf3f21..38d62dc 100644 (file)
@@ -150,6 +150,13 @@ namespace System
         }
 
         /// <summary>
+        /// Returns a reference to the 0th element of the Span. If the Span is empty, returns null reference.
+        /// It can be used for pinning and is required to support the use of span within a fixed statement.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public unsafe ref readonly T GetPinnableReference() => ref (_length != 0) ? ref _pointer.Value : ref Unsafe.AsRef<T>(null);
+
+        /// <summary>
         /// Copies the contents of this read-only span into destination span. If the source
         /// and destinations overlap, this method behaves as if the original values in
         /// a temporary location before the destination is overwritten.
index 0ae1922..131694b 100644 (file)
@@ -155,6 +155,13 @@ namespace System
         }
 
         /// <summary>
+        /// Returns a reference to the 0th element of the Span. If the Span is empty, returns null reference.
+        /// It can be used for pinning and is required to support the use of span within a fixed statement.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public unsafe ref T GetPinnableReference() => ref (_length != 0) ? ref _pointer.Value : ref Unsafe.AsRef<T>(null);
+
+        /// <summary>
         /// Clears the contents of this span.
         /// </summary>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]