英文:
Call Rust Function(Copy_Dir) From Dart (Using ffi)
问题
以下是翻译好的内容:
When i am trying to Run this Function From Dart side it's always giving me this Problem
当我尝试从Dart端运行这个函数时,它总是给我这个问题
memory allocation of 140374366121105 bytes failed
内存分配失败,大小为140374366121105字节
Lost connection to device.
与设备的连接丢失。
But my Source folder is only 2.4 kb
但我的源文件夹只有2.4 kb
Here Is my Rust Function:-
以下是我的Rust函数:
fn copy_dir(source: &Path, destination: &Path) -> io::Result<()> {
if source.is_dir() {
fs::create_dir_all(destination)?;
for entry in fs::read_dir(source)? {
let entry = entry?;
let entry_path = entry.path();
let dest_path = destination.join(entry.file_name());
if entry_path.is_dir() {
fs::create_dir_all( &entry_path).unwrap();
} else {
fs::copy(&entry_path, &dest_path)?;
}
}
} else {
fs::copy(source, destination)?;
}
Ok(())
}
From dart side i am calling this function like this:-
从Dart端,我是这样调用这个函数的:
`typedef CopyDirFunc = ffi.Void Function(
ffi.Pointer<Utf8> source,
ffi.Pointer<Utf8> destination,
);
typedef CopyDir = void Function(
ffi.Pointer<Utf8> source,
ffi.Pointer<Utf8> destination,
);
void main(){
final path = Platform.isWindows
? 'path\\to\\mylib.dll'
: '/path/to/mylib.so';
final dylib = ffi.DynamicLibrary.open(path);
final copyDirPointer =
dylib.lookup<ffi.NativeFunction<CopyDirFunc>>('copy_dir');
final copyDir = copyDirPointer.asFunction<CopyDir>();
const source = '/path/from/ag_fsm_docs'
ffi.Pointer<Utf8> utf8Pointer = source.toNativeUtf8();
const destination = '/path/to/rust_play_folder/Jahid'
ffi.Pointer<Utf8> utf8Pointer2 = destination.toNativeUtf8();
copyDir(utf8Pointer, utf8Pointer2);
calloc.free(utf8Pointer);
calloc.free(utf8Pointer2);
}`
英文:
When i am trying to Run this Function From Dart side it's always giving me this Problem
memory allocation of 140374366121105 bytes failed
Lost connection to device.
But my Source folder is only 2.4 kb
Here Is my Rust Function:-
fn copy_dir(source: &Path, destination: &Path) -> io::Result<()> {
if source.is_dir() {
fs::create_dir_all(destination)?;
for entry in fs::read_dir(source)? {
let entry = entry?;
let entry_path = entry.path();
let dest_path = destination.join(entry.file_name());
if entry_path.is_dir() {
fs::create_dir_all( &entry_path).unwrap();
} else {
fs::copy(&entry_path, &dest_path)?;
}
}
} else {
fs::copy(source, destination)?;
}
Ok(())
}
From dart side i am calling this function like this:-
`typedef CopyDirFunc = ffi.Void Function(
ffi.Pointer<Utf8> source,
ffi.Pointer<Utf8> destination,
);
typedef CopyDir = void Function(
ffi.Pointer<Utf8> source,
ffi.Pointer<Utf8> destination,
);
void main(){
final path = Platform.isWindows
? 'path\\to\\mylib.dll'
: '/path/to/mylib.so';
final dylib = ffi.DynamicLibrary.open(path);
final copyDirPointer =
dylib.lookup<ffi.NativeFunction<CopyDirFunc>>('copy_dir');
final copyDir = copyDirPointer.asFunction<CopyDir>();
const source = '/path/from/ag_fsm_docs';
ffi.Pointer<Utf8> utf8Pointer = source.toNativeUtf8();
const destination = '/path/to/rust_play_folder/Jahid';
ffi.Pointer<Utf8> utf8Pointer2 = destination.toNativeUtf8();
copyDir(utf8Pointer, utf8Pointer2);
calloc.free(utf8Pointer);
calloc.free(utf8Pointer2);
}`
答案1
得分: 1
你需要还原符号并使用 C 调用约定:
#[no_mangle]
extern fn copy_dir(source: *const u8, destination: *const u8) {…}
你也不应该在 FFI 中使用 Result
和 Path
。
英文:
You need to unmangle the symbol and use C calling convention:
#[no_mangle]
extern fn copy_dir(source: *const u8, destination: *const u8) {…}
You’re also not supposed to use Result
s and Path
s in FFI.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论