// Copyright (c) 2022 bradson // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using JetBrains.Annotations; namespace FisheryLib.FunctionPointers; public static class GetHashCode { public static readonly unsafe delegate* Default = (delegate*)FunctionPointers.GetHashCode.HashCodeMethods.GetFunctionPointer(typeof(T)); } [SuppressMessage("Naming", "CA1720")] public static class GetHashCode { [UsedImplicitly(ImplicitUseTargetFlags.Members)] public static class Methods { public static int Byte(byte obj) => obj; public static int Int(int obj) => obj; public static int Uint(uint obj) => (int)obj; public static int Ushort(ushort obj) => obj; public static unsafe int VoidPtr(void* obj) => (int)obj; public static int Enum(T obj) where T : struct => JitHelpers.UnsafeEnumCast(obj).GetHashCode(); public static int SbyteEnum(T obj) where T : struct => ((sbyte)JitHelpers.UnsafeEnumCast(obj)).GetHashCode(); public static int ShortEnum(T obj) where T : struct => ((short)JitHelpers.UnsafeEnumCast(obj)).GetHashCode(); public static int LongEnum(T obj) where T : struct => JitHelpers.UnsafeEnumCastLong(obj).GetHashCode(); public static int ValueType(T obj) where T : struct => obj.GetHashCode(); public static unsafe int Nullable(T? obj) where T : struct => !obj.HasValue ? 0 : GetHashCode.Default(obj.GetValueOrDefault()); public static int Reference(T obj) where T : class => RuntimeHelpers.GetHashCode(obj); public static int Object(T? obj) where T : class => obj?.GetHashCode() ?? 0; } public static readonly MethodFactory.HashCode HashCodeMethods = new(typeof(Methods), "FisheryHashCodeMethod_", false); }