shekohex / allo-isolate

Run Multithreaded Rust along with Dart VM (in isolate) 🌀

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Passing structs through an isolate.post()

jerel opened this issue · comments

@shekohex I've looked through dart-bindgen, allo_isolate, and the flutterrust projects and one example that I can't find is that of passing a struct to an isolate. Is that currently possible?

Example:

struct Location {
  lat: f64,
  lng: f64,
  name: String,
}

let location = Location {
  lat: 20.1,
  lng: 52.2,
  name: String::from("test"),
};

let isolate = Isolate::new(port);
isolate.post(location);

Well, the v0.1.8-beta contains a workaround for this case, where you can send a raw pointer of the struct (heap allocated) and on the dart side you can create the struct from the pointer again.

https://docs.rs/allo-isolate/0.1.8-beta/allo_isolate/trait.IntoDart.html#impl-IntoDart-for-*const%20T

A workaround to send raw pointers to dart over the port. it will be sent as int64 on 64bit targets, and as int32 on 32bit targets.

You can see an example here:
https://github.com/edgeware-builders/edgeware-wallet/blob/d8f62eb5d4dfaeeb941fe3e3b12d89b7c4cfdff9/native/edgeware/src/lib.rs#L175-L180

Also here: https://github.com/edgeware-builders/edgeware-wallet/blob/d8f62eb5d4dfaeeb941fe3e3b12d89b7c4cfdff9/native/edgeware/src/shared_ptr.rs

Just note that you need also to expose a function from the Rust side to the Dart to free the allocated struct.

Hope that helps you!

Thanks for the quick feedback, this was very helpful!