Skip to content

Commit

Permalink
Make PinnableByteReference clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Jan 29, 2024
1 parent 1bec052 commit 9f76140
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
16 changes: 8 additions & 8 deletions src/ntcore/Natives/RefNetworkTableValueMarshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ public void FromManaged(in RefNetworkTableValue managed, Span<byte> callerAlloca
break;
case NetworkTableType.Raw:
m_toPin = ref managed.m_byteSpan.GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.valueRaw.data.GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.valueRaw.data.GetPinnableByteReference();
m_nativeValue.data.valueRaw.size = (nuint)managed.m_byteSpan.Length;
break;
case NetworkTableType.DoubleArray:
m_toPin = ref MemoryMarshal.AsBytes(managed.m_doubleSpan).GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.arrDouble.arr.GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.arrDouble.arr.GetPinnableByteReference();
m_nativeValue.data.arrDouble.size = (nuint)managed.m_doubleSpan.Length;
break;
case NetworkTableType.IntegerArray:
m_toPin = ref MemoryMarshal.AsBytes(managed.m_longSpan).GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.arrInt.arr.GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.arrInt.arr.GetPinnableByteReference();
m_nativeValue.data.arrInt.size = (nuint)managed.m_longSpan.Length;
break;
case NetworkTableType.FloatArray:
m_toPin = ref MemoryMarshal.AsBytes(managed.m_floatSpan).GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.arrFloat.arr.GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.arrFloat.arr.GetPinnableByteReference();
m_nativeValue.data.arrFloat.size = (nuint)managed.m_floatSpan.Length;
break;
case NetworkTableType.BooleanArray:
Expand All @@ -69,7 +69,7 @@ public void FromManaged(in RefNetworkTableValue managed, Span<byte> callerAlloca
boolArraySpan[i] = managed.m_boolSpan[i] ? 1 : 0;
}
m_toPin = ref MemoryMarshal.AsBytes(boolArraySpan).GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.arrBoolean.arr.GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.arrBoolean.arr.GetPinnableByteReference();
m_nativeValue.data.arrBoolean.size = (nuint)managed.m_boolSpan.Length;
break;
case NetworkTableType.String:
Expand All @@ -78,7 +78,7 @@ public void FromManaged(in RefNetworkTableValue managed, Span<byte> callerAlloca
// String is stored as utf-8 in raw span
m_toPin = ref managed.m_byteSpan.GetPinnableReference();
m_nativeValue.data.valueString = new(null, (nuint)managed.m_byteSpan.Length);
m_toAssignPin = ref m_nativeValue.data.valueString.Str.GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.valueString.Str.GetPinnableByteReference();
}
else
{
Expand All @@ -97,7 +97,7 @@ public void FromManaged(in RefNetworkTableValue managed, Span<byte> callerAlloca
Debug.Assert(exactBytes == byteCount);
m_toPin = ref stringSpan.GetPinnableReference();
m_nativeValue.data.valueString = new(null, (nuint)stringSpan.Length);
m_toAssignPin = ref m_nativeValue.data.valueString.Str.GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.valueString.Str.GetPinnableByteReference();
}
break;
case NetworkTableType.StringArray:
Expand All @@ -113,7 +113,7 @@ public void FromManaged(in RefNetworkTableValue managed, Span<byte> callerAlloca
}

m_toPin = ref MemoryMarshal.AsBytes(strings.AsSpan()).GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.arrString.arr.GetPinnableReference();
m_toAssignPin = ref m_nativeValue.data.arrString.arr.GetPinnableByteReference();
m_nativeValue.data.arrString.size = (nuint)managed.m_stringSpan.Length;
break;
default:
Expand Down
34 changes: 34 additions & 0 deletions src/wpiutil/Marshal/Ptr.cs
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));
}
}
31 changes: 0 additions & 31 deletions src/wpiutil/Marshal/WPIStringMarshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,34 +147,3 @@ public readonly string ConvertToString()
}
}
}

public unsafe readonly 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> GetPinnableReference()
{
return ref Unsafe.As<Ptr<T>, Ptr<byte>>(ref Unsafe.AsRef(in this));
}
}

0 comments on commit 9f76140

Please sign in to comment.