Replies: 1 comment
-
| You can use them in F#: [<Struct>] [<System.Runtime.CompilerServices.InlineArray(10)>] type FixedBuffer = struct val element0:int end [<Struct; StructLayout(LayoutKind.Sequential)>] type StructWithBuffer = struct val field1:int val field2:int val otherFixedField:FixedBuffer endand then use it like this: let size = Marshal.SizeOf<StructWithBuffer>() printfn "Size of StructWithBuffer: %i" size let ptr = NativePtr.stackalloc<StructWithBuffer> 1 let unmarshaled : StructWithBuffer = ptr |> NativePtr.read // The type system knows the buffer it pointing to ints, but not the size of the buffer let fixedField = &&unmarshaled.otherFixedField.element0 fixedField |> NativePtr.read |> printfn "Fixed field first element: %i" // You need to take care not to read past the index. NativePtr.get fixedField 4 |> printfn "Fixed field element 4: %i" // You need to take care not to read past the index. NativePtr.get fixedField 11 |> printfn "Fixed field element 11 (this is something else's data): %i"output: You see it allocates 48 bytes:
You can read and write those elements and |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
For example in C# there is :
or in C# 12 supported by the up coming .NET 8
how do I create these struct and use it in F#
Beta Was this translation helpful? Give feedback.
All reactions