rust - What is the best way to get a `*const c_char` from a const array via FFI? -


i trying interface c api in rust. define couple of string constants macros:

#define kofximageeffectpluginapi "ofximageeffectpluginapi" 

and struct const char *pluginapi; constant supposed used:

typedef struct ofxplugin {   const char *pluginapi;   // other fields... } ofxplugin; 

bindgen (servo) creates following rust equivalent:

pub const kofximageeffectpluginapi: &'static [u8; 24usize] = b"ofximageeffectpluginapi\x00"; 
#[repr(c)] #[derive(debug, copy)] pub struct ofxplugin {     pub pluginapi: *const ::std::os::raw::c_char,     // other fields... } 

what best way *const c_char const array? tried both as_ptr , cast, types don't match because array u8 , c_char i8...

let's start minimal example , work our way through:

const k: &'static [u8; 24usize] = b"ofximageeffectpluginapi\x00";  #[derive(debug)] struct ofxplugin {     plugin_api: *const ::std::os::raw::c_char,     // other fields... }  fn main() {     let p = ofxplugin { plugin_api: k };     println!("{:?}", p); } 

the first thing pointer array; indeed as_ptr().

error[e0308]: mismatched types   --> <anon>:10:41    | 10 |         let p = ofxplugin { plugin_api: k.as_ptr() };    |                                         ^^^^^^^^^^ expected i8, found u8  error: aborting due previous error 

the types mismatch, need cast 1 pointer type another. achieved as:

fn main() {     let p = ofxplugin { plugin_api: k.as_ptr() *const _ };     println!("{:?}", p); } 

we could explicit kind of pointer want; it's simpler here let compiler manage it.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -