|
| 1 | +using System; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | + |
| 5 | +namespace Il2CppInterop.Runtime |
| 6 | +{ |
| 7 | + [StructLayout(LayoutKind.Sequential)] |
| 8 | + public readonly struct NativeBoolean : IComparable, IComparable<bool>, IEquatable<bool>, IComparable<NativeBoolean>, IEquatable<NativeBoolean> |
| 9 | + { |
| 10 | + private readonly byte Value; |
| 11 | + |
| 12 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 13 | + public static implicit operator bool(NativeBoolean b) |
| 14 | + => Unsafe.As<NativeBoolean, bool>(ref b); |
| 15 | + |
| 16 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 17 | + public static implicit operator NativeBoolean(bool b) |
| 18 | + => Unsafe.As<bool, NativeBoolean>(ref b); |
| 19 | + |
| 20 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 21 | + public override int GetHashCode() |
| 22 | + => Unsafe.As<byte, bool>(ref Unsafe.AsRef(in Value)).GetHashCode(); |
| 23 | + |
| 24 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 25 | + public override string ToString() |
| 26 | + => Unsafe.As<byte, bool>(ref Unsafe.AsRef(in Value)).ToString(); |
| 27 | + |
| 28 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 29 | + public string ToString(IFormatProvider? provider) |
| 30 | + => Unsafe.As<byte, bool>(ref Unsafe.AsRef(in Value)).ToString(provider); |
| 31 | + |
| 32 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 33 | + public bool TryFormat(Span<char> destination, out int charsWritten) |
| 34 | + => Unsafe.As<byte, bool>(ref Unsafe.AsRef(in Value)).TryFormat(destination, out charsWritten); |
| 35 | + |
| 36 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 37 | + public override bool Equals(object? obj) |
| 38 | + => obj switch |
| 39 | + { |
| 40 | + bool boolean => this == boolean, |
| 41 | + NativeBoolean nativeBool => this == nativeBool, |
| 42 | + _ => false |
| 43 | + }; |
| 44 | + |
| 45 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 46 | + public bool Equals(bool other) |
| 47 | + => this == other; |
| 48 | + |
| 49 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 50 | + public bool Equals(NativeBoolean other) |
| 51 | + => this == other; |
| 52 | + |
| 53 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 54 | + public int CompareTo(object? obj) |
| 55 | + => Unsafe.As<byte, bool>(ref Unsafe.AsRef(in Value)).CompareTo(obj); |
| 56 | + |
| 57 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 58 | + public int CompareTo(bool value) |
| 59 | + => Unsafe.As<byte, bool>(ref Unsafe.AsRef(in Value)).CompareTo(value); |
| 60 | + |
| 61 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 62 | + public int CompareTo(NativeBoolean value) |
| 63 | + => CompareTo(Unsafe.As<NativeBoolean, bool>(ref value)); |
| 64 | + } |
| 65 | +} |
0 commit comments