@@ -23,7 +23,7 @@ namespace System
23
23
public readonly ref struct ReadOnlySpan < T >
24
24
{
25
25
/// <summary>A byref or a native ptr.</summary>
26
- internal readonly ByReference < T > _pointer ;
26
+ internal readonly ByReference < T > _reference ;
27
27
/// <summary>The number of elements this ReadOnlySpan contains.</summary>
28
28
private readonly int _length ;
29
29
@@ -41,7 +41,7 @@ public ReadOnlySpan(T[]? array)
41
41
return ; // returns default
42
42
}
43
43
44
- _pointer = new ByReference < T > ( ref MemoryMarshal . GetArrayDataReference ( array ) ) ;
44
+ _reference = new ByReference < T > ( ref MemoryMarshal . GetArrayDataReference ( array ) ) ;
45
45
_length = array . Length ;
46
46
}
47
47
@@ -75,7 +75,7 @@ public ReadOnlySpan(T[]? array, int start, int length)
75
75
ThrowHelper . ThrowArgumentOutOfRangeException ( ) ;
76
76
#endif
77
77
78
- _pointer = new ByReference < T > ( ref Unsafe . Add ( ref MemoryMarshal . GetArrayDataReference ( array ) , ( nint ) ( uint ) start /* force zero-extension */ ) ) ;
78
+ _reference = new ByReference < T > ( ref Unsafe . Add ( ref MemoryMarshal . GetArrayDataReference ( array ) , ( nint ) ( uint ) start /* force zero-extension */ ) ) ;
79
79
_length = length ;
80
80
}
81
81
@@ -102,17 +102,27 @@ public unsafe ReadOnlySpan(void* pointer, int length)
102
102
if ( length < 0 )
103
103
ThrowHelper . ThrowArgumentOutOfRangeException ( ) ;
104
104
105
- _pointer = new ByReference < T > ( ref Unsafe . As < byte , T > ( ref * ( byte * ) pointer ) ) ;
105
+ _reference = new ByReference < T > ( ref Unsafe . As < byte , T > ( ref * ( byte * ) pointer ) ) ;
106
106
_length = length ;
107
107
}
108
108
109
+ // TODO https://github.com/dotnet/runtime/issues/67445: Make this public.
110
+ /// <summary>Creates a new <see cref="ReadOnlySpan{T}"/> of length 1 around the specified reference.</summary>
111
+ /// <param name="reference">A reference to data.</param>
112
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
113
+ internal ReadOnlySpan( in T reference )
114
+ {
115
+ _reference = new ByReference< T > ( ref Unsafe . AsRef ( in reference ) ) ;
116
+ _length = 1 ;
117
+ }
118
+
109
119
// Constructor for internal use only.
110
120
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
111
- internal ReadOnlySpan( ref T ptr , int length )
121
+ internal ReadOnlySpan( ref T reference , int length )
112
122
{
113
123
Debug. Assert ( length >= 0 ) ;
114
124
115
- _pointer = new ByReference < T > ( ref ptr ) ;
125
+ _reference = new ByReference< T > ( ref reference ) ;
116
126
_length = length ;
117
127
}
118
128
@@ -133,7 +143,7 @@ public ref readonly T this[int index]
133
143
{
134
144
if ( ( uint ) index >= ( uint ) _length)
135
145
ThrowHelper . ThrowIndexOutOfRangeException ( ) ;
136
- return ref Unsafe . Add ( ref _pointer . Value , ( nint ) ( uint ) index /* force zero-extension */ ) ;
146
+ return ref Unsafe . Add ( ref _reference . Value , ( nint ) ( uint ) index /* force zero-extension */ ) ;
137
147
}
138
148
}
139
149
@@ -251,7 +261,7 @@ public ref readonly T GetPinnableReference()
251
261
{
252
262
// Ensure that the native code has just one forward branch that is predicted-not-taken.
253
263
ref T ret = ref Unsafe. NullRef < T > ( ) ;
254
- if ( _length != 0 ) ret = ref _pointer . Value ;
264
+ if ( _length != 0 ) ret = ref _reference . Value ;
255
265
return ref ret;
256
266
}
257
267
@@ -274,7 +284,7 @@ public void CopyTo(Span<T> destination)
274
284
275
285
if ( ( uint ) _length <= ( uint ) destination . Length )
276
286
{
277
- Buffer. Memmove ( ref destination . _pointer . Value , ref _pointer . Value , ( uint ) _length ) ;
287
+ Buffer. Memmove ( ref destination . _reference . Value , ref _reference . Value , ( uint ) _length ) ;
278
288
}
279
289
else
280
290
{
@@ -295,7 +305,7 @@ public bool TryCopyTo(Span<T> destination)
295
305
bool retVal = false;
296
306
if ( ( uint ) _length <= ( uint ) destination . Length )
297
307
{
298
- Buffer. Memmove ( ref destination . _pointer . Value , ref _pointer . Value , ( uint ) _length ) ;
308
+ Buffer. Memmove ( ref destination . _reference . Value , ref _reference . Value , ( uint ) _length ) ;
299
309
retVal = true;
300
310
}
301
311
return retVal;
@@ -307,7 +317,7 @@ public bool TryCopyTo(Span<T> destination)
307
317
/// </summary>
308
318
public static bool operator == ( ReadOnlySpan < T > left , ReadOnlySpan < T > right ) =>
309
319
left . _length == right . _length &&
310
- Unsafe . AreSame < T > ( ref left . _pointer . Value , ref right . _pointer . Value ) ;
320
+ Unsafe . AreSame < T > ( ref left . _reference . Value , ref right . _reference . Value ) ;
311
321
312
322
/// <summary>
313
323
/// For <see cref="ReadOnlySpan{Char}"/>, returns a new instance of string that represents the characters pointed to by the span.
@@ -317,7 +327,7 @@ public override string ToString()
317
327
{
318
328
if ( typeof ( T ) == typeof ( char ) )
319
329
{
320
- return new string ( new ReadOnlySpan < char > ( ref Unsafe . As < T , char > ( ref _pointer . Value ) , _length ) ) ;
330
+ return new string ( new ReadOnlySpan < char > ( ref Unsafe . As < T , char > ( ref _reference . Value ) , _length ) ) ;
321
331
}
322
332
return $"System. ReadOnlySpan < { typeof ( T ) . Name } > [ { _length } ] ";
323
333
}
@@ -335,7 +345,7 @@ public ReadOnlySpan<T> Slice(int start)
335
345
if ( ( uint ) start > ( uint ) _length)
336
346
ThrowHelper . ThrowArgumentOutOfRangeException ( ) ;
337
347
338
- return new ReadOnlySpan < T > ( ref Unsafe . Add ( ref _pointer . Value , ( nint ) ( uint ) start /* force zero-extension */ ) , _length - start ) ;
348
+ return new ReadOnlySpan < T > ( ref Unsafe . Add ( ref _reference . Value , ( nint ) ( uint ) start /* force zero-extension */ ) , _length - start ) ;
339
349
}
340
350
341
351
/// <summary>
@@ -358,7 +368,7 @@ public ReadOnlySpan<T> Slice(int start, int length)
358
368
ThrowHelper. ThrowArgumentOutOfRangeException( ) ;
359
369
#endif
360
370
361
- return new ReadOnlySpan< T> ( ref Unsafe. Add( ref _pointer . Value, ( nint ) ( uint ) start /* force zero-extension */ ) , length) ;
371
+ return new ReadOnlySpan< T> ( ref Unsafe. Add( ref _reference . Value, ( nint ) ( uint ) start /* force zero-extension */ ) , length) ;
362
372
}
363
373
364
374
/// <summary>
@@ -372,7 +382,7 @@ public T[] ToArray()
372
382
return Array. Empty< T> ( ) ;
373
383
374
384
var destination = new T[ _length] ;
375
- Buffer. Memmove( ref MemoryMarshal. GetArrayDataReference( destination) , ref _pointer . Value, ( uint ) _length) ;
385
+ Buffer. Memmove( ref MemoryMarshal. GetArrayDataReference( destination) , ref _reference . Value, ( uint ) _length) ;
376
386
return destination;
377
387
}
378
388
}
0 commit comments