From 672a0016b38cadcc380b89877ad958ad3c31adb3 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Thu, 16 Jul 2020 23:39:53 -0700 Subject: [PATCH] Use "in" instead of "ref" for read-only SpanHelper methods --- src/LibHac/Common/Buffer.cs | 8 ++++---- src/LibHac/Common/SpanHelpers.cs | 16 ++++++++-------- src/LibHac/Diag/Assert.cs | 19 ++++++++++++++++++- src/LibHac/FsSystem/FsPath.cs | 2 +- src/LibHac/Sm/ServiceManager.cs | 4 ++-- src/LibHac/Sm/ServiceName.cs | 2 +- src/hactoolnet/ProcessNax0.cs | 2 +- 7 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/LibHac/Common/Buffer.cs b/src/LibHac/Common/Buffer.cs index ffe69932..071fdc36 100644 --- a/src/LibHac/Common/Buffer.cs +++ b/src/LibHac/Common/Buffer.cs @@ -34,7 +34,7 @@ namespace LibHac.Common [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator ReadOnlySpan(in Buffer16 value) { - return SpanHelpers.AsReadOnlyByteSpan(ref Unsafe.AsRef(in value)); + return SpanHelpers.AsReadOnlyByteSpan(in value); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -57,7 +57,7 @@ namespace LibHac.Common [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly ReadOnlySpan AsReadOnlySpan() where T : unmanaged { - return SpanHelpers.AsReadOnlySpan(ref Unsafe.AsRef(in this)); + return SpanHelpers.AsReadOnlySpan(in this); } public override string ToString() @@ -93,7 +93,7 @@ namespace LibHac.Common [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator ReadOnlySpan(in Buffer32 value) { - return SpanHelpers.AsReadOnlyByteSpan(ref Unsafe.AsRef(in value)); + return SpanHelpers.AsReadOnlyByteSpan(in value); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -116,7 +116,7 @@ namespace LibHac.Common [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly ReadOnlySpan AsReadOnlySpan() where T : unmanaged { - return SpanHelpers.AsReadOnlySpan(ref Unsafe.AsRef(in this)); + return SpanHelpers.AsReadOnlySpan(in this); } public override string ToString() diff --git a/src/LibHac/Common/SpanHelpers.cs b/src/LibHac/Common/SpanHelpers.cs index 77ddbeac..700ab9bd 100644 --- a/src/LibHac/Common/SpanHelpers.cs +++ b/src/LibHac/Common/SpanHelpers.cs @@ -33,29 +33,29 @@ namespace LibHac.Common } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ReadOnlySpan CreateReadOnlySpan(ref T reference, int length) + public static ReadOnlySpan CreateReadOnlySpan(in T reference, int length) { - return MemoryMarshal.CreateReadOnlySpan(ref reference, length); + return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in reference), length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ReadOnlySpan AsReadOnlySpan(ref T reference) where T : unmanaged + public static ReadOnlySpan AsReadOnlySpan(in T reference) where T : unmanaged { - return CreateReadOnlySpan(ref reference, 1); + return CreateReadOnlySpan(in reference, 1); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ReadOnlySpan AsReadOnlySpan(ref TStruct reference) + public static ReadOnlySpan AsReadOnlySpan(in TStruct reference) where TStruct : unmanaged where TSpan : unmanaged { - return CreateReadOnlySpan(ref Unsafe.As(ref reference), + return CreateReadOnlySpan(in Unsafe.As(ref Unsafe.AsRef(in reference)), Unsafe.SizeOf() / Unsafe.SizeOf()); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ReadOnlySpan AsReadOnlyByteSpan(ref T reference) where T : unmanaged + public static ReadOnlySpan AsReadOnlyByteSpan(in T reference) where T : unmanaged { - return CreateReadOnlySpan(ref Unsafe.As(ref reference), Unsafe.SizeOf()); + return CreateReadOnlySpan(in Unsafe.As(ref Unsafe.AsRef(in reference)), Unsafe.SizeOf()); } // All AsStruct methods do bounds checks on the input diff --git a/src/LibHac/Diag/Assert.cs b/src/LibHac/Diag/Assert.cs index a269a4fb..af40976c 100644 --- a/src/LibHac/Diag/Assert.cs +++ b/src/LibHac/Diag/Assert.cs @@ -1,4 +1,5 @@ -using System.Diagnostics; +using System; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; namespace LibHac.Diag @@ -42,5 +43,21 @@ namespace LibHac.Diag throw new LibHacException($"Value {value} is not in the range {lowerInclusive} to {upperExclusive}"); } } + + public static void Equal(T value1, T value2) where T : IEquatable + { + if (!value1.Equals(value2)) + { + throw new LibHacException($"Values were not equal: {value1}, {value2}"); + } + } + + public static void NotEqual(T value1, T value2) where T : IEquatable + { + if (value1.Equals(value2)) + { + throw new LibHacException($"Values should not be equal: {value1}, {value2}"); + } + } } } diff --git a/src/LibHac/FsSystem/FsPath.cs b/src/LibHac/FsSystem/FsPath.cs index 208a2983..25bc9831 100644 --- a/src/LibHac/FsSystem/FsPath.cs +++ b/src/LibHac/FsSystem/FsPath.cs @@ -33,7 +33,7 @@ namespace LibHac.FsSystem } [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); } diff --git a/src/LibHac/Sm/ServiceManager.cs b/src/LibHac/Sm/ServiceManager.cs index 7f31d592..0ecd38f4 100644 --- a/src/LibHac/Sm/ServiceManager.cs +++ b/src/LibHac/Sm/ServiceManager.cs @@ -76,7 +76,7 @@ namespace LibHac.Sm int nameLen; for (nameLen = 1; nameLen < Unsafe.SizeOf(); nameLen++) { - if (SpanHelpers.AsReadOnlyByteSpan(ref name)[nameLen] == 0) + if (SpanHelpers.AsReadOnlyByteSpan(in name)[nameLen] == 0) { break; } @@ -85,7 +85,7 @@ namespace LibHac.Sm // Names must be all-zero after they end. for (; nameLen < Unsafe.SizeOf(); nameLen++) { - if (SpanHelpers.AsReadOnlyByteSpan(ref name)[nameLen] != 0) + if (SpanHelpers.AsReadOnlyByteSpan(in name)[nameLen] != 0) { return ResultSm.InvalidServiceName.Log(); } diff --git a/src/LibHac/Sm/ServiceName.cs b/src/LibHac/Sm/ServiceName.cs index be92af43..78653184 100644 --- a/src/LibHac/Sm/ServiceName.cs +++ b/src/LibHac/Sm/ServiceName.cs @@ -37,7 +37,7 @@ namespace LibHac.Sm public override string ToString() { ulong name = Name; - return StringUtils.Utf8ZToString(SpanHelpers.AsReadOnlyByteSpan(ref name)); + return StringUtils.Utf8ZToString(SpanHelpers.AsReadOnlyByteSpan(in name)); } } } diff --git a/src/hactoolnet/ProcessNax0.cs b/src/hactoolnet/ProcessNax0.cs index b9d042e9..4ddc7055 100644 --- a/src/hactoolnet/ProcessNax0.cs +++ b/src/hactoolnet/ProcessNax0.cs @@ -60,7 +60,7 @@ namespace hactoolnet AesXtsFileHeader header = xtsFile.Header; 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 Size:", $"{header.Size:x12}"); PrintItem(sb, colLen, " Header HMAC:", header.Signature);