Skip to content

Consider adding operators to FFI Pointer objects and deprecate/remove not-very-idiomatic elementAt #54250

Closed
@mkustermann

Description

@mkustermann

If we have a Pointer<Uint8> and would like to get one that is 1 bytes further we can

Pointer<Uint8> p;

final p1 = p.elementAt(1);
final p1 = Pointer.fromAddress(p.address + 1);

None of which seem very nice.

Also the name elementAt would suggest one actually gets the element at a certain offset, which it doesn't, it gets the address of the element at an offset.

In C one would simply

uint8_t* p = ...;

uint8_t* p10 = p + 1;

We should consider adding extension methods such as

extension on Pointer<Uint8> {
  Pointer<Uint8> operator +(int offset) =>
      Pointer.fromAddress(address + offset);
}

then the example will turn into

Pointer<Uint8> p;
final p10 = p + 1;

Even the following would work out of the box:

Pointer<Uint8> p;
p++;

If we do so, there may no longer be a need to have elementAt and could deprecate & remove it.

There's a question whether we should support operator- and if so, whether it should take two pointers and return the difference as an integer or it should take an int and return a pointer minus the offset.

/cc @dcharkes opinions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.good first issueA good starting issue for contributors (issues with this label will appear in /contribute)library-ffitriagedIssue has been triaged by sub teamtype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions