qemu-devel
[Top][All Lists]
Advanced

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

Re: [PATCH] PoC: Rust binding for QAPI (qemu-ga only, for now)


From: Paolo Bonzini
Subject: Re: [PATCH] PoC: Rust binding for QAPI (qemu-ga only, for now)
Date: Fri, 11 Sep 2020 12:24:44 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0


On Thu, Sep 10, 2020 at 7:49 PM <marcandre.lureau@redhat.com> wrote:
> The usage is relatively simple:
>
> - from_qemu_none(ptr: *const sys::P) -> T
>   Return a Rust type T for a const ffi pointer P.
>
> - from_qemu_full(ptr: *mut sys::P) -> T
>   Return a Rust type T for a ffi pointer P, taking ownership.
>
> - T::to_qemu_none() -> Stash<P>
>   Returns a borrowed ffi pointer P (using a Stash to destroy "glue"
>   storage data, if any).
>
> - T::to_qemu_full() -> P
>   Returns a ffi pointer P. (P resources are leaked/passed to C/ffi)

I know these come from glib-rs, but still the names are awful. :)

What about:

- an unsafe variant of From/Into for from_qemu_full:

    trait UnsafeFrom<T> {
        unsafe fn unsafe_from(_: T) -> Self;
    }
    trait UnsafeInto<T> {
        unsafe fn unsafe_into(self) -> T;
    }
    impl <T, U> UnsafeInto<U> for T where U: UnsafeFrom<T> {
        unsafe fn unsafe_into(self) -> U { U::unsafe_from(self) }
    }

Example:

    impl UnsafeFrom<*mut c_char> for String {
        unsafe fn unsafe_from(ptr: *mut c_char) -> Self {
            let res = Self::new_from_foreign(ptr);
            libc::free(ptr as *mut c_void);
            res
        }
    }

- likewise, a generic IntoRaw trait for to_qemu_full:

    trait IntoRaw<T> {
        fn into_raw(self) -> *mut T;
    }

Example:

    impl IntoRaw<c_char> for String {
        fn into_raw(self) -> *mut c_char {
            unsafe {
                libc::strndup(self.as_ptr() as *const c_char,
                              self.len() as size_t)
            }
        }
    }

- and a simpler/nicer version of Stash, from_qemu_none and to_qemu_none like 
this:

    pub struct BorrowedPointer<'a, P, T: 'a> {
        pub native: *const P,
        pub storage: T,
        _marker: PhantomData<&'a T>,
    }

    impl<'a, P: Copy, T: 'a> BorrowedPointer<'a, P, T> {
        fn new(native: *const P, storage: T) -> Self {
            BorrowedPointer {
                native,
                storage,
                _marker: PhantomData
            }
        }

        fn as_ptr(&self) -> *const P {
            self.native
        }
    }

    trait ForeignConvertible<'a> {
        type Native: Copy;
        type Storage: 'a;
         unsafe fn new_from_foreign(p: *const Self::Native) -> Self;
        fn as_foreign(&'a self) -> BorrowedPointer<'a, Self::Native, 
Self::Storage>;
    }

Implemented like this:

    impl ForeignConvertible<'_> for String {
        type Native = c_char;
        type Storage = CString;

        unsafe fn new_from_foreign(p: *const c_char) -> Self {
            let cstr = CStr::from_ptr(p);
            String::from_utf8_lossy(cstr.to_bytes()).into_owned()
        }
        fn as_foreign(&self) -> BorrowedPointer<c_char, CString> {
            let tmp = CString::new(&self[..]).unwrap();
            BorrowedPointer::new(tmp.as_ptr(), tmp)
        }
    }

and possibly:

    impl<'a, P: Copy, T: 'a> BorrowedMutPointer<'a, P, T> {
        fn new(native: *mut P, storage: T) -> Self {
            BorrowedMutPointer {
                native,
                storage,
                _marker: PhantomData
            }
        }

        fn as_ptr(&self) -> *const P {
            self.native
        }

        fn as_mut_ptr(&mut self) -> *mut P {
            self.native
        }
    }

    trait ForeignMutConvertible<'a>: ForeignConvertible<'a> {
      fn as_foreign_mut(&self) -> BorrowedMutPointer<Self::Native, 
Self::Storage>;
    }

I placed the source code for the above at https://github.com/bonzini/rust-ptr

I'll look later at the rest of the code.  It's quite big. :)

Paolo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]