<div dir="ltr">Hi, I'm now implementing generators of Rust, and I faced a problem.<br><br>In order for the wrapper to 'understand' the struct passed from C API, it is required to incorporate the definition of the struct in C into Rust code. <br><br>I have two approaches.<br><br>1. Create raw struct(#[repr(C)]), which has the equivalent memory mapping to C struct and access through this struct in Rust<br>2. Use bindgen to create ffi struct from guestfs.h<br><br>Each of them has advantages and disadvantages.<br><br># 1st approach<br>This is highly dependent on the implementation of API because it is subject to the memory mapping of API struct. When the way struct is structured changes in API, you have to modify Rust bindings. In order to avoid this situation, you can define the same struct in C and Rust and then access the API struct in C and then Rust accesses the data through this struct. This means that<br><br>```<br>API: <br> struct guestfs_A: <br>  - field: x FString<br><br>C: <br> <br>struct RawA {<br>  char* x<br>};<br>struct RawA to_RawA(struct guestfs_A* src) {<br>    struct RawA x = {src->x};<br>    return x;<br>}  <br><br>Rust:<br>  <br>  #repr(C)<br>use std::ffi::CStr;<br>use std::str;<br><br>#[repr(C)]<br>struct RawA {<br>x: *const i8<br>}<br>enum guestfs_A {} // opaque struct<br>extern {<br>fn to_RawA( src: *const guestfs_A ) -> RawA;<br>}<br><br>struct A {<br>x: String<br>}<br><br>impl A {<br>    fn new(src: *const guestfs_A) -> A {<br>        let dst = unsafe {to_RawA(src)};<br>        let c_str = unsafe { CStr::from_ptr(dst.x) };<br>        let s = c_str.to_str().unwrap().to_string();<br>        A{ x: s }<br>    }<br>}  <br>```<div><br>This is a little verbose and inefficient.<br><br># 2nd approach<br><br>The above is easily done by 'bindgen', which automatically generates rust ffi bindings to C library. By using this, API struct is automatically generated. However, it requires a new dependency: libclang. <br><br># Question<br><br>Which of these approaches is more preferable? <br><div><br></div><div>Regards,</div><div>Hiroyuki Katsura</div></div></div>