-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace WPIUtil.Marshal; | ||
|
||
public readonly unsafe struct Ptr<T> where T : unmanaged | ||
{ | ||
private readonly T* ptr; | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private Ptr(T* ptr) => this.ptr = ptr; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator Ptr<T>(T* p) => new(p); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator T*(Ptr<T> ptr) => ptr.ptr; | ||
|
||
public ref T this[int index] | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => ref *(ptr + index); | ||
} | ||
|
||
public bool IsNull | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => ptr == null; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public ref Ptr<byte> GetPinnableByteReference() | ||
{ | ||
return ref Unsafe.As<Ptr<T>, Ptr<byte>>(ref Unsafe.AsRef(in this)); | ||
} | ||
} |
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