Description
dart:ffi
internally calculates the fields offsets of structs in order to enable loading/storing from/to those fields.
However, sometimes one would like to pass a pointer to a field inside a struct to C. For that use case we should expose the internal offsets.
One way we could do this, for example for
sdk/tests/ffi_2/very_large_struct.dart
Lines 8 to 51 in 4150349
class VeryLargeStruct extends Struct {
@Int8()
int a;
external static int get offsetOfA; // Will be filled in by the compiler.
// ...
}
Or alternatively we could provide inner pointers:
class VeryLargeStruct extends Struct {
@Int8()
int a;
external static Pointer<Int8> get pointerToA; // Will be filled in by the compiler.
// ...
}
(Though, I like exposing offset better. That is a better indication you're doing your own pointer arithmetic.)
Edit: we can't expose Pointers
because the struct might be backed by TypedData
. But we could generate extension methods for the struct pointers in package:ffigen
: dart-lang/native#331.
cc @timsneath