-
Hello, hope this project is doing well. I wanted to ask a question in discussions, but since I couldn't create a thread I write here. I had a simple question just like the title says. I thought that, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
If I understand you correctly, you mean when you send a allo-isolate is very low-level when it comes to send data from Dart to Rust, especially on Objects/Classes/Structs ..etc. The way you get a pointer from the 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 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 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 |
Beta Was this translation helpful? Give feedback.
-
Great explanation. Thank you very much :) |
Beta Was this translation helpful? Give feedback.
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 usingstd::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…