<div dir="ltr">Dear Richard,<div><br></div><div>> We have tended to avoid this in other languages because the results</div>> can be very poor quality (eg. swig bindings are usually terrible).<br>> However if the resulting code from bindgen is high quality and<br>> accurately covers all the features that libguestfs bindings need then<br>> we can consider it.<div><br></div><div>Thank you for your reply. Actually, I have not used bindgen and I cannot guarantee the quality. However, in this bindings, generated structs are accessed not from bindings user but from only Rust bindings. And required features are the only translation of structs, whose types are just char, char *, uint32_t, char [32], uint32_t, int32_t, int64_t and float. I tested guestfs.h and it looked good.</div><div><br></div><div>But I am a newbie of developing libguestfs and I cannot assure the bindgen will not cause any problems, so I think I follow the convention for now. </div><div><br></div><div>Regards,</div><div>Hiroyuki</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">2019年6月5日(水) 16:32 Richard W.M. Jones <<a href="mailto:rjones@redhat.com">rjones@redhat.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Wed, Jun 05, 2019 at 03:33:51PM +0900, Hiroyuki Katsura wrote:<br>
> 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<br>
> is required to incorporate the definition of the struct in C into Rust<br>
> code.<br>
> <br>
> I have two approaches.<br>
<br>
I don't know a lot about Rust bindings, but ...<br>
<br>
> 1. Create raw struct(#[repr(C)]), which has the equivalent memory mapping<br>
> to C struct and access through this struct in Rust<br>
<br>
... this is what I did in nbdkit (although it is not generated, but<br>
written by hand in this case):<br>
<br>
<a href="https://github.com/libguestfs/nbdkit/blob/a5d94ab65110136680598677610868196ed71145/plugins/rust/src/lib.rs#L38" rel="noreferrer" target="_blank">https://github.com/libguestfs/nbdkit/blob/a5d94ab65110136680598677610868196ed71145/plugins/rust/src/lib.rs#L38</a><br>
<br>
> 2. Use bindgen to create ffi struct from guestfs.h<br>
<br>
We have tended to avoid this in other languages because the results<br>
can be very poor quality (eg. swig bindings are usually terrible).<br>
However if the resulting code from bindgen is high quality and<br>
accurately covers all the features that libguestfs bindings need then<br>
we can consider it.<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<br>
> to the memory mapping of API struct. When the way struct is structured<br>
> changes in API, you have to modify Rust bindings. In order to avoid this<br>
> situation, you can define the same struct in C and Rust and then access the<br>
> API struct in C and then Rust accesses the data through this struct. This<br>
> means that<br>
<br>
Sure, I understand all this.  We use the default C memory<br>
representation for the C compiler in use.  Rust should be able to deal<br>
with this naturally.<br>
<br>
> # 2nd approach<br>
> <br>
> The above is easily done by 'bindgen', which automatically generates rust<br>
> ffi bindings to C library. By using this, API struct is automatically<br>
> generated. However, it requires a new dependency: libclang.<br>
> <br>
> # Question<br>
> <br>
> Which of these approaches is more preferable?<br>
<br>
Depends on the quality of the bindgen output IMO.  If it's good, and<br>
covers all the features needed, use it.  If not, generate the repr(C)<br>
structs.<br>
<br>
Rich.<br>
<br>
-- <br>
Richard Jones, Virtualization Group, Red Hat <a href="http://people.redhat.com/~rjones" rel="noreferrer" target="_blank">http://people.redhat.com/~rjones</a><br>
Read my programming and virtualization blog: <a href="http://rwmj.wordpress.com" rel="noreferrer" target="_blank">http://rwmj.wordpress.com</a><br>
virt-builder quickly builds VMs from scratch<br>
<a href="http://libguestfs.org/virt-builder.1.html" rel="noreferrer" target="_blank">http://libguestfs.org/virt-builder.1.html</a><br>
</blockquote></div>