调用Rust函数(Copy_Dir)从Dart(使用ffi)

huangapple go评论57阅读模式
英文:

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 中使用 ResultPath

英文:

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 Results and Paths in FFI.

huangapple
  • 本文由 发表于 2023年2月27日 12:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定