<div dir="ltr">> Is this just trying if the guestfs can be linked with?<div><br></div><div>Yes. In OCaml bindings, there is the corresponding test(<a href="https://github.com/libguestfs/libguestfs/blob/master/ocaml/t/guestfs_010_load.ml">https://github.com/libguestfs/libguestfs/blob/master/ocaml/t/guestfs_010_load.ml</a>). I just mimicked it. If it is not required, I will remove it.</div><div><br></div><div>divided the generated files and handmade files in rust/src/ directory. I'll send this fixed patch to this mailing list.</div><div><br></div><div>I'm not sure about the license problems. Can you teach me that?</div><div><br></div><div>Regards,</div><div>Hiroyuki</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">2019年7月17日(水) 22:40 Martin Kletzander <<a href="mailto:mkletzan@redhat.com">mkletzan@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, Jul 17, 2019 at 06:49:39PM +0900, Hiroyuki Katsura wrote:<br>
>From: Hiroyuki_Katsura <<a href="mailto:hiroyuki.katsura.0513@gmail.com" target="_blank">hiroyuki.katsura.0513@gmail.com</a>><br>
><br>
>Rust bindings: Add create / close functions<br>
><br>
>Rust bindings: Add 4 bindings tests<br>
><br>
>Rust bindings: Add generator of structs<br>
><br>
>Rust bindings: Add generator of structs for optional arguments<br>
><br>
>Rust bindings: Add generator of function signatures<br>
><br>
>Rust bindings: Complete actions<br>
><br>
>Rust bindings: Fix memory management<br>
><br>
>Rust bindings: Add bindtests<br>
><br>
>Rust bindings: Add additional 4 bindings tests<br>
><br>
>Rust bindings: Format test files<br>
><br>
>Rust bindings: Incorporate bindings to build system<br>
>---<br>
> Makefile.am                         |   3 +<br>
> <a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a>                        |   6 +<br>
> generator/Makefile.am               |   3 +<br>
> generator/<a href="http://bindtests.ml" rel="noreferrer" target="_blank">bindtests.ml</a>              |  66 +++<br>
> generator/bindtests.mli             |   1 +<br>
> generator/<a href="http://main.ml" rel="noreferrer" target="_blank">main.ml</a>                   |   5 +<br>
> generator/<a href="http://rust.ml" rel="noreferrer" target="_blank">rust.ml</a>                   | 806 ++++++++++++++++++++++++++++<br>
> generator/rust.mli                  |  22 +<br>
> m4/guestfs-rust.m4                  |  33 ++<br>
> <a href="http://run.in" rel="noreferrer" target="_blank">run.in</a>                              |   9 +<br>
> rust/.gitignore                     |   3 +<br>
> rust/<a href="http://Cargo.toml.in" rel="noreferrer" target="_blank">Cargo.toml.in</a>                  |   6 +<br>
> rust/Makefile.am                    |  42 ++<br>
> rust/run-bindtests                  |  23 +<br>
> rust/run-tests                      |  21 +<br>
> rust/src/.gitkeep                   |   0<br>
> rust/src/bin/.gitkeep               |   0<br>
> rust/tests/.gitkeep                 |   0<br>
> rust/tests/<a href="http://010_load.rs" rel="noreferrer" target="_blank">010_load.rs</a>              |  24 +<br>
> rust/tests/<a href="http://020_create.rs" rel="noreferrer" target="_blank">020_create.rs</a>            |  24 +<br>
> rust/tests/<a href="http://030_create_flags.rs" rel="noreferrer" target="_blank">030_create_flags.rs</a>      |  29 +<br>
> rust/tests/<a href="http://040_create_multiple.rs" rel="noreferrer" target="_blank">040_create_multiple.rs</a>   |  38 ++<br>
> rust/tests/<a href="http://050_handle_properties.rs" rel="noreferrer" target="_blank">050_handle_properties.rs</a> |  62 +++<br>
> rust/tests/<a href="http://070_opt_args.rs" rel="noreferrer" target="_blank">070_opt_args.rs</a>          |  41 ++<br>
> rust/tests/<a href="http://080_version.rs" rel="noreferrer" target="_blank">080_version.rs</a>           |  26 +<br>
> rust/tests/<a href="http://090_ret_values.rs" rel="noreferrer" target="_blank">090_ret_values.rs</a>        |  61 +++<br>
> rust/tests/<a href="http://100_launch.rs" rel="noreferrer" target="_blank">100_launch.rs</a>            |  65 +++<br>
> 27 files changed, 1419 insertions(+)<br>
> create mode 100644 generator/<a href="http://rust.ml" rel="noreferrer" target="_blank">rust.ml</a><br>
> create mode 100644 generator/rust.mli<br>
> create mode 100644 m4/guestfs-rust.m4<br>
> create mode 100644 rust/.gitignore<br>
> create mode 100644 rust/<a href="http://Cargo.toml.in" rel="noreferrer" target="_blank">Cargo.toml.in</a><br>
> create mode 100644 rust/Makefile.am<br>
> create mode 100755 rust/run-bindtests<br>
> create mode 100755 rust/run-tests<br>
> create mode 100644 rust/src/.gitkeep<br>
> create mode 100644 rust/src/bin/.gitkeep<br>
> create mode 100644 rust/tests/.gitkeep<br>
> create mode 100644 rust/tests/<a href="http://010_load.rs" rel="noreferrer" target="_blank">010_load.rs</a><br>
> create mode 100644 rust/tests/<a href="http://020_create.rs" rel="noreferrer" target="_blank">020_create.rs</a><br>
> create mode 100644 rust/tests/<a href="http://030_create_flags.rs" rel="noreferrer" target="_blank">030_create_flags.rs</a><br>
> create mode 100644 rust/tests/<a href="http://040_create_multiple.rs" rel="noreferrer" target="_blank">040_create_multiple.rs</a><br>
> create mode 100644 rust/tests/<a href="http://050_handle_properties.rs" rel="noreferrer" target="_blank">050_handle_properties.rs</a><br>
> create mode 100644 rust/tests/<a href="http://070_opt_args.rs" rel="noreferrer" target="_blank">070_opt_args.rs</a><br>
> create mode 100644 rust/tests/<a href="http://080_version.rs" rel="noreferrer" target="_blank">080_version.rs</a><br>
> create mode 100644 rust/tests/<a href="http://090_ret_values.rs" rel="noreferrer" target="_blank">090_ret_values.rs</a><br>
> create mode 100644 rust/tests/<a href="http://100_launch.rs" rel="noreferrer" target="_blank">100_launch.rs</a><br>
><br>
>diff --git a/generator/<a href="http://rust.ml" rel="noreferrer" target="_blank">rust.ml</a> b/generator/<a href="http://rust.ml" rel="noreferrer" target="_blank">rust.ml</a><br>
>new file mode 100644<br>
>index 000000000..b7bc76da8<br>
>--- /dev/null<br>
>+++ b/generator/<a href="http://rust.ml" rel="noreferrer" target="_blank">rust.ml</a><br>
>@@ -0,0 +1,806 @@<br>
>+(* libguestfs<br>
>+ * Copyright (C) 2019 Red Hat Inc.<br>
>+ *<br>
>+ * This program is free software; you can redistribute it and/or modify<br>
>+ * it under the terms of the GNU General Public License as published by<br>
>+ * the Free Software Foundation; either version 2 of the License, or<br>
>+ * (at your option) any later version.<br>
>+ *<br>
>+ * This program is distributed in the hope that it will be useful,<br>
>+ * but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
>+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br>
>+ * GNU General Public License for more details.<br>
>+ *<br>
>+ * You should have received a copy of the GNU General Public License<br>
>+ * along with this program; if not, write to the Free Software<br>
>+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA<br>
>+*)<br>
>+<br>
>+(* Please read generator/README first. *)<br>
>+<br>
>+open Std_utils<br>
>+open Types<br>
>+open Utils<br>
>+open Pr<br>
>+open Docstrings<br>
>+open Optgroups<br>
>+open Actions<br>
>+open Structs<br>
>+open C<br>
>+open Events<br>
>+<br>
>+(* Utilities for Rust *)<br>
>+(* Are there corresponding functions to them? *)<br>
>+(* Should they be placed in <a href="http://utils.ml" rel="noreferrer" target="_blank">utils.ml</a>? *)<br>
>+let rec indent n = match n with<br>
>+  | x when x > 0 -> pr "    "; indent (x - 1)<br>
>+  | _ -> ()<br>
>+<br>
>+(* split_on_char exists since OCaml 4.04 *)<br>
>+(* but current requirements: >=4.01 *)<br>
>+let split_on_char c = Str.split (Str.regexp (String.make 1 c))<br>
>+<br>
>+let snake2caml name =<br>
>+  let l = split_on_char '_' name in<br>
>+  let l = List.map (fun x -> String.capitalize_ascii x) l in<br>
>+  String.concat "" l<br>
>+<br>
>+(* because there is a function which contains 'unsafe' field *)<br>
>+let black_list = ["unsafe"]<br>
>+<br>
>+let translate_bad_symbols s =<br>
>+  if List.exists (fun x -> s = x) black_list then<br>
>+    s ^ "_"<br>
>+  else<br>
>+    s<br>
>+<br>
>+let generate_rust () =<br>
>+  generate_header CStyle LGPLv2plus;<br>
>+<br>
>+  pr "<br>
<br>
I started with this as well (for the libnbd bindings), but it was a PITA for me<br>
to modify parts of the generator.  I then realized that I found out two things,<br>
each one should help you move part of this outside the generator:<br>
<br>
 1) you can have impls for the same struct in different files<br>
<br>
 2) you can scope the `pub`, for example `pub(crate)` would make the definition<br>
    public but only for this particular crate.  That way you can share things<br>
    between modules without exposing it to consumers of this crate<br>
<br>
>+use std::collections;<br>
>+use std::convert;<br>
>+use std::convert::TryFrom;<br>
>+use std::ffi;<br>
>+use std::os::raw::{c_char, c_int, c_void};<br>
>+use std::ptr;<br>
>+use std::slice;<br>
>+use std::str;<br>
>+<br>
>+#[allow(non_camel_case_types)]<br>
>+enum guestfs_h {} // opaque struct<br>
>+<br>
<br>
You should not use empty enums for ffi opaque structs as they can be optimized<br>
out.  Also #[repr(C)] it.<br>
<br>
You should rather have a struct with an empty member:<br>
<br>
  struct guestfs_handle {<br>
    _unused: [u32; 0],<br>
  }<br>
<br>
I'll try to find the official info in rust docs, I just do not know on which<br>
docs page it was.<br>
<br>
/me goes looking<br>
<br>
Oh yeah, it was in the nomicon:<br>
<br>
  <a href="https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs" rel="noreferrer" target="_blank">https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs</a><br>
<br>
It will be even nicer when RFC 1861 gets stabilized, but that's long long in the<br>
future, I guess:<br>
<br>
  <a href="https://github.com/rust-lang/rust/issues/43467" rel="noreferrer" target="_blank">https://github.com/rust-lang/rust/issues/43467</a><br>
<br>
Otherwise it looks very similar to what I started, so that should be good<br>
(hopefully) =)<br>
<br>
I hope I'll get some more time to go over the whole posting here and try it out<br>
as well.<br>
<br>
[...]<br>
<br>
>diff --git a/rust/Makefile.am b/rust/Makefile.am<br>
>new file mode 100644<br>
>index 000000000..ff284ec00<br>
>--- /dev/null<br>
>+++ b/rust/Makefile.am<br>
>@@ -0,0 +1,42 @@<br>
>+# libguestfs golang bindings<br>
<br>
golang? =)<br>
<br>
>+# Copyright (C) 2019 Red Hat Inc.<br>
<br>
I'll let Rich figure out what is supposed to be here :)<br>
<br>
>diff --git a/rust/run-bindtests b/rust/run-bindtests<br>
>new file mode 100755<br>
>index 000000000..2986e898d<br>
>--- /dev/null<br>
>+++ b/rust/run-bindtests<br>
>@@ -0,0 +1,23 @@<br>
>+#!/bin/sh -<br>
>+# libguestfs Golang bindings<br>
>+# Copyright (C) 2013 Red Hat Inc.<br>
<br>
dtto<br>
<br>
and some other files as well<br>
<br>
[...]<br>
<br>
>diff --git a/rust/tests/<a href="http://010_load.rs" rel="noreferrer" target="_blank">010_load.rs</a> b/rust/tests/<a href="http://010_load.rs" rel="noreferrer" target="_blank">010_load.rs</a><br>
>new file mode 100644<br>
>index 000000000..4cb43f2c1<br>
>--- /dev/null<br>
>+++ b/rust/tests/<a href="http://010_load.rs" rel="noreferrer" target="_blank">010_load.rs</a><br>
>@@ -0,0 +1,24 @@<br>
>+/* libguestfs Rust bindings<br>
>+Copyright (C) 2009-2019 Red Hat Inc.<br>
>+<br>
>+This program is free software; you can redistribute it and/or modify<br>
>+it under the terms of the GNU General Public License as published by<br>
>+the Free Software Foundation; either version 2 of the License, or<br>
>+(at your option) any later version.<br>
>+<br>
>+This program is distributed in the hope that it will be useful,<br>
>+but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
>+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br>
>+GNU General Public License for more details.<br>
>+<br>
>+You should have received a copy of the GNU General Public License<br>
>+along with this program; if not, write to the Free Software<br>
>+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.<br>
>+*/<br>
>+<br>
>+extern crate guestfs;<br>
>+<br>
>+#[test]<br>
>+fn load() {<br>
>+    // nop<br>
>+}<br>
<br>
Is this just trying if the guestfs can be linked with?<br>
<br>
>diff --git a/rust/tests/<a href="http://020_create.rs" rel="noreferrer" target="_blank">020_create.rs</a> b/rust/tests/<a href="http://020_create.rs" rel="noreferrer" target="_blank">020_create.rs</a><br>
>new file mode 100644<br>
>index 000000000..017dbbac0<br>
>--- /dev/null<br>
>+++ b/rust/tests/<a href="http://020_create.rs" rel="noreferrer" target="_blank">020_create.rs</a><br>
>@@ -0,0 +1,24 @@<br>
>+/* libguestfs Rust bindings<br>
>+Copyright (C) 2009-2019 Red Hat Inc.<br>
>+<br>
>+This program is free software; you can redistribute it and/or modify<br>
>+it under the terms of the GNU General Public License as published by<br>
>+the Free Software Foundation; either version 2 of the License, or<br>
>+(at your option) any later version.<br>
>+<br>
>+This program is distributed in the hope that it will be useful,<br>
>+but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
>+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br>
>+GNU General Public License for more details.<br>
>+<br>
>+You should have received a copy of the GNU General Public License<br>
>+along with this program; if not, write to the Free Software<br>
>+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.<br>
>+*/<br>
>+<br>
>+extern crate guestfs;<br>
>+<br>
>+#[test]<br>
>+fn create() {<br>
>+    assert!(!guestfs::Handle::create().is_err(), "create fail");<br>
<br>
Isn't Result.is_ok() same as !Result.is_err()?  Maybe even unwrap() would work<br>
here as it would actually show the error that was returned in the backtrace.<br>
<br>
[...]<br>
<br>
>diff --git a/rust/tests/<a href="http://050_handle_properties.rs" rel="noreferrer" target="_blank">050_handle_properties.rs</a> b/rust/tests/<a href="http://050_handle_properties.rs" rel="noreferrer" target="_blank">050_handle_properties.rs</a><br>
>new file mode 100644<br>
>index 000000000..0b955d5cf<br>
>--- /dev/null<br>
>+++ b/rust/tests/<a href="http://050_handle_properties.rs" rel="noreferrer" target="_blank">050_handle_properties.rs</a><br>
>@@ -0,0 +1,62 @@<br>
>+/* libguestfs Rust bindings<br>
>+Copyright (C) 2009-2019 Red Hat Inc.<br>
>+<br>
>+This program is free software; you can redistribute it and/or modify<br>
>+it under the terms of the GNU General Public License as published by<br>
>+the Free Software Foundation; either version 2 of the License, or<br>
>+(at your option) any later version.<br>
>+<br>
>+This program is distributed in the hope that it will be useful,<br>
>+but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
>+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br>
>+GNU General Public License for more details.<br>
>+<br>
>+You should have received a copy of the GNU General Public License<br>
>+along with this program; if not, write to the Free Software<br>
>+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.<br>
<br>
Oh, you're also using the older version of the license here (with the old<br>
"logistics" mentioned), but I guess that's fine.  I, for one, would ban these,<br>
but it's a recommendation to put them in each file (although I think it's an<br>
outdated one, but you never know with lawyers), so...<br>
</rant><br>
<br>
Anyway, it looks good, although I just skimmed it and I haven't tested it.<br>
<br>
Martin<br>
</blockquote></div>