This repository was archived by the owner on Jan 23, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +29
-3
lines changed
src/mscorlib/shared/System Expand file tree Collapse file tree 4 files changed +29
-3
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ public readonly struct ReadOnlyMemory<T>
2929 private readonly int _index ;
3030 private readonly int _length ;
3131
32- private const int RemoveOwnedFlagBitMask = 0x7FFFFFFF ;
32+ internal const int RemoveOwnedFlagBitMask = 0x7FFFFFFF ;
3333
3434 /// <summary>
3535 /// Creates a new memory over the entirety of the target array.
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ namespace System
2121 public readonly ref struct ReadOnlySpan < T >
2222 {
2323 /// <summary>A byref or a native ptr.</summary>
24- private readonly ByReference < T > _pointer ;
24+ internal readonly ByReference < T > _pointer ;
2525 /// <summary>The number of elements this ReadOnlySpan contains.</summary>
2626#if PROJECTN
2727 [ Bound ]
Original file line number Diff line number Diff line change 22// The .NET Foundation licenses this file to you under the MIT license.
33// See the LICENSE file in the project root for more information.
44
5+ using System . Buffers ;
56using System . Runtime . CompilerServices ;
67
78namespace System . Runtime . InteropServices
@@ -23,5 +24,30 @@ public static class MemoryMarshal
2324 /// </remarks>
2425 public static Memory < T > AsMemory < T > ( ReadOnlyMemory < T > readOnlyMemory ) =>
2526 Unsafe . As < ReadOnlyMemory < T > , Memory < T > > ( ref readOnlyMemory ) ;
27+
28+ public static ref T GetReference < T > ( Span < T > span ) => ref span . _pointer . Value ;
29+
30+ public static ref readonly T GetReference < T > ( ReadOnlySpan < T > span ) => ref span . _pointer . Value ;
31+
32+ public static bool TryGetArray < T > ( ReadOnlyMemory < T > readOnlyMemory , out ArraySegment < T > arraySegment )
33+ {
34+ object obj = readOnlyMemory . GetObjectStartLength ( out int index , out int length ) ;
35+ if ( index < 0 )
36+ {
37+ if ( ( ( OwnedMemory < T > ) obj ) . TryGetArray ( out var segment ) )
38+ {
39+ arraySegment = new ArraySegment < T > ( segment . Array , segment . Offset + ( index & ReadOnlyMemory < T > . RemoveOwnedFlagBitMask ) , length ) ;
40+ return true ;
41+ }
42+ }
43+ else if ( obj is T [ ] arr )
44+ {
45+ arraySegment = new ArraySegment < T > ( arr , index , length ) ;
46+ return true ;
47+ }
48+
49+ arraySegment = default ;
50+ return false ;
51+ }
2652 }
2753}
Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ namespace System
2727 public readonly ref struct Span < T >
2828 {
2929 /// <summary>A byref or a native ptr.</summary>
30- private readonly ByReference < T > _pointer ;
30+ internal readonly ByReference < T > _pointer ;
3131 /// <summary>The number of elements this Span contains.</summary>
3232#if PROJECTN
3333 [ Bound ]
You can’t perform that action at this time.
0 commit comments