[Libguestfs] [PATCH 2/9] Rust bindings: Add create / close functions

Hiroyuki Katsura hiroyuki.katsura.0513 at gmail.com
Thu Jun 27 08:06:03 UTC 2019


From: Hiroyuki_Katsura <hiroyuki.katsura.0513 at gmail.com>

---
 generator/rust.ml | 84 ++++++++++++++++++++++++++++++++++++++++++++++-
 rust/Cargo.toml   |  2 ++
 2 files changed, 85 insertions(+), 1 deletion(-)

diff --git a/generator/rust.ml b/generator/rust.ml
index 83afdfe73..dbe9db010 100644
--- a/generator/rust.ml
+++ b/generator/rust.ml
@@ -31,4 +31,86 @@ open Events
 
 
 let generate_rust () =
-  generate_header CStyle LGPLv2plus;
\ No newline at end of file
+  generate_header CStyle LGPLv2plus;
+
+  pr "
+#[allow(non_camel_case_types)]
+enum guestfs_h {}
+
+extern \"C\" {
+    fn guestfs_create() -> *mut guestfs_h;
+    fn guestfs_create_flags(flags: i64) -> *mut guestfs_h;
+    fn guestfs_close(g: *mut guestfs_h);
+    static GUESTFS_CREATE_NO_ENVIRONMENT: i64;
+    static GUESTFS_CREATE_NO_CLOSE_ON_EXIT: i64;
+}
+
+pub struct Handle {
+    g: *mut guestfs_h,
+}
+
+impl Drop for Handle {
+    fn drop(&mut self) {
+        unsafe { guestfs_close(self.g) }
+    }
+}
+
+pub struct CreateFlags {
+    create_no_environment_flag: bool,
+    create_no_close_on_exit_flag: bool,
+}
+
+impl CreateFlags {
+    pub fn new() -> CreateFlags {
+        CreateFlags {
+            create_no_environment_flag: false,
+            create_no_close_on_exit_flag: false,
+        }
+    }
+
+    pub fn create_no_environment(mut self, flag: bool) -> CreateFlags {
+        self.create_no_environment_flag = flag;
+        self
+    }
+
+    pub fn create_no_close_on_exit_flag(mut self, flag: bool) -> CreateFlags {
+        self.create_no_close_on_exit_flag = flag;
+        self
+    }
+
+    unsafe fn to_libc_int(self) -> i64 {
+        let mut flag = 0;
+        flag |= if self.create_no_environment_flag {
+            GUESTFS_CREATE_NO_ENVIRONMENT
+        } else {
+            0
+        };
+        flag |= if self.create_no_close_on_exit_flag {
+            GUESTFS_CREATE_NO_CLOSE_ON_EXIT
+        } else {
+            0
+        };
+        flag
+    }
+}
+
+impl Handle {
+    pub fn create() -> Result<Handle, &'static str> {
+        let g = unsafe { guestfs_create() };
+        if g.is_null() {
+            Err(\"failed to create guestfs handle\")
+        } else {
+            Ok(Handle { g })
+        }
+    }
+
+    pub fn create_flags(flags: CreateFlags) -> Result<Handle, &'static str> {
+        let g = unsafe { guestfs_create_flags(flags.to_libc_int()) };
+        if g.is_null() {
+            Err(\"failed to create guestfs handle\")
+        } else {
+            Ok(Handle { g })
+        }
+    }
+}
+      "
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index e730ee830..6cd94ce6a 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -4,3 +4,5 @@ version = "0.1.0"
 edition = "2018"
 
 [dependencies]
+libc = "0.2"
+
-- 
2.20.1 (Apple Git-117)




More information about the Libguestfs mailing list