@@ -20,7 +20,7 @@ public readonly struct Memory<T>
2020
2121 // The highest order bit of _index is used to discern whether _object is an array/string or an owned memory
2222 // if (_index >> 31) == 1, object _object is an OwnedMemory<T>
23- // else, object _object is a T[] or a string. It can only be a string if the Memory<T> was created by
23+ // else, object _object is a T[] or a string. It can only be a string if the Memory<T> was created by
2424 // using unsafe / marshaling code to reinterpret a ReadOnlyMemory<char> wrapped around a string as
2525 // a Memory<T>.
2626 private readonly object _object ;
@@ -122,7 +122,7 @@ public static implicit operator ReadOnlyMemory<T>(Memory<T> memory) =>
122122 /// <summary>
123123 /// Returns an empty <see cref="Memory{T}"/>
124124 /// </summary>
125- public static Memory < T > Empty { get ; } = Array . Empty < T > ( ) ;
125+ public static Memory < T > Empty => default ;
126126
127127 /// <summary>
128128 /// The number of items in the memory.
@@ -187,15 +187,19 @@ public Span<T> Span
187187 {
188188 // This is dangerous, returning a writable span for a string that should be immutable.
189189 // However, we need to handle the case where a ReadOnlyMemory<char> was created from a string
190- // and then cast to a Memory<T>. Such a cast can only be done with unsafe or marshaling code,
190+ // and then cast to a Memory<T>. Such a cast can only be done with unsafe or marshaling code,
191191 // in which case that's the dangerous operation performed by the dev, and we're just following
192192 // suit here to make it work as best as possible.
193193 return new Span < T > ( ref Unsafe . As < char , T > ( ref s . GetRawStringData ( ) ) , s . Length ) . Slice ( _index , _length ) ;
194194 }
195- else
195+ else if ( _object != null )
196196 {
197197 return new Span < T > ( ( T [ ] ) _object , _index , _length ) ;
198198 }
199+ else
200+ {
201+ return default ;
202+ }
199203 }
200204 }
201205
@@ -224,7 +228,7 @@ public Span<T> Span
224228
225229 public unsafe MemoryHandle Retain ( bool pin = false )
226230 {
227- MemoryHandle memoryHandle ;
231+ MemoryHandle memoryHandle = default ;
228232 if ( pin )
229233 {
230234 if ( _index < 0 )
@@ -243,9 +247,8 @@ public unsafe MemoryHandle Retain(bool pin = false)
243247 void * pointer = Unsafe . Add < T > ( Unsafe . AsPointer ( ref s . GetRawStringData ( ) ) , _index ) ;
244248 memoryHandle = new MemoryHandle ( null , pointer , handle ) ;
245249 }
246- else
250+ else if ( _object is T [ ] array )
247251 {
248- var array = ( T [ ] ) _object ;
249252 var handle = GCHandle . Alloc ( array , GCHandleType . Pinned ) ;
250253 void * pointer = Unsafe . Add < T > ( Unsafe . AsPointer ( ref array . GetRawSzArrayData ( ) ) , _index ) ;
251254 memoryHandle = new MemoryHandle ( null , pointer , handle ) ;
@@ -258,10 +261,6 @@ public unsafe MemoryHandle Retain(bool pin = false)
258261 ( ( OwnedMemory < T > ) _object ) . Retain ( ) ;
259262 memoryHandle = new MemoryHandle ( ( OwnedMemory < T > ) _object ) ;
260263 }
261- else
262- {
263- memoryHandle = new MemoryHandle ( null ) ;
264- }
265264 }
266265 return memoryHandle ;
267266 }
@@ -280,14 +279,10 @@ public bool TryGetArray(out ArraySegment<T> arraySegment)
280279 return true ;
281280 }
282281 }
283- else
282+ else if ( _object is T [ ] arr )
284283 {
285- T [ ] arr = _object as T [ ] ;
286- if ( typeof ( T ) != typeof ( char ) || arr != null )
287- {
288- arraySegment = new ArraySegment < T > ( arr , _index , _length ) ;
289- return true ;
290- }
284+ arraySegment = new ArraySegment < T > ( arr , _index , _length ) ;
285+ return true ;
291286 }
292287
293288 arraySegment = default ( ArraySegment < T > ) ;
@@ -333,7 +328,7 @@ public bool Equals(Memory<T> other)
333328 [ EditorBrowsable ( EditorBrowsableState . Never ) ]
334329 public override int GetHashCode ( )
335330 {
336- return CombineHashCodes ( _object . GetHashCode ( ) , _index . GetHashCode ( ) , _length . GetHashCode ( ) ) ;
331+ return _object != null ? CombineHashCodes ( _object . GetHashCode ( ) , _index . GetHashCode ( ) , _length . GetHashCode ( ) ) : 0 ;
337332 }
338333
339334 private static int CombineHashCodes ( int left , int right )
0 commit comments