在Rust端,我写了一个函数,它返回一个字符串作为字节指针(以C结构在内存中布局):
在Rust侧,我编写了一个函数,该函数返回一个作为字节指针的字符串(在内存中以C结构布局):
#[repr(C)]
pub struct RustByteSlice {
pub bytes: *const u8,
pub len: size_t,
}
#[no_mangle]
pub extern "C" fn get_string_from_rust() -> RustByteSlice {
let s = "This is a string from Rust.";
RustByteSlice {
bytes: s.as_ptr(),
len: s.len() as size_t,
}
}
使用 cbindgen 为其生成头文件时,它会给我以下输出:
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct {
const uint8_t *bytes;
size_t len;
} RustByteSlice;
RustByteSlice get_string_from_rust(void);
char *hello(const char *to);
void hello_release(char *s);
void utf8_bytes_to_rust(const uint8_t *bytes, size_t len);
在我的Xcode项目中,该头文件被用作桥接头文件,并且从 Rust 代码编译的共享库已添加到依赖列表中。头文件和包含文件夹在构建属性中定义。
在 Swift 方面,我是这样调用 rust 函数的:struct RustByteSlice {
var bytes: UnsafePointer<UInt8>
var len: Int
func asUnsafeBufferPointer() -> UnsafeBufferPointer<UInt8> {
return UnsafeBufferPointer(start: bytes, count: len)
}
func asString(encoding: String.Encoding = String.Encoding.utf8) -> String? {
return String(bytes: asUnsafeBufferPointer(), encoding: encoding)
}
}
func strPtrRet() {
let rustString: RustByteSlice = get_string_from_rust()
if let stringFromRust = rustString.asString() {
print("got a string from Rust: (stringFromRust)")
} else {
print("Could not parse Rust string as UTF-8")
}
}
在这一行 let rustString: RustByteSlice = get_string_from_rust()
,我得到了以下错误:
Cannot convert value of type '__ObjC.RustByteSlice' to specified type 'ed25_ios_app.RustByteSlice'
我该如何解决或绕过这个错误?