2
我試圖從一個Ruby模塊傳遞一個FFI結構到生鏽,突變結構並將結構傳回給ruby模塊。從Ruby FFI返回變性的鏽結構struct
在這種情況下處理生命期的正確方法是什麼?
我遇到了一個終生的錯誤:
src/lib.rs:20:55: 20:70 error: missing lifetime specifier [E0106]
src/lib.rs:20 pub extern fn add_one_to_vals(numbers: TwoNumbers) -> &mut TwoNumbers {
^~~~~~~~~~~~~~~
src/lib.rs:20:55: 20:70 help: run `rustc --explain E0106` to see a detailed explanation
src/lib.rs:20:55: 20:70 help: this function's return type contains a borrowed value, but the signature does not say which one of `numbers`'s 0 elided lifetimes it is borrowed from
鏽代碼:
pub struct TwoNumbers {
first: i32,
second: i32,
}
impl TwoNumbers {
fn plus_one_to_each(&mut self) -> &mut TwoNumbers {
self.first = self.first + 1;
self.first = self.second + 1;
self
}
}
#[no_mangle]
pub extern fn add_one_to_vals(numbers: TwoNumbers) -> &mut TwoNumbers {
numbers.plus_one_to_each()
}
你需要'再版(C)'上' struct'是否跨越邊界使用 –
您可能需要閱讀[* The Rust FFI Omnibus *](http://jakegoulding.com/rust-ffi-omnibus/)並提出改進建議(如果它們缺失)。 – Shepmaster