using Brutal.Attributes; namespace Brutal.Pointers; /// /// A wrapper for an untyped pointer. /// public readonly unsafe struct Ptr(void* pointer) : INullable, IEquatable { /// /// The raw void pointer. /// public void* Pointer => pointer; /// /// Returns a new , offset by the given byte count. /// public Ptr Offset(ByteSize byteOffset) { return new Ptr((byte*)Pointer + byteOffset); } /// /// Converts the pointer to a Span of the given type and length. /// [return: Fixed] public Span AsSpan(ElementCount length) { return new Span(Pointer, length); } /// /// Casts the pointer to a typed pointer. /// public Ptr Cast() where TCastType : unmanaged { return new Ptr((TCastType*)Pointer); } /// public bool IsNull() { return Pointer == null; } /// /// Implicit conversion from void* to /// public static implicit operator Ptr(void* pointer) { return new Ptr(pointer); } /// /// Implicit conversion from to void*. /// public static implicit operator void*(Ptr ptr) { return ptr.Pointer; } /// /// Explicit conversion from nint to . /// public static explicit operator Ptr(nint ptr) { return new Ptr((void*)ptr); } /// /// Explicit cast to nint. /// public static explicit operator nint(Ptr ptr) { return (nint)ptr.Pointer; } public bool Equals(Ptr other) { return Pointer == other.Pointer; } public override bool Equals(object? obj) { return obj is Ptr other && Equals(other); } public override int GetHashCode() { return ((nint)Pointer).GetHashCode(); } public static bool operator ==(Ptr left, Ptr right) { return left.Equals(right); } public static bool operator !=(Ptr left, Ptr right) { return !left.Equals(right); } }