From 5c72a03ed70c02eaad0f73de47f35c3ae75e247f Mon Sep 17 00:00:00 2001 From: miko Date: Sun, 23 Jun 2024 22:08:12 +0800 Subject: [PATCH] Fix span usages --- .../Binary/Creators.Endianness/RepeatLittleEndianConverter.cs | 2 +- code/Binary/Creators/VariableBoundArrayConverter.cs | 2 +- code/Binary/Features.Fallback/LittleEndianFallback.cs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/code/Binary/Creators.Endianness/RepeatLittleEndianConverter.cs b/code/Binary/Creators.Endianness/RepeatLittleEndianConverter.cs index d7664d23..fc589cde 100644 --- a/code/Binary/Creators.Endianness/RepeatLittleEndianConverter.cs +++ b/code/Binary/Creators.Endianness/RepeatLittleEndianConverter.cs @@ -29,7 +29,7 @@ public static void Encode(ref byte target, T item) Debug.Assert(Unsafe.SizeOf() > Unsafe.SizeOf()); Debug.Assert(Unsafe.SizeOf() % Unsafe.SizeOf() is 0); var length = Unsafe.SizeOf() / Unsafe.SizeOf(); - var source = MemoryMarshal.CreateSpan(ref Unsafe.As(ref item), length); + var source = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As(ref item), length); for (var i = 0; i < source.Length; i++) LittleEndianFallback.Encode(ref Unsafe.Add(ref target, i * Unsafe.SizeOf()), source[i]); return; diff --git a/code/Binary/Creators/VariableBoundArrayConverter.cs b/code/Binary/Creators/VariableBoundArrayConverter.cs index 37e6e8fe..ab11528f 100644 --- a/code/Binary/Creators/VariableBoundArrayConverter.cs +++ b/code/Binary/Creators/VariableBoundArrayConverter.cs @@ -38,7 +38,7 @@ public override void Encode(ref Allocator allocator, T? item) Converter.Encode(ref allocator, lengthList[i] = origin.GetLength(i)); for (var i = 0; i < rank; i++) Converter.Encode(ref allocator, startsList[i] = origin.GetLowerBound(i)); - var source = MemoryMarshal.CreateSpan(ref Unsafe.As(ref MemoryMarshal.GetArrayDataReference(origin)), origin.Length); + var source = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As(ref MemoryMarshal.GetArrayDataReference(origin)), origin.Length); var converter = this.converter; foreach (var i in source) converter.EncodeAuto(ref allocator, i); diff --git a/code/Binary/Features.Fallback/LittleEndianFallback.cs b/code/Binary/Features.Fallback/LittleEndianFallback.cs index 3324ef5d..adec3fb0 100644 --- a/code/Binary/Features.Fallback/LittleEndianFallback.cs +++ b/code/Binary/Features.Fallback/LittleEndianFallback.cs @@ -38,7 +38,7 @@ private static void Encode128(ref byte target, T item) where T : unmanaged internal static T Decode(ref byte source) where T : unmanaged { [MethodImpl(MethodImplOptions.AggressiveInlining)] - static T MakeCast(E data) => Unsafe.As(ref data); + static T MakeCast(E data) => Unsafe.ReadUnaligned(ref Unsafe.As(ref data)); [MethodImpl(MethodImplOptions.AggressiveInlining)] static ReadOnlySpan MakeSpan(ref byte location) => MemoryMarshal.CreateReadOnlySpan(ref location, Unsafe.SizeOf()); @@ -58,7 +58,7 @@ internal static T Decode(ref byte source) where T : unmanaged internal static void Encode(ref byte target, T item) where T : unmanaged { [MethodImpl(MethodImplOptions.AggressiveInlining)] - static E MakeCast(T data) => Unsafe.As(ref data); + static E MakeCast(T data) => Unsafe.ReadUnaligned(ref Unsafe.As(ref data)); [MethodImpl(MethodImplOptions.AggressiveInlining)] static Span MakeSpan(ref byte location) => MemoryMarshal.CreateSpan(ref location, Unsafe.SizeOf());