using Brutal.Attributes; namespace Brutal.Pointers; /// /// Wraps a pointer to a pointer. /// public readonly unsafe struct PtrPtr(void** pointerPointer) : INullable { /// /// The pointer-pointer. /// public void** Pointer => pointerPointer; /// /// Convert the pointer to a span of pointers. /// [return: Fixed] public Span AsSpan(ElementCount length) { // Ptr is equivalent to void*, // So we can cast void** to Ptr*, // Or so we will hope return new Span((Ptr*)Pointer, length); } /// /// Convert the pointer to a span of pointers. /// public Span> AsSpan(ElementCount length) where T : unmanaged { // Ptr is equivalent to T*, // So we can cast void** to Ptr, // Or so we will hope return new Span>((Ptr*)Pointer, length); } /// public bool IsNull() { return Pointer == null; } /// /// Get the value of the pointer as a reference. /// public ref void* Value => ref *Pointer; /// /// Access the pointer at the given index. /// public Ptr this[int index] { get => Pointer[index]; set => Pointer[index] = value.Pointer; } /// /// Implicit conversion from void** to PtrPtr. /// public static implicit operator PtrPtr(void** address) { return new PtrPtr(address); } /// /// Implicit conversion from PtrPtr to void** /// public static implicit operator void**(PtrPtr ptr) { return ptr.Pointer; } /// /// Explicit conversion from nint to PtrPtr. /// public static explicit operator PtrPtr(nint ptr) { return new PtrPtr((void**)ptr); } /// /// Explicit cast to nint. /// public static explicit operator nint(PtrPtr ptr) { return (nint)ptr.Pointer; } }