UserDataSlot<TValue>
Uniquely identifies user data which can be associated
with any object.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
namespace DotNext
{
[StructLayout(LayoutKind.Auto)]
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
public readonly struct UserDataSlot<TValue> : IEquatable<UserDataSlot<TValue>>
{
private static volatile int valueIndexCounter;
internal static readonly int TypeIndex = UserDataSlot.Allocate();
private readonly int valueIndex;
internal int ValueIndex => valueIndex - 1;
public bool IsAllocated => valueIndex != 0;
public UserDataSlot()
{
valueIndex = Interlocked.Increment(ref valueIndexCounter);
}
[Obsolete("Use public constructor to allocate the slot")]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static UserDataSlot<TValue> Allocate()
{
return new UserDataSlot<TValue>();
}
public bool Equals([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] UserDataSlot<TValue> other)
{
return valueIndex == other.valueIndex;
}
public override bool Equals([NotNullWhen(true)] object other)
{
if (other is UserDataSlot<TValue>) {
UserDataSlot<TValue> other2 = (UserDataSlot<TValue>)other;
return Equals(other2);
}
return false;
}
public override int GetHashCode()
{
return valueIndex;
}
[System.Runtime.CompilerServices.NullableContext(1)]
public override string ToString()
{
return UserDataSlot.ToString(TypeIndex, valueIndex);
}
public static bool operator ==([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] UserDataSlot<TValue> first, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] UserDataSlot<TValue> second)
{
return first.valueIndex == second.valueIndex;
}
public static bool operator !=([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] UserDataSlot<TValue> first, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] UserDataSlot<TValue> second)
{
return first.valueIndex != second.valueIndex;
}
}
}