Skip to content

Commit

Permalink
Fix span usages
Browse files Browse the repository at this point in the history
  • Loading branch information
afxres committed Jun 23, 2024
1 parent dae1b39 commit 5c72a03
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void Encode(ref byte target, T item)
Debug.Assert(Unsafe.SizeOf<T>() > Unsafe.SizeOf<E>());
Debug.Assert(Unsafe.SizeOf<T>() % Unsafe.SizeOf<E>() is 0);
var length = Unsafe.SizeOf<T>() / Unsafe.SizeOf<E>();
var source = MemoryMarshal.CreateSpan(ref Unsafe.As<T, E>(ref item), length);
var source = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<T, E>(ref item), length);
for (var i = 0; i < source.Length; i++)
LittleEndianFallback.Encode(ref Unsafe.Add(ref target, i * Unsafe.SizeOf<E>()), source[i]);
return;
Expand Down
2 changes: 1 addition & 1 deletion code/Binary/Creators/VariableBoundArrayConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte, E>(ref MemoryMarshal.GetArrayDataReference(origin)), origin.Length);
var source = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<byte, E>(ref MemoryMarshal.GetArrayDataReference(origin)), origin.Length);
var converter = this.converter;
foreach (var i in source)
converter.EncodeAuto(ref allocator, i);
Expand Down
4 changes: 2 additions & 2 deletions code/Binary/Features.Fallback/LittleEndianFallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static void Encode128<T>(ref byte target, T item) where T : unmanaged
internal static T Decode<T>(ref byte source) where T : unmanaged
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static T MakeCast<E>(E data) => Unsafe.As<E, T>(ref data);
static T MakeCast<E>(E data) => Unsafe.ReadUnaligned<T>(ref Unsafe.As<E, byte>(ref data));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static ReadOnlySpan<byte> MakeSpan(ref byte location) => MemoryMarshal.CreateReadOnlySpan(ref location, Unsafe.SizeOf<T>());
Expand All @@ -58,7 +58,7 @@ internal static T Decode<T>(ref byte source) where T : unmanaged
internal static void Encode<T>(ref byte target, T item) where T : unmanaged
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static E MakeCast<E>(T data) => Unsafe.As<T, E>(ref data);
static E MakeCast<E>(T data) => Unsafe.ReadUnaligned<E>(ref Unsafe.As<T, byte>(ref data));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static Span<byte> MakeSpan(ref byte location) => MemoryMarshal.CreateSpan(ref location, Unsafe.SizeOf<T>());
Expand Down

0 comments on commit 5c72a03

Please sign in to comment.