Return Slice of Strings from Rust to C# #75
Replies: 4 comments
-
Which problem do you mean, and what do you mean by "calling |
Beta Was this translation helpful? Give feedback.
-
By problem I mean: How do I use the returned variable as "the underlying type" i.e. FFIPlayerStruct in my case. The bindings generated look like this: public SliceMutFFIPlayerStruct(GCHandle handle, ulong count)
{
this.data = handle.AddrOfPinnedObject();
this.len = count;
}
public SliceMutFFIPlayerStruct(IntPtr handle, ulong count)
{
this.data = handle;
this.len = count;
}
#if UNITY_2018_1_OR_NEWER
public SliceMutFFIPlayerStruct(NativeArray<IntPtr> handle)
{
unsafe
{
this.data = new IntPtr(NativeArrayUnsafeUtility.GetUnsafeReadOnlyPtr(handle));
this.len = (ulong) handle.Length;
}
} This is how I am trying to use the bindings in C#. Maybe I am misunderstanding how to create the FFISlices? int len = Bindings.UnityWrapper.GetFFIPlayerStructsLen();
var output = new NativeArray<IntPtr>(len, Allocator.Temp);
Bindings.UnityWrapper.SliceMutFFIPlayerStruct mut_slice = new Bindings.UnityWrapper.SliceMutFFIPlayerStruct(output);
interop.GetPlayerStructs(ref mut_slice);
foreach (IntPtr item in output) {
Debug.Log(item.GetPlayerName()); // i understand this doesn't work but is where i get stuck, how can i treat IntPtr like the underlying FFIPlayerStruct
} I hope this makes sense! Many thanks for taking the time helping out. |
Beta Was this translation helpful? Give feedback.
-
I still have some trouble following, but do you do This at least might contribute to any issue. You are passing a reference to a slice, which isn't well supported (and really only needed if you want to interface with the Burst compiler). In other words, try this: pub fn get_player_structs(&self, buffer: FFISliceMut<FFIPlayerStruct>) -> Result<(), Error> { ... } If that's not it, could you please push a self-contained "almost compiling" MVP somewhere, I have some trouble "compiling" this problem in my mind as-is. |
Beta Was this translation helpful? Give feedback.
-
Hey, sorry for delay in response! Hope this makes sense and merry christmas :-) |
Beta Was this translation helpful? Give feedback.
-
Hi!
I am sure this isn't an actual issue but rather me not understanding how things work.
I am trying give a slice of strings from Rust to C# but can't figure out how to structure the flow.
I've been trying to do something like this (my assumption is Rust should own the underlying CString and then just return an AsciiPointer to it)...
Some other service holds a vector of player structs. My understanding is that passing a mutable slice with pre-allocated size is how we're meant to pass a vector/array.
The interop bindings generated look like this (I assume the IntPtr is because FFIPlayerStruct is opaque)
My question is if this is the right way to go around the problem and if so - how are you meant to call get_player_name() from an IntPtr? Apologies but my C# isn't the strongest.
Btw, been diving deeper and deeper into the crate over the last few weeks and it's really great!
Beta Was this translation helpful? Give feedback.
All reactions