-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[vm/ffi] Pointer.asArray
or Array.fromPointer
#54526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Any pointer can be seen as an array (array is just a pointer and a known size). Why is this not extension ArrayPointer<T extends NativeType> on Pointer<T> {
external Array<T> asArray(int dim);
// Requires H to have `dims.length` nested `Array`s with innermost argument being `T`
external H asMultiArray<H extends Array>(List<int> dims);
} to allow Pointer<Float32> floats;
Array<Float32> vector = floats.asArray(4);
Array<Array<Float32>> matrix = floats.asMultiArray<Array<Array<Float32>>>([4, 4]); The n-dimensional part is a little weird, because one has to explicitly write the array types as the static type system doesn't look at the One could also consider making this something like this instead: extension ArrayPointer<T extends NativeType> on Pointer<T> {
external Array<T> asArray(int n);
}
extension ArrayOfArray<T extends NativeType> on Array<T> {
external Array<Array<T>> split(int n);
} to allow Pointer<Float32> floats;
Array<Float32> vector = floats.asArray(4);
Array<Array<Float32>> matrix = floats.asArray(16).split(4); // 4x4 matrix
Array<Array<Array<Float32>>> matrix = floats.asArray(16).split(4).split(2); // 4x2x2 |
Ah right, yes I mixed up the types for multi-dimensional arrays. I think we want to add extension methods in the extensions that are for fixed-size types. To avoid having to write special static checks for
That could work, but does I think it would make slightly more sense to split the outer dimension, because the outermost Array type represents the outermost dimension. Pointer<Float32> floats;
Array<Float32> vector = floats.asArray(4);
Array<Array<Float32>> matrix = floats.asArray(16).split(4); // 4x4 matrix
Array<Array<Array<Float32>>> matrix2 = floats.asArray(16).split(4).split(2); // 2x2x4
final innerMatrix = matrix[0].split(2); // 2x2 this would split the innermost dimension (after dereferencing the outer dimension) But then doing splits in reverse order because you're always acting on the outermost dimension feels somewhat weird. Maybe the documentation should just mention "innermost" in bold, idk what the right solution is here. @lrhn to the rescue! It would definitely help not having to retype type arguments. |
Good point. Would an intermediary super type (that |
Maybe a marker type instead of a super type, that could also serve as documentation. |
A type that As for split, I'd split the outer array. That's the one that the extension is matching on, so it's the only one mentioned in the signature. I know |
Yes, exactly, it should be public. And yes, we should use it for I was thinking of |
The only thing that worries me is that almost every type is |
|
(Maybe it need to be This means we will need to a breaking change for
I don't believe we have a way to indicate that we want to tighten a type argument bound on a method with a |
Quick mock up of changes for @lrhn any suggestions for doing the breaking change to I'm assuming we have users implementing the |
Why? One cannot |
The implementors of |
Strictly speaking, mainly the higher level abstractions that work on the types need to be updated, i.e. The |
Ack. Type parameter bounds are invariant, so any change to only one of an override-pair will be breaking. You'll need to update all of them at the same time. Allocator is in Keeping You could introduce a It's a breaking change to split a type into two, and start restricting to only one of them in places where you previously accepted all. The best approach is to do it as a proper breaking change. Not necessarily the easiest, but it'll likely have the best final outcome, where everything is again coherent. EDIT: Or if @mkustermann is right, don't necessarily change |
In some cases it might be useful to create an
Array
out of aPointer
.We could add extension methods to
Pointer
:This would be kind of similar to the
asTypedList
methods, but would work on anything that is sized, including structs, unions, nested arrays.Alternatively, we could add a factories to
Array
. That would make it more similar to constructors that take a typed data:Array.fromTypedData
constructors #54525(Note that if the above issue is addressed, a workaround for struct pointers could be
Array<...>.fromTypedData(pointer.cast<Uint8>().asTypedList(sizeOf<MyStruct>() * length), dimensions...)
Workaround for compile-time length arrays
Define a wrapper struct, cast the pointer to that, and read the array out:
The text was updated successfully, but these errors were encountered: