2014-10-27 65 views
0

我已經使用了rust-bindgen來生成防鏽接口代碼。現在Rust調用C,C代碼中的靜態常量

在C代碼,你可以發現這一點:

extern const struct mps_key_s _mps_key_ARGS_END; 
#define MPS_KEY_ARGS_END (&_mps_key_ARGS_END) 

注意,在代碼_mps_key_ARGS_END的孔休息不會再次出現。

宏MPS_KEY_ARGS_END在其他simular mps_key_s上得到了正常使用。

現在,通過防鏽定義BindGen產生的代碼是這樣的:

pub static _mps_key_ARGS_END: Struct_mps_key_s; 

現在,在這裏C代碼是一個示例用法:

extern void _mps_args_set_key(mps_arg_s args[MPS_ARGS_MAX], unsigned i, 
           mps_key_t key); 

_mps_args_set_key(args, 0, MPS_KEY_ARGS_END); 

在鏽它看起來像這樣:

pub fn _mps_args_set_key(args: [mps_arg_s, ..32u], i: ::libc::c_uint, 
         key: mps_key_t); 

現在我試圖這樣稱呼它:

_mps_args_set_key(args, 0 as u32, _mps_key_ARGS_END); 

但我得到一個錯誤:

error: mismatched types: expected *const Struct_mps_key_s , found Struct_mps_key_s (expected *-ptr, found enum Struct_mps_key_s)

我不是一個很好的C程序員,我甚至不明白的地方,這些C固定,即使那裏值從。

感謝您的幫助。

編輯:

更新的基礎上,克里斯摩根的答案。

我加入這個代碼(注意,我換成mps_key_t * const的mps_key_s):

pub static MPS_KEY_ARGS_END: mps_key_t = &_mps_key_ARGS_END; 

只是爲什麼即時通訊使用mps_key_t,在C上一些額外的信息:

typedef const struct mps_key_s *mps_key_t; 

生鏽:

pub type mps_key_t = *const Struct_mps_key_s; 

這接縫縫工作更好然後befor但現在我得到一個壞的崩潰:

error: internal compiler error: unexpected failure note: the compiler hit an unexpected failure path. this is a bug. note: we would appreciate a bug report: http://doc.rust-lang.org/complement-bugreport.html note: run with RUST_BACKTRACE=1 for a backtrace task 'rustc' failed at 'expected item, found foreign item _mps_key_ARGS_END::_mps_key_ARGS_END (id=1102)', /home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/libsyntax/ast_map/mod.rs:327

+0

內部編譯器錯誤總是一個錯誤,應該按照指示進行報告。 – 2014-10-27 04:48:08

+0

Allreay做了https://github.com/rust-lang/rust/issues/18360 – nickik 2014-10-27 05:02:41

回答

1
#define MPS_KEY_ARGS_END (&_mps_key_ARGS_END)

&部分指示它採用指針的對象,即的MPS_KEY_ARGS_END的類型將是mps_key_s const*。在Rust中,這是*const mps_key_s(一個原始指針),可以按照與C,&_mps_key_ARGS_END相同的方式實現。你可以用這樣的方式定義MPS_KEY_ARGS_END

static MPS_KEY_ARGS_END: *const mps_key_s = &_mps_key_ARGS_END; 
+0

感謝您的幫助,我更新了我的問題 – nickik 2014-10-27 04:46:24

+0

@MatthieuM。:好點(我不習慣在C中工作,忘記了這一點)。修訂。 – 2014-10-27 10:00:32