Giter VIP home page Giter VIP logo

Comments (2)

shekohex avatar shekohex commented on August 19, 2024

If I understand you correctly, you mean when you send a Uint8List from Dart to Rust, is there any memcopy involved? The answer depends on the implementation really, the DartVM and your code.

allo-isolate is very low-level when it comes to send data from Dart to Rust, especially on Objects/Classes/Structs ..etc.
Rust expects the things that gets sent to its functions to be C-ABI compatible; Hence when sending a Uint8List to Rust you need to send two things (ptr, len) so Rust can read the data by using std::slice::from_raw_parts.

The way you get a pointer from the Uint8List is a bit tricky, since afaik, there is no direct way to do so, since this data is managed by the GC. The only way that I know is to allocate memory in the heap, copy the bytes form the Uint8List and then send a pointer to that newly allocated memory to rust with the len, so rust can work with it.

Here is an example:

pub unsafe extern "C" fn my_rusty_function(
    port: i64,
    data: *const u8,
    len: usize,
) -> i32 {
    // check if the pointer is null first
    if data.is_null() {
        return -1;
    }
    // then read it as a slice
    let buf = std::slice::from_raw_parts(data, len);
    let isolate = allo_isolate::Isolate::new(port);
    // Do something with the buffer.
    // And return the result back to dart.
    isolate.post(true);
   0
}

To Call my_rusty_function from Dart, you need bindings that can be generated, here is a snippet of the generated code:

int my_rusty_function(
    int port,
    ffi.Pointer<ffi.Uint8> data,
    int len,
  ) {
    return _my_rusty_function(
      port,
      data,
      len,
    );
  }

final _my_rusty_functionPtr = _lookup<ffi.NativeFunction<ffi.Int32 Function(ffi.Int64, ffi.Pointer<ffi.Uint8>, ffi.Uint64)>>('my_rusty_function');
final _my_rusty_function = _my_rusty_functionPtr.asFunction<int Function(int, ffi.Pointer<ffi.Uint8>, int)>();

To call the above function with a UintList8 you will need to allocate memory on the heap and free it manually too!

final mData = Unit8List.from([1, 2, 3, 4]);
final mDataLen = myData.length;
final mDataPtr = ffi.malloc.allocate<Uint8>(mDataLen)..asTypedList(mDataLen).setAll(0, mData);
// Do not worry about these, they are for creating an isolate and get the port.
final completer = Completer<Uint8List>();
final port = singleCompletePort(completer);

final callResult = my_rusty_function(
    port.nativePort,
    mDataPtr,
    mDataLen,
  );
// do something with the callResult (i.e assert it is zero)
final response = await completer.future;
// do something with the response from Rust side.
...

// DO NOT FORGET TO FREE THE ALLOCATED BUFFER.
ffi.malloc.free(mDataPtr);

The allo-isolate does not enforce any pattern on how you would send that data, and gives you the full freedom to do so. In the previous example you can also agree and let rust free the buffer for you once it is done with it, instead of doing it on dart side, but ofc not both.

from allo-isolate.

temeddix avatar temeddix commented on August 19, 2024

Great explanation. Thank you very much :)

from allo-isolate.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.