[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH 02/12] Rust bindings: Add create / close functions



From: Hiroyuki_Katsura <hiroyuki katsura 0513 gmail com>

---
 generator/rust.ml | 90 ++++++++++++++++++++++++++++++++++++++++++++++-
 run.in            |  9 +++++
 rust/Cargo.toml   |  2 +-
 3 files changed, 99 insertions(+), 2 deletions(-)

diff --git a/generator/rust.ml b/generator/rust.ml
index 40ca8d198..d235dbefe 100644
--- a/generator/rust.ml
+++ b/generator/rust.ml
@@ -30,5 +30,93 @@ open C
 open Events
 
 let generate_rust () =
-  generate_header CStyle LGPLv2plus
+  generate_header CStyle LGPLv2plus;
+
+  pr "
+#[allow(non_camel_case_types)]
+enum guestfs_h {}
+
+#[link(name = \"guestfs\")]
+extern \"C\" {
+    fn guestfs_create() -> *mut guestfs_h;
+    fn guestfs_create_flags(flags: i64) -> *mut guestfs_h;
+    fn guestfs_close(g: *mut guestfs_h);
+}
+
+const GUESTFS_CREATE_NO_ENVIRONMENT: i64 = 1;
+const GUESTFS_CREATE_NO_CLOSE_ON_EXIT: i64 = 2;
+
+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 none() -> CreateFlags {
+        CreateFlags {
+            create_no_environment_flag: false,
+            create_no_close_on_exit_flag: false,
+        }
+    }
+
+    pub fn new() -> CreateFlags {
+        CreateFlags::none()
+    }
+
+    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/run.in b/run.in
index 488e1b937..301b02664 100755
--- a/run.in
+++ b/run.in
@@ -201,6 +201,15 @@ else
 fi
 export CGO_LDFLAGS
 
+# For rust
+export RUST="@RUST@"
+if [ -z "$RUSTFLAGS" ]; then
+    RUSTFLAGS="-C link-args=-L$b/lib/.libs"
+else
+    RUSTFLAGS="$RUSTFLAGS -C link-args=-L$b/lib/.libs"
+fi
+export RUSTFLAGS
+
 # For GObject, Javascript and friends.
 export GJS="@GJS@"
 prepend GI_TYPELIB_PATH "$b/gobject"
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index e730ee830..4ea7c299b 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -1,5 +1,5 @@
 [package]
-name = "rust"
+name = "guestfs"
 version = "0.1.0"
 edition = "2018"
 
-- 
2.20.1 (Apple Git-117)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]