Use "in" instead of "ref" for read-only SpanHelper methods

This commit is contained in:
Alex Barney 2020-07-16 23:39:53 -07:00
parent 717e54fe4e
commit 672a0016b3
7 changed files with 35 additions and 18 deletions

View file

@ -34,7 +34,7 @@ namespace LibHac.Common
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator ReadOnlySpan<byte>(in Buffer16 value) public static implicit operator ReadOnlySpan<byte>(in Buffer16 value)
{ {
return SpanHelpers.AsReadOnlyByteSpan(ref Unsafe.AsRef(in value)); return SpanHelpers.AsReadOnlyByteSpan(in value);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -57,7 +57,7 @@ namespace LibHac.Common
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ReadOnlySpan<T> AsReadOnlySpan<T>() where T : unmanaged public readonly ReadOnlySpan<T> AsReadOnlySpan<T>() where T : unmanaged
{ {
return SpanHelpers.AsReadOnlySpan<Buffer16, T>(ref Unsafe.AsRef(in this)); return SpanHelpers.AsReadOnlySpan<Buffer16, T>(in this);
} }
public override string ToString() public override string ToString()
@ -93,7 +93,7 @@ namespace LibHac.Common
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator ReadOnlySpan<byte>(in Buffer32 value) public static implicit operator ReadOnlySpan<byte>(in Buffer32 value)
{ {
return SpanHelpers.AsReadOnlyByteSpan(ref Unsafe.AsRef(in value)); return SpanHelpers.AsReadOnlyByteSpan(in value);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -116,7 +116,7 @@ namespace LibHac.Common
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ReadOnlySpan<T> AsReadOnlySpan<T>() where T : unmanaged public readonly ReadOnlySpan<T> AsReadOnlySpan<T>() where T : unmanaged
{ {
return SpanHelpers.AsReadOnlySpan<Buffer32, T>(ref Unsafe.AsRef(in this)); return SpanHelpers.AsReadOnlySpan<Buffer32, T>(in this);
} }
public override string ToString() public override string ToString()

View file

@ -33,29 +33,29 @@ namespace LibHac.Common
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<T> CreateReadOnlySpan<T>(ref T reference, int length) public static ReadOnlySpan<T> CreateReadOnlySpan<T>(in T reference, int length)
{ {
return MemoryMarshal.CreateReadOnlySpan(ref reference, length); return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in reference), length);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<T> AsReadOnlySpan<T>(ref T reference) where T : unmanaged public static ReadOnlySpan<T> AsReadOnlySpan<T>(in T reference) where T : unmanaged
{ {
return CreateReadOnlySpan(ref reference, 1); return CreateReadOnlySpan(in reference, 1);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<TSpan> AsReadOnlySpan<TStruct, TSpan>(ref TStruct reference) public static ReadOnlySpan<TSpan> AsReadOnlySpan<TStruct, TSpan>(in TStruct reference)
where TStruct : unmanaged where TSpan : unmanaged where TStruct : unmanaged where TSpan : unmanaged
{ {
return CreateReadOnlySpan(ref Unsafe.As<TStruct, TSpan>(ref reference), return CreateReadOnlySpan(in Unsafe.As<TStruct, TSpan>(ref Unsafe.AsRef(in reference)),
Unsafe.SizeOf<TStruct>() / Unsafe.SizeOf<TSpan>()); Unsafe.SizeOf<TStruct>() / Unsafe.SizeOf<TSpan>());
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<byte> AsReadOnlyByteSpan<T>(ref T reference) where T : unmanaged public static ReadOnlySpan<byte> AsReadOnlyByteSpan<T>(in T reference) where T : unmanaged
{ {
return CreateReadOnlySpan(ref Unsafe.As<T, byte>(ref reference), Unsafe.SizeOf<T>()); return CreateReadOnlySpan(in Unsafe.As<T, byte>(ref Unsafe.AsRef(in reference)), Unsafe.SizeOf<T>());
} }
// All AsStruct methods do bounds checks on the input // All AsStruct methods do bounds checks on the input

View file

@ -1,4 +1,5 @@
using System.Diagnostics; using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace LibHac.Diag namespace LibHac.Diag
@ -42,5 +43,21 @@ namespace LibHac.Diag
throw new LibHacException($"Value {value} is not in the range {lowerInclusive} to {upperExclusive}"); throw new LibHacException($"Value {value} is not in the range {lowerInclusive} to {upperExclusive}");
} }
} }
public static void Equal<T>(T value1, T value2) where T : IEquatable<T>
{
if (!value1.Equals(value2))
{
throw new LibHacException($"Values were not equal: {value1}, {value2}");
}
}
public static void NotEqual<T>(T value1, T value2) where T : IEquatable<T>
{
if (value1.Equals(value2))
{
throw new LibHacException($"Values should not be equal: {value1}, {value2}");
}
}
} }
} }

View file

@ -33,7 +33,7 @@ namespace LibHac.FsSystem
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator U8Span(in FsPath value) => new U8Span(SpanHelpers.AsReadOnlyByteSpan(ref Unsafe.AsRef(in value))); public static implicit operator U8Span(in FsPath value) => new U8Span(SpanHelpers.AsReadOnlyByteSpan(in value));
public override string ToString() => StringUtils.Utf8ZToString(Str); public override string ToString() => StringUtils.Utf8ZToString(Str);
} }

View file

@ -76,7 +76,7 @@ namespace LibHac.Sm
int nameLen; int nameLen;
for (nameLen = 1; nameLen < Unsafe.SizeOf<ServiceName>(); nameLen++) for (nameLen = 1; nameLen < Unsafe.SizeOf<ServiceName>(); nameLen++)
{ {
if (SpanHelpers.AsReadOnlyByteSpan(ref name)[nameLen] == 0) if (SpanHelpers.AsReadOnlyByteSpan(in name)[nameLen] == 0)
{ {
break; break;
} }
@ -85,7 +85,7 @@ namespace LibHac.Sm
// Names must be all-zero after they end. // Names must be all-zero after they end.
for (; nameLen < Unsafe.SizeOf<ServiceName>(); nameLen++) for (; nameLen < Unsafe.SizeOf<ServiceName>(); nameLen++)
{ {
if (SpanHelpers.AsReadOnlyByteSpan(ref name)[nameLen] != 0) if (SpanHelpers.AsReadOnlyByteSpan(in name)[nameLen] != 0)
{ {
return ResultSm.InvalidServiceName.Log(); return ResultSm.InvalidServiceName.Log();
} }

View file

@ -37,7 +37,7 @@ namespace LibHac.Sm
public override string ToString() public override string ToString()
{ {
ulong name = Name; ulong name = Name;
return StringUtils.Utf8ZToString(SpanHelpers.AsReadOnlyByteSpan(ref name)); return StringUtils.Utf8ZToString(SpanHelpers.AsReadOnlyByteSpan(in name));
} }
} }
} }

View file

@ -60,7 +60,7 @@ namespace hactoolnet
AesXtsFileHeader header = xtsFile.Header; AesXtsFileHeader header = xtsFile.Header;
uint magic = header.Magic; uint magic = header.Magic;
PrintItem(sb, colLen, " Magic:", Utilities.GetUtf8String(SpanHelpers.AsReadOnlyByteSpan(ref magic))); PrintItem(sb, colLen, " Magic:", Utilities.GetUtf8String(SpanHelpers.AsReadOnlyByteSpan(in magic)));
PrintItem(sb, colLen, " Content Type:", GetContentType(contentType)); PrintItem(sb, colLen, " Content Type:", GetContentType(contentType));
PrintItem(sb, colLen, " Content Size:", $"{header.Size:x12}"); PrintItem(sb, colLen, " Content Size:", $"{header.Size:x12}");
PrintItem(sb, colLen, " Header HMAC:", header.Signature); PrintItem(sb, colLen, " Header HMAC:", header.Signature);