-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate boolean fields in generic structs as a custom struct.
- Loading branch information
Showing
3 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Il2CppInterop.Runtime | ||
{ | ||
[StructLayout(LayoutKind.Sequential)] | ||
public readonly struct NativeBoolean : IComparable, IComparable<bool>, IEquatable<bool>, IComparable<NativeBoolean>, IEquatable<NativeBoolean> | ||
{ | ||
private readonly byte Value; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator bool(NativeBoolean b) | ||
=> Unsafe.As<NativeBoolean, bool>(ref b); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator NativeBoolean(bool b) | ||
=> Unsafe.As<bool, NativeBoolean>(ref b); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public override int GetHashCode() | ||
=> Unsafe.As<byte, bool>(ref Unsafe.AsRef(in Value)).GetHashCode(); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public override string ToString() | ||
=> Unsafe.As<byte, bool>(ref Unsafe.AsRef(in Value)).ToString(); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public string ToString(IFormatProvider? provider) | ||
=> Unsafe.As<byte, bool>(ref Unsafe.AsRef(in Value)).ToString(provider); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool TryFormat(Span<char> destination, out int charsWritten) | ||
=> Unsafe.As<byte, bool>(ref Unsafe.AsRef(in Value)).TryFormat(destination, out charsWritten); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public override bool Equals(object? obj) | ||
=> obj switch | ||
{ | ||
bool boolean => this == boolean, | ||
NativeBoolean nativeBool => this == nativeBool, | ||
_ => false | ||
}; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool Equals(bool other) | ||
=> this == other; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool Equals(NativeBoolean other) | ||
=> this == other; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public int CompareTo(object? obj) | ||
=> Unsafe.As<byte, bool>(ref Unsafe.AsRef(in Value)).CompareTo(obj); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public int CompareTo(bool value) | ||
=> Unsafe.As<byte, bool>(ref Unsafe.AsRef(in Value)).CompareTo(value); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public int CompareTo(NativeBoolean value) | ||
=> CompareTo(Unsafe.As<NativeBoolean, bool>(ref value)); | ||
} | ||
} |