mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
Rust bindings: Add Event structs, Clarify Handle lifetime
Without clarifying handle's lifetime, it is unable to see how long the callbacks which the handle owns will live. Then, Rust compiler will infer that the callbacks have 'static lifetime. It is not convenient for users.
This commit is contained in:
committed by
Richard W.M. Jones
parent
ab09bc25c7
commit
bb0cb3e730
@@ -17,6 +17,9 @@
|
||||
*/
|
||||
|
||||
use crate::error;
|
||||
use crate::event;
|
||||
use crate::guestfs;
|
||||
use std::collections;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[repr(C)]
|
||||
@@ -34,31 +37,37 @@ extern "C" {
|
||||
const GUESTFS_CREATE_NO_ENVIRONMENT: i64 = 1;
|
||||
const GUESTFS_CREATE_NO_CLOSE_ON_EXIT: i64 = 2;
|
||||
|
||||
pub struct Handle {
|
||||
pub struct Handle<'a> {
|
||||
pub(crate) g: *mut guestfs_h,
|
||||
pub(crate) callbacks: collections::HashMap<
|
||||
event::EventHandle,
|
||||
Box<Box<dyn Fn(guestfs::Event, event::EventHandle, &[u8], &[u64]) + 'a>>,
|
||||
>,
|
||||
}
|
||||
|
||||
impl Handle {
|
||||
pub fn create() -> Result<Handle, error::Error> {
|
||||
impl<'a> Handle<'a> {
|
||||
pub fn create() -> Result<Handle<'a>, error::Error> {
|
||||
let g = unsafe { guestfs_create() };
|
||||
if g.is_null() {
|
||||
Err(error::Error::Create)
|
||||
} else {
|
||||
Ok(Handle { g })
|
||||
let callbacks = collections::HashMap::new();
|
||||
Ok(Handle { g, callbacks })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_flags(flags: CreateFlags) -> Result<Handle, error::Error> {
|
||||
pub fn create_flags(flags: CreateFlags) -> Result<Handle<'a>, error::Error> {
|
||||
let g = unsafe { guestfs_create_flags(flags.to_libc_int()) };
|
||||
if g.is_null() {
|
||||
Err(error::Error::Create)
|
||||
} else {
|
||||
Ok(Handle { g })
|
||||
let callbacks = collections::HashMap::new();
|
||||
Ok(Handle { g, callbacks })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Handle {
|
||||
impl<'a> Drop for Handle<'a> {
|
||||
fn drop(&mut self) {
|
||||
unsafe { guestfs_close(self.g) }
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ impl convert::From<str::Utf8Error> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl base::Handle {
|
||||
impl<'a> base::Handle<'a> {
|
||||
pub(crate) fn get_error_from_handle(&self, operation: &'static str) -> Error {
|
||||
let c_msg = unsafe { guestfs_last_error(self.g) };
|
||||
let message = unsafe { utils::char_ptr_to_string(c_msg).unwrap() };
|
||||
|
||||
4
rust/src/event.rs
Normal file
4
rust/src/event.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
#[derive(Hash, PartialEq, Eq)]
|
||||
pub struct EventHandle {
|
||||
eh: i32,
|
||||
}
|
||||
@@ -18,9 +18,11 @@
|
||||
|
||||
mod base;
|
||||
mod error;
|
||||
mod event;
|
||||
mod guestfs;
|
||||
mod utils;
|
||||
|
||||
pub use crate::base::*;
|
||||
pub use crate::error::*;
|
||||
pub use crate::event::*;
|
||||
pub use crate::guestfs::*;
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
extern crate guestfs;
|
||||
|
||||
fn create() -> guestfs::Handle {
|
||||
fn create<'a>() -> guestfs::Handle<'a> {
|
||||
match guestfs::Handle::create() {
|
||||
Ok(g) => g,
|
||||
Err(e) => panic!("fail: {:?}", e),
|
||||
|
||||
Reference in New Issue
Block a user