简体   繁体   English

通过带有 c 绑定的 FFI 公开 Rust 函数

[英]Expose Rust function through FFI with c bindings

I'm creating a Rust library and want to expose my Rust functions through c bindings to Dart .我正在创建一个 Rust 库,并希望通过 c 绑定公开我的 Rust 函数到Dart This question is just related to the setup of actually exposing the Rust function through C bindings and not on how to call it in Dart .这个问题只与通过 C 绑定实际公开 Rust 函数的设置有关,与如何在Dart调用它无关。
This is my function which I want to expose through FFI :这是我想通过FFI公开的功能:

pub fn create_channel(credential: String) -> Result<String, iota_streams::core::Error> {
    let seed = create_seed::new();

    // Create the Transport Client
    let client = Client::new_from_url(&dotenv::var("URL").unwrap());
    let mut author = Author::new(&seed, ChannelType::SingleBranch, client.clone());

    // Create the channel with an announcement message. Make sure to save the resulting link somewhere,
    let announcement_link = author.send_announce()?;

    // This link acts as a root for the channel itself
    let ann_link_string = announcement_link.to_string();

    // Author will now send signed encrypted messages in a chain
    let msg_inputs = vec![credential];

    let mut prev_msg_link = announcement_link;
    for input in &msg_inputs {
        let (msg_link, _seq_link) = author.send_signed_packet(
            &prev_msg_link,
            &Bytes::default(),
            &Bytes(input.as_bytes().to_vec()),
        )?;
        println!("Sent msg: {}", msg_link);
        prev_msg_link = msg_link;
    }
    Ok(ann_link_string)
}

The credential String is supposed to be just a stringified json object. credential字符串应该只是一个字符串化的 json 对象。 Which I want to provide from Dart through C bindings into Rust and then use inside my create_channel function.我想从Dart通过 C 绑定提供给 Rust,然后在我的create_channel函数中使用。 But I don't know how to define the type of my credential parameter, because it would come in as a C type and then would need to be converted to Rust.但是我不知道如何定义我的credential参数的类型,因为它会以 C 类型出现,然后需要转换为 Rust。

#[no_mangle]
pub extern "C" fn create_channel(credential: *const raw::c_char) -> String {
    streams::create_channel(credential).unwrap()
}

Right now I'm just defining the parameters of my extern function to be off type c_char but I would then need to convert this C type to a Rust String or &str .现在我只是将我的extern函数的参数定义为c_char类型,但我需要将此C类型转换为 Rust String&str So that I can use it inside of my actual create_channel function written in Rust.这样我就可以在用 Rust 编写的实际create_channel函数中使用它。

As what types should I define the credential parameter and how would I convert the c_char into a String or &str?至于我应该定义什么类型的credential参数,以及如何将c_char转换为String&str?

Rust has the convenient CStr and CString types, you can use Cstr::from_ptr to wrap the raw string and then call to_str on that. Rust 有方便的CStrCString类型,你可以使用Cstr::from_ptr来包装原始字符串,然后调用to_str Of course you need to do some error handling here for cases where the string isn't valid UTF-8.当然,对于字符串不是有效 UTF-8 的情况,您需要在此处进行一些错误处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM