使用Rust FFI时如何将复杂结构返回给Python?

时间:2016-08-17 08:16:35

标签: rust ffi

我希望在Rust中重写Python项目的某些部分以加快速度。我们的想法是使用Rust的FFI接口通过ctypes连接到Python。我每晚都在使用Rust 1.10。

我需要将相当复杂的数组/结构返回给Python。这不能正常工作。不幸的是,我在网上找不到任何关于此类问题的教程。

extern crate libc;
use libc::{c_int, c_char, size_t, c_float, c_double};
use std::ffi::{CStr, CString};
use std::str;
use std::vec::Vec;
use std::slice;
use std::mem;

#[repr(C)]
pub struct CPeak {
    // chemsc: *mut c_char, // adduct: c_char_p,
    mz_exp: c_float,
    mz_diff: c_float,
    intensity: c_float,
    resolution: c_float,
    noise: c_float,
    nb_frg: c_int,
    frgs: *mut CFrg,
}

#[repr(C)]
pub struct CFrg {
    mz_exp: c_float,
    mz_diff: c_float,
    intensity: c_float,
    resolution: c_float,
    noise: c_float,
}

#[no_mangle]
pub extern "C" fn standard_finder_get_standards(mut data: *mut *mut CPeak, mut len: *mut size_t) {
    // fill 'peaks' vector
    let peaks = find_standards(&standards.standards, "test.xml", 5.0, 10.0);

    // Copy contents of peaks (Rust structure) to CPeaks (C structure).
    // NOTE that CPeaks has a entry in the struct which is also a vector of
    // structs (frgs)
    let mut cpeaks: Vec<CPeak> = vec![];
    for peak in &peaks {

        // mk a vector frgs
        let mut frgs: Vec<CFrg> = vec![];
        if peak.frgs.len() > 0 {
            for frg in &peak.frgs {
                let f = CFrg {
                    mz_exp: frg.mz as c_float,
                    mz_diff: frg.mz_diff as c_float,
                    intensity: frg.intensity as c_float,
                    resolution: frg.resolution as c_float,
                    noise: frg.resolution as c_float,
                };
                frgs.push(f);
            }
        }
        frgs.shrink_to_fit();

        // mk a vector cpeaks
        cpeaks.push(CPeak {
            mz_exp: peak.mz as c_float,
            mz_diff: peak.mz_diff as c_float,
            intensity: peak.intensity as c_float,
            resolution: peak.resolution as c_float,
            noise: peak.resolution as c_float,
            nb_frg: peak.frgs.len() as c_int,
            frgs: frgs.as_ptr() as *mut CFrg, // <- add the frgs vector as c pointer (array)
        });
    }
    cpeaks.shrink_to_fit();
    unsafe {
        *data = cpeaks.as_ptr() as *mut CPeak;
        *len = cpeaks.len() as size_t;
    }
    mem::forget(cpeaks);
}

Playground)。

此代码采用Rust向量(peaks: Vec<Peak>)并将其内容复制到其C结构数组对应(cpeaks: Vec<CPeak>)。 CPeak内是另一个指向CFrg数组的指针。 cpeaks向量作为引用调用返回,以便为错误返回值留出空间。

当我尝试用Python读取数据时,我有两个问题:

  • cpeaks的第一个条目为空或垃圾
  • CFrg条目都是垃圾。

我猜这个问题是cpeaks的生命周期,这可能不足以在Python中访问。但是,我不知道如何让它保持活力。我试过Box,但这没有帮助。

1 个答案:

答案 0 :(得分:1)

您可以将rust代码称为python模块。

https://developers.redhat.com/blog/2017/11/16/speed-python-using-rust/

我不确定这是否是您要的东西,但也许值得一看。